最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)證碼類位于 /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)的,如果不希望修改框架自帶的方法的話,也可以在子類中再定義這些屬性和方法。

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

測(cè)試的試圖位于 /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;  // 驗(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>  

效果:

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

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

相關(guān)文章

  • Yii2中添加全局函數(shù)的方法分析

    Yii2中添加全局函數(shù)的方法分析

    這篇文章主要介紹了Yii2中添加全局函數(shù)的方法,結(jié)合實(shí)例形式對(duì)比分析了2種添加全局函數(shù)的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-05-05
  • Laravel中七個(gè)非常有用但很少人知道的Carbon方法

    Laravel中七個(gè)非常有用但很少人知道的Carbon方法

    在編寫PHP應(yīng)用時(shí)經(jīng)常需要處理日期和時(shí)間,Carbon繼承自 PHP DateTime 類的 API 擴(kuò)展,它使得處理日期和時(shí)間更加簡(jiǎn)單,這篇文章主要給大家分享了Laravel中七個(gè)非常有用但很少人知道的Carbon方法,需要的朋友可以參考下。
    2017-09-09
  • PHP中使用pthread拓展

    PHP中使用pthread拓展

    這篇文章主要介紹了PHP中使用pthread拓展,本文講述線程類的使用方法和線程類的概念和使用場(chǎng)景,以及具體的代碼實(shí)現(xiàn),以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • smarty高級(jí)特性之對(duì)象的使用方法

    smarty高級(jí)特性之對(duì)象的使用方法

    這篇文章主要介紹了smarty高級(jí)特性之對(duì)象的使用方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了使用類與對(duì)象的相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • PHP判斷字符串長(zhǎng)度的兩種方法很實(shí)用

    PHP判斷字符串長(zhǎng)度的兩種方法很實(shí)用

    這篇文章主要介紹了PHP判斷字符串長(zhǎng)度的相關(guān)資料,需要的朋友可以參考下
    2015-09-09
  • 基于Laravel實(shí)現(xiàn)的用戶動(dòng)態(tài)模塊開發(fā)

    基于Laravel實(shí)現(xiàn)的用戶動(dòng)態(tài)模塊開發(fā)

    這篇文章主要給大家介紹了關(guān)于基于Laravel實(shí)現(xiàn)的用戶動(dòng)態(tài)模塊開發(fā)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • 解決laravel查詢構(gòu)造器中的別名問(wèn)題

    解決laravel查詢構(gòu)造器中的別名問(wèn)題

    今天小編就為大家分享一篇解決laravel查詢構(gòu)造器中的別名問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • 基于php+mysql的期末作業(yè)小項(xiàng)目(學(xué)生信息管理系統(tǒng))

    基于php+mysql的期末作業(yè)小項(xiàng)目(學(xué)生信息管理系統(tǒng))

    最近自己寫的一個(gè)簡(jiǎn)單的php期末作業(yè)項(xiàng)目,分享給大家,下面這篇文章主要給大家介紹了關(guān)于基于php+mysql的期末作業(yè)小項(xiàng)目,一個(gè)學(xué)生信息管理系統(tǒng),需要的朋友可以參考下
    2023-01-01
  • laravel執(zhí)行php artisan migrate報(bào)錯(cuò)的解決方法

    laravel執(zhí)行php artisan migrate報(bào)錯(cuò)的解決方法

    今天小編就為大家分享一篇laravel執(zhí)行php artisan migrate報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • Apache PHP MySql安裝配置圖文教程

    Apache PHP MySql安裝配置圖文教程

    這篇文章主要以圖文結(jié)合的方式為大家詳細(xì)介紹了Apache PHP MySql安裝配置教程,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評(píng)論

府谷县| 吉木萨尔县| 饶平县| 南阳市| 九台市| 新巴尔虎右旗| 濮阳县| 龙南县| 五台县| 遂平县| 铜山县| 喜德县| 黄石市| 峨眉山市| 茌平县| 皋兰县| 镇远县| 宁海县| 武山县| 沁阳市| 龙山县| 彰化市| 蚌埠市| 巴彦县| 平乐县| 离岛区| 师宗县| 沙雅县| 沧源| 淄博市| 遵义县| 凯里市| 瑞金市| 呼玛县| 吉安县| 奎屯市| 安宁市| 上思县| 古浪县| 漠河县| 四会市|