Rumah > pembangunan bahagian belakang > tutorial php > php封装的验证码类详解

php封装的验证码类详解

墨辰丷
Lepaskan: 2023-03-27 22:20:02
asal
1910 orang telah melayarinya

本文给大家分享的是一个php封装的验证码类的代码和原理及思路,非常的清晰详细,有需要的小伙伴可以参考下

验证码是我们开发的时候经常用到的功能,所以在此本人包装了一个验证码类,应该可以作为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()函数创建的颜色

  下面是思路:

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

  下面是代码:

<?php
namespace captcha;
/*
*验证码类
*verify方法生成验证码字符串
*entry方法生成验证码
*特别提醒:这里要先用entry生成验证码,再用verify生成验证码的字符串,也就是必须先调用entry,然后才能够调用verify生成验证码的字符串,原因代码已经说明问题了,因为验证码的字符串是在entry方法调用captchaImage生成的,所以必须先调用它才行
*有的地方对中文的字体要求比较高,所以,有的地方不支持中文验证码
*/
class Captcha{
  //配置参数
  private $config = array();
  //验证码
  private $verifyCode = &#39;&#39;;
  //获取配置文件的配置信息,给类传参数就行,例如new Captcha($config);$config是你的配置文件信息
  public function __construct($config=array(&#39;width&#39;=>100,&#39;height&#39;=>40,&#39;length&#39;=>4,&#39;size&#39;=>7,&#39;lines&#39;=>0,&#39;dots&#39;=>0,&#39;font&#39;=>&#39;simfang.ttf&#39;,&#39;rectangle&#39;=>array(255,55,122),&#39;charset&#39;=>true,&#39;chinese&#39;=>&#39;来到新机场主航站楼建设在婚姻关系存续期间所负债务她在收到要求她偿还前夫在婚姻关系存续期间所欠债务的法院传票后要精益求精善始善终&#39;)){
    $this->config = $config;
  }
  //创建验证码
  private function captchaImage(){
    //画布
    $img = imagecreatetruecolor($this->config[&#39;width&#39;],$this->config[&#39;height&#39;]);
    //填充画布颜色
    imagefill($img,0,0,imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
    //需要边框则画边框
    if($this->config[&#39;rectangle&#39;] && is_array($this->config[&#39;rectangle&#39;]) && count($this->config[&#39;rectangle&#39;]) == 3){
      $this->tangle($img);
    }
    $this->verifyCode = $this->code($img,$this->config[&#39;charset&#39;],$this->config[&#39;chinese&#39;]);
    //存在则添加干扰线
    if($this->config[&#39;lines&#39;]){
      $this->codeLines($img);
    }
    //存在则添加干扰点
    if($this->config[&#39;dots&#39;]){
      $this->codeDots($img);
    }
    return $img;
  }
  private function codeLines($img){
    //绘制干扰线
    for($i=0;$i<$this->config[&#39;lines&#39;];$i++){
      imageline($img,mt_rand(0,$this->config[&#39;width&#39;] / 10),mt_rand(0,$this->config[&#39;height&#39;]),mt_rand($this->config[&#39;width&#39;] * 7/ 10,$this->config[&#39;width&#39;] * 9/ 10),mt_rand(0,$this->config[&#39;height&#39;]),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
    }
  }
  private function codeDots($img){
    //添加噪点
    for($i=0;$i<$this->config[&#39;dots&#39;];$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),&#39;*&#39;,$color);
    
    }
  }
  /*画布边框*/
  private function tangle($img){
    imagerectangle($img,0,0,$this->config[&#39;width&#39;]-1,$this->config[&#39;height&#39;]-1,imagecolorallocate($img,$this->config[&#39;rectangle&#39;][0],$this->config[&#39;rectangle&#39;][1],$this->config[&#39;rectangle&#39;][2]));
  }
  /*生成验证码,默认英文,$ch为true则为中文*/
  private function code($img,$ch=false,$set=&#39;&#39;){
    $str = "";
    //计算间隔
    $span = ceil($this->config[&#39;width&#39;]/($this->config[&#39;length&#39;]+1));
    if($ch && !empty($set)){
      //随机产生字符
      $set = $this->config[&#39;chinese&#39;];
      for($i=0;$i<$this->config[&#39;length&#39;];$i++){
        $end = strlen($set)/3;
        $pos = mt_rand(0,$end-1);
        $str .= substr($set,$pos*3,3);
      }
      //每次绘制一个字符
      for($i=1;$i<=$this->config[&#39;length&#39;];$i++){
        imagettftext($img,16,mt_rand(-30,60),$i*$span,$this->config[&#39;height&#39;]*3/5,imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)),$this->config[&#39;font&#39;],substr($str,($i-1)*3,3));
      }
    }else{
      //随机生成字母或者数字
      for($i=0;$i<$this->config[&#39;length&#39;];$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[&#39;length&#39;];$i++){
        imagestring($img,$this->config[&#39;size&#39;],$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();
Salin selepas log masuk

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

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

JavaScript+Regex实现身份证号正则验证步奏详解

php实现封装的验证码类

php传值方式和ajax实现验证功能的方法

Atas ialah kandungan terperinci php封装的验证码类详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan