验证码配色为统一颜色,只是透明不同,感觉这样更安全些。02 /**03 * @package 验证码类04 * @version 1.105 *06 * @link www.baoluowanxiang.com07 *08 */09 class Captcha {10 /**11 * 检验验证码时注意:验证码采用下面方式保存12 * SESSION中保存的值为: $_SESSION['Captcha'] = md5(strtoupper(验证码文本));13 */14 15 private $_fontDir = '../font/ariali.ttf'; // 验证码字体 这个字体系统里都带着呐 自己去找找16 private $_imgWidth = 120; // 背景宽度17 private $_imgHeight = 40; // 背景高度18 private $_imgText = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // 验证码字符集19 private $_imgTextLen = 4; // 验证码长度20 private $_imgTextSP = 24; // 验证码字符间距21 private $_imgTextT = 18; // 验证码最大字体22 private $_imgTextB = 14; // 验证码最小字体23 private $_imgTextRot = true; // 字体倾斜 treu为启用 false为关闭24 private $_imgBGLine = 5; // 背景干扰线数量25 private $_imgLine = 3; // 横向干扰线数量26 private $_imgDot = 100; // 干扰点数量27 function __construct()28 {29 // 定义PHP输出内容类型30 header('Content-Type: image/png');31 session_start();32 // 新建真彩图33 $img = imagecreatetruecolor($this->_imgWidth, $this->_imgHeight);34 // 真彩图背景色35 $imgColor = imagecolorallocate($img, 255, 255, 255);36 // 填充真彩图37 imagefill($img, 0, 0, $imgColor);38 // 随机RGB值39 $r = rand(50, 120);40 $g = rand(50, 120);41 $b = rand(50, 120);42 // 干扰线颜色43 $linecolor = imagecolorallocatealpha($img, $r, $g, $b, 85);44 // 背景干扰线45 for ($i=0; $i _imgBGLine; $i++) {46 imagesetthickness($img, rand(8, 16));47 imageline($img, rand(5, 60), rand(0, $this->_imgHeight), rand($this->_imgWidth-5, $this->_imgHeight-60), rand($this->_imgHeight, 0), $linecolor);48 }49 // 横向干扰线50 for ($i=0; $i _imgLine; $i++) {51 imagesetthickness($img, rand(4, 8));52 imageline($img, 0, rand(0, $this->_imgHeight), $this->_imgWidth, rand($this->_imgHeight, 0), $linecolor);53 }54 // 干扰点55 for ($i=0; $i _imgDot; $i++) {56 $dotcolor = imagecolorallocatealpha($img, $r, $g, $b, rand(0,20));57 imagesetpixel($img, rand(0, $this->_imgWidth), rand(0,$this->_imgHeight), $dotcolor);58 }59 // 文字颜色60 $imgTextColor = imagecolorallocate($img, $r, $g, $b);61 //session数组62 $sessionvalue = array();63 // 文字X轴起点64 $textx = rand(2,20);65 // 根据验证码长度循环写入字符66 for ($i=0; $i _imgTextLen; $i++) {67 // 取得验证码字符集的长度68 $strlen = strlen($this->_imgText);69 // 随机取一个验证码字符集内字符70 $text = $this->_imgText[rand(0, $strlen-1)];71 // 把取得字符添加到sessionvalue数组72 $sessionvalue[$i] = $text;73 if ( $this->_imgTextRot !== false ) {74 $textrot = rand(-20, 20);75 }else{76 $textrot = 0;77 }78 imagettftext($img, rand($this->_imgTextB, $this->_imgTextT), $textrot, $textx+($i*$this->_imgTextSP), rand(20, $this->_imgHeight-4), $imgTextColor, $this->_fontDir, $text);79 }80 // 把sessionvalue数组内元素合并为字符串81 $sessionText = implode('', $sessionvalue);82 // 把字符串保存到$_SESSION['Captcha']中83 $_SESSION['Captcha'] = md5(strtoupper($session)Text);84 imagepng($img);85 }86 function __destruct ()87 {88 // 清除图像资源89 imagedestroy($img);90 }91 }92 $Captcha = new Captcha();93 ?>本文由www.njtbo.com整理转发