欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂PHP培訓(xùn)分享ThinkPHP3.2.3實(shí)現(xiàn)加減乘除圖片驗(yàn)證碼的方法

2019-04-15 13:54:59 3080瀏覽

本篇文章扣丁學(xué)堂PHP培訓(xùn)給小伙伴們分享一下ThinkPHP3.2.3實(shí)現(xiàn)加減乘除圖片驗(yàn)證碼的方法,文中有詳細(xì)的代碼列出供讀者們參考學(xué)習(xí),希望對(duì)小伙伴們有所幫助。



扣丁學(xué)堂PHP培訓(xùn)分享ThinkPHP3.2.3實(shí)現(xiàn)加減乘除圖片驗(yàn)證碼的方法



ThinkPHP3.2.3 自帶的驗(yàn)證碼類位于 /ThinkPHP/Library/Think/Verify.class.php,字體文件位于 /ThinkPHP/Library/Think/Verify/


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


VerifyProcess.class.php:


<?php
 
namespace Home\Common;
use Think\Verify;
 
class VerifyProcess extends Verify {
 
 private $_image = NULL;  // 驗(yàn)證碼圖片實(shí)例
 private $_color = NULL;  // 驗(yàn)證碼字體顏色
 
 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);
 
 // 設(shè)置背景  
 imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]);
 
 // 驗(yàn)證碼字體隨機(jī)顏色
 $this->_color = imagecolorallocate($this->_image, mt_rand(1,150), 
 mt_rand(1,150), mt_rand(1,150));
 // 驗(yàn)證碼使用隨機(jī)字體
 $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) {
  // 繪雜點(diǎn)
  $this->_writeNoise();
 }
 if ($this->useCurve) {
  // 繪干擾線
  $this->_writeCurve();
 }
  
 // 繪驗(yàn)證碼
 $codeNX = 0; // 驗(yàn)證碼第N個(gè)字符的左邊距
 
 // 驗(yàn)證碼為簡(jiǎn)單運(yù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]);
 }
 
 // 保存驗(yàn)證碼
 $key  = $this->authcode($this->seKey);
 $result  = $this->authcode($result);
 $secode  = array();
 $secode['verify_code'] = $result; // 把校驗(yàn)碼保存到session
 $secode['verify_time'] = NOW_TIME; // 驗(yàn)證碼創(chuàng)建時(shí)間
 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);
 }
 
 /**
 * 畫雜點(diǎn)
 * 往圖片上寫不同顏色的字母或數(shù)字
 */
 private function _writeNoise() {
  $codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
  for($i = 0; $i < 10; $i++){
   //雜點(diǎn)顏色
   $noiseColor = imagecolorallocate($this->_image, mt_rand(150,225), 
   mt_rand(150,225), mt_rand(150,225));
   for($j = 0; $j < 5; $j++) {
    // 繪雜點(diǎn)
    imagestring($this->_image, 5, mt_rand(-10, $this->imageW), 
   mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
   }
  }
 }
 
 /**
 * 畫一條由兩條連在一起構(gòu)成的隨機(jī)正弦函數(shù)曲線作干擾線(你可以改成更帥的曲線函數(shù))
 *  
 *  高中的數(shù)學(xué)公式咋都忘了涅,寫出來(lái)
 *  正弦型函數(shù)解析式:y=Asin(ωx+φ)+b
 *  各常數(shù)值對(duì)函數(shù)圖像的影響:
 *  A:決定峰值(即縱向拉伸壓縮的倍數(shù))
 *  b:表示波形在Y軸的位置關(guān)系或縱向移動(dòng)距離(上加下減)
 *  φ:決定波形與X軸位置關(guān)系或橫向移動(dòng)距離(左加右減)
 *  ω:決定周期(最小正周期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; // 曲線橫坐標(biāo)起始位置
 $px2 = mt_rand($this->imageW/2, $this->imageW * 0.8); // 曲線橫坐標(biāo)結(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)畫像素點(diǎ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--;
   }
  }
 }
 }
 
 /* 加密驗(yàn)證碼 */
 private function authcode($str){
 $key = substr(md5($this->seKey), 5, 8);
 $str = substr(md5($str), 8, 10);
 return md5($key . $str);
 } 
 
 /**
 * 繪制背景圖片
 * 注:如果驗(yàn)證碼輸出圖片比較大,將占用比較多的系統(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');
 }
 
 // 驗(yàn)證
 public function check_verify() {
   
  $verify = new VerifyProcess();
 if(!$verify->check($_POST['verify'])) {
  $this->error('驗(yàn)證碼錯(cuò)誤');
 }
 }
 
 // 顯示驗(yàn)證碼
 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>驗(yàn)證碼:</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" >更換驗(yàn)證碼</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> 


效果:

PHP學(xué)習(xí)


也可以點(diǎn)擊圖片更換驗(yàn)證碼,只需要把點(diǎn)擊事件換到圖片上就行了。



想要了解更多關(guān)于PHP開(kāi)發(fā)方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的PHP講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開(kāi)發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂PHP技術(shù)交流群:374332265。


扣丁學(xué)堂微信公眾號(hào)                              HTML5前端開(kāi)發(fā)VIP免費(fèi)公開(kāi)課

     【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】           【掃碼進(jìn)入HTML5前端開(kāi)發(fā)VIP免費(fèi)公開(kāi)課



查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>


標(biāo)簽: PHP培訓(xùn) PHP基礎(chǔ)教程 PHP學(xué)習(xí)視頻 PHP教學(xué)視頻 PHP入門教程 PHP教程視頻 PHP在線學(xué)習(xí) PHP在線視頻 PHP在線教程 扣丁學(xué)堂PHP培訓(xùn)

熱門專區(qū)

暫無(wú)熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部