欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年11月16日 09:43:08   作者:Huibe  
這篇文章主要為大家詳細(xì)介紹了ThinkPHP 3.2.3實(shí)現(xiàn)加減乘除圖片驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

測(cè)試的試圖位于 /Application/Home/View/User/verify.html

自定義的子類(lèi)位于 /Applicaion/Home/Common/VerifyProcess.class.php?

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);
 }
 
 /**
 * 畫(huà)雜點(diǎn)
 * 往圖片上寫(xiě)不同顏色的字母或數(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);
   }
  }
 }
 
 /**
 * 畫(huà)一條由兩條連在一起構(gòu)成的隨機(jī)正弦函數(shù)曲線作干擾線(你可以改成更帥的曲線函數(shù))
 *  
 *  高中的數(shù)學(xué)公式咋都忘了涅,寫(xiě)出來(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)畫(huà)像素點(diǎn)比imagettftext和imagestring用字體大小一次畫(huà)出
    (不用這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>  

效果:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家,關(guān)注腳本之家公眾號(hào)的更多精彩內(nèi)容。

相關(guān)文章

  • laravel yajra插件 datatable的使用詳解

    laravel yajra插件 datatable的使用詳解

    這篇文章主要介紹了laravel yajra插件 datatable的使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • PHP導(dǎo)出帶樣式的Excel示例代碼

    PHP導(dǎo)出帶樣式的Excel示例代碼

    相信大家在工作的時(shí)候有客戶(hù)會(huì)向你抱怨,軟件為他們導(dǎo)出的Excel格式太難看了,這個(gè)時(shí)候我們就需要到處自定義樣式的Excel了,那么或許這篇文章會(huì)對(duì)你有所幫助,有需要的可以參考借鑒。
    2016-08-08
  • 8個(gè)必備的PHP功能實(shí)例代碼

    8個(gè)必備的PHP功能實(shí)例代碼

    本文將分享8個(gè)開(kāi)發(fā)必備的PHP功能,個(gè)個(gè)都非常實(shí)用:傳遞任意數(shù)量的函數(shù)參數(shù) 、使用glob()查找文件、獲取內(nèi)存使用情況信息、獲取CPU使用情況信息 、獲取系統(tǒng)常量 、生成唯一的id 、序列化 、字符串壓縮。很實(shí)用的8個(gè)PHP功能。
    2013-10-10
  • 微博短鏈接算法php版本實(shí)現(xiàn)代碼

    微博短鏈接算法php版本實(shí)現(xiàn)代碼

    將長(zhǎng)網(wǎng)址md5生成32位簽名串,分為4段, 每段8個(gè)字節(jié)
    2012-09-09
  • Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法

    Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法

    這篇文章主要介紹了Codeigniter通過(guò)SimpleXML將xml轉(zhuǎn)換成對(duì)象的方法,涉及Codeigniter操作XML文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • PHP中使用FFMPEG獲取視頻縮略圖和視頻總時(shí)長(zhǎng)實(shí)例

    PHP中使用FFMPEG獲取視頻縮略圖和視頻總時(shí)長(zhǎng)實(shí)例

    這篇文章主要介紹了PHP中使用FFMPEG獲取視頻縮略圖和視頻總時(shí)長(zhǎng)實(shí)例,需要的朋友可以參考下
    2014-05-05
  • PHP 數(shù)組黑名單/白名單實(shí)例代碼詳解

    PHP 數(shù)組黑名單/白名單實(shí)例代碼詳解

    這篇文章主要介紹了PHP 數(shù)組黑名單/白名單,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • ThinkPHP 框架實(shí)現(xiàn)的讀取excel導(dǎo)入數(shù)據(jù)庫(kù)操作示例

    ThinkPHP 框架實(shí)現(xiàn)的讀取excel導(dǎo)入數(shù)據(jù)庫(kù)操作示例

    這篇文章主要介紹了ThinkPHP 框架實(shí)現(xiàn)的讀取excel導(dǎo)入數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式分析了thinkPHP針對(duì)Excel文件的讀取、解析以及數(shù)據(jù)庫(kù)的寫(xiě)入相關(guān)操作技巧,需要的朋友可以參考下
    2020-04-04
  • 基于curl數(shù)據(jù)采集之單頁(yè)面采集函數(shù)get_html的使用

    基于curl數(shù)據(jù)采集之單頁(yè)面采集函數(shù)get_html的使用

    在做數(shù)據(jù)采集時(shí)經(jīng)常要使用到curl+正則的方式采集需要的數(shù)據(jù) 根據(jù)自己的工作經(jīng)驗(yàn) 把自己寫(xiě)的一些常用自定義函數(shù) 與大家來(lái)分享 如果有寫(xiě)得不恰當(dāng)?shù)牡胤?請(qǐng)多多指教
    2013-04-04
  • PHP單鏈表的實(shí)現(xiàn)代碼

    PHP單鏈表的實(shí)現(xiàn)代碼

    單鏈表是一種鏈?zhǔn)酱嫒〉臄?shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲(chǔ)單元存放線性表中的數(shù)據(jù)元素。接下來(lái)通過(guò)本文給大家分享php單鏈表的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧
    2016-07-07

最新評(píng)論