导航首页 » 技术教程 » php封装的验证码类分享
php封装的验证码类分享 137 2024-01-15   

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

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

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

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

  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()函数创建的颜色

  下面是思路:

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

  下面是代码:

<?php
namespace captcha;
/*
*验证码类
*verify方法生成验证码字符串
*entry方法生成验证码
*特别提醒:这里要先用entry生成验证码,再用verify生成验证码的字符串,也就是必须先调用entry,然后才能够调用verify生成验证码的字符串,原因代码已经说明问题了,因为验证码的字符串是在entry方法调用captchaImage生成的,所以必须先调用它才行
*有的地方对中文的字体要求比较高,所以,有的地方不支持中文验证码
*/
class Captcha{
  //配置参数
  private $config = array();
  //验证码
  private $verifyCode = '';
  //获取配置文件的配置信息,给类传参数就行,例如new Captcha($config);$config是你的配置文件信息
  public function __construct($config=array('width'=>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中生成的,所以必须先调用它才行 有的地方对中文的字体要求比较高,所以,有的地方不支持中文验证码



!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103