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

php封装的验证码类分享

php  /  管理员 发布于 7年前   148

验证码是我们开发的时候经常用到的功能,所以在此本人包装了一个验证码类,应该可以作为php的类插件用,在此分享给各位读友。

  实现的原理也是很简单,就是利用画布的几个函数,再加上一些字符串的获取,东凑西凑就构成了,呵呵。

  这里大概写一下思路吧,其实这个类已经注释的非常清楚了,不过,个人还是在行文前乱幌隆

  首先是关于一些函数的解释,这里的解释纯属个人体会,有什么错误的地方,还请读者指正。

  1、创建画布函数:imagecreatetruecolor(w,h);

    说明:用于创建一个画布。

    w 画布的宽

    h 画布的高

    此函数的返回值资源类(gd)

  2、为画布创建一种颜色:imagecolorallocate(img,red,green,blue)

    说明:

    img画布资源

    red,green,blue  是0~255的范围

  3、为画布添加背景色

    imagefill(img,x,y,color);

    说明:

    在 image 图像的坐标 x,y(图像左上角为 0, 0)

  4、画边框

    imagerectangle($img,x1,y1,x2,y2,color);

    说明:

    其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。

  3、绘制内容(字符)

    imagestring(img ,size,x,y,string,color);

    说明:

    img画布

    size是字大小 1至5

    x,y是起始点

    string是所要画的内容

    color是颜色

  4、告诉浏览器图片格式

    header("Content-type:image/png");可为image/gif等等

  5、输出(或保存),也可以使用第2个参数实现保存

    imagepng(img【,filename】)

    imagejpeg(img【,filename】)

    imagegif(img【,filename】)

  6、添加干扰线,本质就是直线

    imageline(img,x1,y1,x2,y2,color);

    说明:

      img 画布

      x1,y1 起点

      x2,y2 终点

      color 颜色

  7、imagettftext ( img,size, angle, x, y, color, fontfile,text )

      说明:

        img 画布

        size 字体大小,缺省单位像素

        angle 角度

        x,y 坐标点

        color 颜色

        fontfile 字体文件,必须是中文字体

        text 内容

  特别说明:这里的color参数都是imagecolorallocate()函数创建的颜色

  下面是思路:

  这里最先生成画布,之后就是为画布添加字符串,直线,噪点,边框,来生成验证码的,最后类返回的两个公用接口是:可供外面调用的生成验证码的画布和验证码的字符串构成,为的是给外界输出验证码画布,以及存储字符串,作为验证用

  下面是代码:

100,'height'=>40,'length'=>4,'size'=>7,'lines'=>0,'dots'=>0,'font'=>'simfang.ttf','rectangle'=>array(255,55,122),'charset'=>true,'chinese'=>'来到新机场主航站楼建设在婚姻关系存续期间所负债务她在收到要求她偿还前夫在婚姻关系存续期间所欠债务的法院传票后要精益求精善始善终')){    $this->config = $config;  }  //创建验证码  private function captchaImage(){    //画布    $img = imagecreatetruecolor($this->config['width'],$this->config['height']);    //填充画布颜色    imagefill($img,0,0,imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));    //需要边框则画边框    if($this->config['rectangle'] && is_array($this->config['rectangle']) && count($this->config['rectangle']) == 3){      $this->tangle($img);    }    $this->verifyCode = $this->code($img,$this->config['charset'],$this->config['chinese']);    //存在则添加干扰线    if($this->config['lines']){      $this->codeLines($img);    }    //存在则添加干扰点    if($this->config['dots']){      $this->codeDots($img);    }    return $img;  }  private function codeLines($img){    //绘制干扰线    for($i=0;$i<$this->config['lines'];$i++){      imageline($img,mt_rand(0,$this->config['width'] / 10),mt_rand(0,$this->config['height']),mt_rand($this->config['width'] * 7/ 10,$this->config['width'] * 9/ 10),mt_rand(0,$this->config['height']),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));    }  }  private function codeDots($img){    //添加噪点    for($i=0;$i<$this->config['dots'];$i++){      //噪点颜色      $color = imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180));      imagestring($img,mt_rand(1,3),mt_rand(0,170),mt_rand(0,30),'*',$color);        }  }  /*画布边框*/  private function tangle($img){    imagerectangle($img,0,0,$this->config['width']-1,$this->config['height']-1,imagecolorallocate($img,$this->config['rectangle'][0],$this->config['rectangle'][1],$this->config['rectangle'][2]));  }  /*生成验证码,默认英文,$ch为true则为中文*/  private function code($img,$ch=false,$set=''){    $str = "";    //计算间隔    $span = ceil($this->config['width']/($this->config['length']+1));    if($ch && !empty($set)){      //随机产生字符      $set = $this->config['chinese'];      for($i=0;$i<$this->config['length'];$i++){        $end = strlen($set)/3;        $pos = mt_rand(0,$end-1);        $str .= substr($set,$pos*3,3);      }      //每次绘制一个字符      for($i=1;$i<=$this->config['length'];$i++){        imagettftext($img,16,mt_rand(-30,60),$i*$span,$this->config['height']*3/5,imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)),$this->config['font'],substr($str,($i-1)*3,3));      }    }else{      //随机生成字母或者数字      for($i=0;$i<$this->config['length'];$i++){        switch(mt_rand(0,2)){          case 0:          $str .= chr(mt_rand(65,90));          break;        case 1:          $str .= chr(mt_rand(97,122));          break;        case 2:          $str .= chr(mt_rand(48,57));        }      }      //每次绘制一个字符      for($i=1;$i<=$this->config['length'];$i++){        imagestring($img,$this->config['size'],$i*$span,0,$str[$i-1],imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)));      }    }    return $str;  }  //获取验证码  public function verify(){    return $this->verifyCode;  }  //生成验证码  public function entry(){    header("content-type:image/png");    imagepng($this->captchaImage());  }}$ob = new Captcha;$ob->entry();

最后,为了不误人子弟,还是再强调一遍:
这里必须先用entry生成验证码,再用verify生成验证码的字符串,也就是必须先调用entry,然后才能够调用verify生成验证码的字符串,原因代码已经说明问题了,因为验证码的字符串是在entry方法的方法captchaImage中生成的,所以必须先调用它才行 有的地方对中文的字体要求比较高,所以,有的地方不支持中文验证码

您可能感兴趣的文章:

  • 一个漂亮的php验证码类(分享)
  • PHP验证码类代码( 最新修改,完全定制化! )
  • 一个经典的PHP验证码类分享
  • 一个好用的PHP验证码类实例分享
  • PHP 动态随机生成验证码类代码
  • 一个PHP验证码类代码分享(已封装成类)
  • PHP学习笔记 用户注册模块用户类以及验证码类
  • php实现的Captcha验证码类实例


  • 上一条:
    浅谈PHP的反射API
    下一条:
    解决PHP 7编译安装错误:cannot stat ‘phar.phar’: No such file or directory
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(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
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

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

    侯体宗的博客