ThinkPHP 3.2.3 自帶的驗證碼類位於 /ThinkPHP/Library/Think/Verify.class.php,字體文件位於 /ThinkPHP/Library/Think/Verify/javascript
能夠在 Verify.class.php 文件內進行修改,也能夠單獨寫一個類繼承自帶的驗證碼類。若是單獨寫一個繼承的類,能夠重用父類的屬性和方法,可是要注意的是父類中有一些屬性和方法是私有(private)的,能夠修改這些私有的屬性和方法爲保護(protected)的,若是不但願修改框架自帶的方法的話,也能夠在子類中再定義這些屬性和方法。php
測試的控制器位於 /Application/Home/Controller/TestVerifyController.class.phphtml
測試的試圖位於 /Application/Home/View/User/verify.htmljava
自定義的子類位於 /Applicaion/Home/Common/VerifyProcess.class.phpjquery
VerifyProcess.class.php:session
<?php namespace Home\Common; use Think\Verify; class VerifyProcess extends Verify { private $_image = NULL; // 驗證碼圖片實例 private $_color = NULL; // 驗證碼字體顏色 public function entryProcess($id = '') { // 圖片寬(px) $this->imageW || $this->imageW = $this->length*$this->fontSize*1.5 + $this->length*$this->fontSize/2; // 圖片高(px) $this->imageH || $this->imageH = $this->fontSize * 2.5; // 創建一幅 $this->imageW x $this->imageH 的圖像 $this->_image = imagecreate($this->imageW, $this->imageH); // 設置背景 imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]); // 驗證碼字體隨機顏色 $this->_color = imagecolorallocate($this->_image, mt_rand(1,150), mt_rand(1,150), mt_rand(1,150)); // 驗證碼使用隨機字體 $ttfPath = $_SERVER['DOCUMENT_ROOT'].'/ThinkPHP/Library/Think/Verify/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/'; if(empty($this->fontttf)){ $dir = dir($ttfPath); $ttfs = array(); while (false !== ($file = $dir->read())) { if($file[0] != '.' && substr($file, -4) == '.ttf') { $ttfs[] = $file; } } $dir->close(); $this->fontttf = $ttfs[array_rand($ttfs)]; } $this->fontttf = $ttfPath . $this->fontttf; if($this->useImgBg) { $this->_background(); } if ($this->useNoise) { // 繪雜點 $this->_writeNoise(); } if ($this->useCurve) { // 繪干擾線 $this->_writeCurve(); } // 繪驗證碼 $codeNX = 0; // 驗證碼第N個字符的左邊距 // 驗證碼爲簡單運算 $a = mt_rand(1,9); $b = mt_rand(1,9); $operate_array = array('+', '-', '*'); $key = mt_rand(0, count($operate_array) - 1); if($operate_array[$key] == '+') { // 加法 $code = $a.'+'.$b.'='; $result = intval($a + $b); } elseif($operate_array[$key] == '-') { // 減法 $code = max($a,$b).'-'.min($a,$b).'='; $result = intval(abs($a - $b)); } else { // 乘法 $code = $a.'*'.$b.'='; $result = intval($a * $b); } $this->length = 4; for ($i = 0; $i<$this->length; $i++) { $codeNX += mt_rand($this->fontSize*1.2, $this->fontSize*1.6); imagettftext($this->_image, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize*1.6, $this->_color, $this->fontttf, $code[$i]); } // 保存驗證碼 $key = $this->authcode($this->seKey); $result = $this->authcode($result); $secode = array(); $secode['verify_code'] = $result; // 把校驗碼保存到session $secode['verify_time'] = NOW_TIME; // 驗證碼建立時間 session($key.$id, $secode); header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header("content-type: image/png"); // 輸出圖像 imagepng($this->_image); imagedestroy($this->_image); } /** * 畫雜點 * 往圖片上寫不一樣顏色的字母或數字 */ private function _writeNoise() { $codeSet = '2345678abcdefhijkmnpqrstuvwxyz'; for($i = 0; $i < 10; $i++){ //雜點顏色 $noiseColor = imagecolorallocate($this->_image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225)); for($j = 0; $j < 5; $j++) { // 繪雜點 imagestring($this->_image, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor); } } } /** * 畫一條由兩條連在一塊兒構成的隨機正弦函數曲線做干擾線(你能夠改爲更帥的曲線函數) * * 高中的數學公式咋都忘了涅,寫出來 * 正弦型函數解析式:y=Asin(ωx+φ)+b * 各常數值對函數圖像的影響: * A:決定峯值(即縱向拉伸壓縮的倍數) * b:表示波形在Y軸的位置關係或縱向移動距離(上加下減) * φ:決定波形與X軸位置關係或橫向移動距離(左加右減) * ω:決定週期(最小正週期T=2π/∣ω∣) * */ private function _writeCurve() { $px = $py = 0; // 曲線前部分 $A = mt_rand(1, $this->imageH/2); // 振幅 $b = mt_rand(-$this->imageH/4, $this->imageH/4); // Y軸方向偏移量 $f = mt_rand(-$this->imageH/4, $this->imageH/4); // X軸方向偏移量 $T = mt_rand($this->imageH, $this->imageW*2); // 週期 $w = (2* M_PI)/$T; $px1 = 0; // 曲線橫座標起始位置 $px2 = mt_rand($this->imageW/2, $this->imageW * 0.8); // 曲線橫座標結束位置 for ($px=$px1; $px<=$px2; $px = $px + 1) { if ($w!=0) { $py = $A * sin($w*$px + $f)+ $b + $this->imageH/2; // y = Asin(ωx+φ) + b $i = (int) ($this->fontSize/5); while ($i > 0) { imagesetpixel($this->_image, $px + $i , $py + $i, $this->_color); // 這裏(while)循環畫像素點比imagettftext和imagestring用字體大小一次畫出(不用這while循環)性能要好不少 $i--; } } } // 曲線後部分 $A = mt_rand(1, $this->imageH/2); // 振幅 $f = mt_rand(-$this->imageH/4, $this->imageH/4); // X軸方向偏移量 $T = mt_rand($this->imageH, $this->imageW*2); // 週期 $w = (2* M_PI)/$T; $b = $py - $A * sin($w*$px + $f) - $this->imageH/2; $px1 = $px2; $px2 = $this->imageW; for ($px=$px1; $px<=$px2; $px=$px+ 1) { if ($w!=0) { $py = $A * sin($w*$px + $f)+ $b + $this->imageH/2; // y = Asin(ωx+φ) + b $i = (int) ($this->fontSize/5); while ($i > 0) { imagesetpixel($this->_image, $px + $i, $py + $i, $this->_color); $i--; } } } } /* 加密驗證碼 */ private function authcode($str){ $key = substr(md5($this->seKey), 5, 8); $str = substr(md5($str), 8, 10); return md5($key . $str); } /** * 繪製背景圖片 * 注:若是驗證碼輸出圖片比較大,將佔用比較多的系統資源 */ private function _background() { $path = dirname(__FILE__).'/Verify/bgs/'; $dir = dir($path); $bgs = array(); while (false !== ($file = $dir->read())) { if($file[0] != '.' && substr($file, -4) == '.jpg') { $bgs[] = $path . $file; } } $dir->close(); $gb = $bgs[array_rand($bgs)]; list($width, $height) = @getimagesize($gb); // Resample $bgImage = @imagecreatefromjpeg($gb); @imagecopyresampled($this->_image, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height); @imagedestroy($bgImage); } }
TestVerifyController.class.php:app
<?php namespace Home\Controller; use Think\Controller; use Home\Common\VerifyProcess; class TestVerifyController extends Controller { // 界面 public function index() { $this->display('User/verify'); } // 驗證 public function check_verify() { $verify = new VerifyProcess(); if(!$verify->check($_POST['verify'])) { $this->error('驗證碼錯誤'); } } // 顯示驗證碼 public function verify() { $verify = new VerifyProcess(); $verify->entryProcess(); } }
verify.html:框架
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> </head> <body> <form action="{:U('Home/TestVerify/check_verify','','')}" method="post"> <table> <tr> <td>驗證碼:</td> <td><input type="text" name="verify"></td> <td> <img id="verify" src="{:U('Home/TestVerify/verify','','')}" style="cursor: pointer;" alt=""><a id="refresh" href="javascript:void(0)">更換驗證碼</a></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form> </body> <script> $(function(){ $src = $("#verify").attr('src'); $("#refresh").click(function(){ change_verify(); }); $("#verify").click(function(){ change_verify(); }); function change_verify() { $('#verify').attr('src', $src + '?' + Math.random()); } }); </script> </html>
效果:dom