侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

ThinkPHP 3.2.3实现加减乘除图片验证码

ThinkPHP  /  管理员 发布于 7年前   505

ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Verify/

可以在 Verify.class.php 文件内进行修改,也可以单独写一个类继承自带的验证码类。如果单独写一个继承的类,可以重用父类的属性和方法,但是要注意的是父类中有一些属性和方法是私有(private)的,可以修改这些私有的属性和方法为保护(protected)的,如果不希望修改框架自带的方法的话,也可以在子类中再定义这些属性和方法。

测试的控制器位于 /Application/Home/Controller/TestVerifyController.class.php

测试的试图位于 /Application/Home/View/User/verify.html

自定义的子类位于 /Applicaion/Home/Common/VerifyProcess.class.php 

VerifyProcess.class.php:

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π/OωO) * */ 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:

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:

  Document  
验证码: 更换验证码
  

效果:

也可以点击图片更换验证码,只需要把点击事件换到图片上就行了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持,关注公众号的更多精彩内容。

您可能感兴趣的文章:

  • 完美解决thinkphp验证码出错无法显示的方法
  • ThinkPHP验证码使用简明教程
  • thinkphp验证码显示不出来的解决方法
  • ThinkPHP打开验证码页面显示乱码的解决方法
  • thinkPHP中验证码的简单使用方法
  • thinkphp3.2点击刷新生成验证码
  • thinkphp验证码的实现(form、ajax实现验证)
  • 详解ThinkPHP3.2.3验证码显示、刷新、校验
  • thinkPHP实现的验证码登录功能示例
  • thinkphp自带验证码全面解析


  • 上一条:
    swoole和websocket简单聊天室开发
    下一条:
    详解PHP swoole process的使用方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-12
    • 2018-01
    • 2018-02
    • 2020-03
    • 2021-07
    • 2021-12
    • 2022-05
    • 2022-06
    • 2022-09
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客