佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > ThinkPHP 3.2.3實現(xiàn)加減乘除圖片驗證碼

ThinkPHP 3.2.3實現(xiàn)加減乘除圖片驗證碼

熱門標簽:銀行業(yè)務 阿里云 電子圍欄 Linux服務器 服務器配置 團購網(wǎng)站 Mysql連接數(shù)設置 科大訊飛語音識別系統(tǒng)

ThinkPHP 3.2.3 自帶的驗證碼類位于 /ThinkPHP/Library/Think/Verify.class.php,字體文件位于 /ThinkPHP/Library/Think/Verify/

可以在 Verify.class.php 文件內(nèi)進行修改,也可以單獨寫一個類繼承自帶的驗證碼類。如果單獨寫一個繼承的類,可以重用父類的屬性和方法,但是要注意的是父類中有一些屬性和方法是私有(private)的,可以修改這些私有的屬性和方法為保護(protected)的,如果不希望修改框架自帶的方法的話,也可以在子類中再定義這些屬性和方法。

測試的控制器位于 /Application/Home/Controller/TestVerifyController.class.php

測試的試圖位于 /Application/Home/View/User/verify.html

自定義的子類位于 /Applicaion/Home/Common/VerifyProcess.class.php 

VerifyProcess.class.php:

?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; // 驗證碼創(chuàng)建時間
 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);
 }
 
 /**
 * 畫雜點
 * 往圖片上寫不同顏色的字母或數(shù)字
 */
 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);
   }
  }
 }
 
 /**
 * 畫一條由兩條連在一起構(gòu)成的隨機正弦函數(shù)曲線作干擾線(你可以改成更帥的曲線函數(shù))
 *  
 *  高中的數(shù)學公式咋都忘了涅,寫出來
 *  正弦型函數(shù)解析式:y=Asin(ωx+φ)+b
 *  各常數(shù)值對函數(shù)圖像的影響:
 *  A:決定峰值(即縱向拉伸壓縮的倍數(shù))
 *  b:表示波形在Y軸的位置關(guān)系或縱向移動距離(上加下減)
 *  φ:決定波形與X軸位置關(guān)系或橫向移動距離(左加右減)
 *  ω:決定周期(最小正周期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); // 曲線橫坐標結(jié)束位置
 
 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)循環(huán)畫像素點比imagettftext和imagestring用字體大小一次畫出
    (不用這while循環(huán))性能要好很多   
    $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);
 } 
 
 /**
 * 繪制背景圖片
 * 注:如果驗證碼輸出圖片比較大,將占用比較多的系統(tǒng)資源
 */
 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:

?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)" rel="external nofollow" >更換驗證碼/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>  

效果:

也可以點擊圖片更換驗證碼,只需要把點擊事件換到圖片上就行了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家,關(guān)注腳本之家公眾號的更多精彩內(nèi)容。

您可能感興趣的文章:
  • 用php簡單實現(xiàn)加減乘除計算器
  • php的chr和ord函數(shù)實現(xiàn)字符加減乘除運算實現(xiàn)代碼
  • PHP實現(xiàn)加減乘除最簡單的實例分享

標簽:廣元 衡水 蚌埠 萍鄉(xiāng) 棗莊 衢州 江蘇 大理

巨人網(wǎng)絡通訊聲明:本文標題《ThinkPHP 3.2.3實現(xiàn)加減乘除圖片驗證碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    无极县| 凉城县| 宜兰市| 防城港市| 永善县| 五寨县| 招远市| 来安县| 得荣县| 六盘水市| 黄龙县| 吉林市| 宁晋县| 山阴县| 麻江县| 嵊泗县| 新丰县| 南靖县| 景谷| 新宁县| 灯塔市| 涞水县| 蓬莱市| 呼和浩特市| 张家川| 江门市| 海阳市| 湘乡市| 金阳县| 隆昌县| 民权县| 赤水市| 炉霍县| 天峻县| 凭祥市| 建阳市| 越西县| 张北县| 远安县| 九龙坡区| 应用必备|