PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,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,color);

    说明:

      img 画布

      x1,y1 起点

      x2,y2 终点

      color 颜色

  7、imagettftext ( img,angle,color,fontfile,text )

      说明:

        img 画布

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

        angle 角度

        x,y 坐标点

        color 颜色

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

        text 内容

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

  下面是思路:

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

  下面是代码

PHP;">
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,imagecolorallocate($img,mt_rand(200,255),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),$this->config['height']),mt_rand($this->config['width'] * 7/ 10,$this->config['width'] * 9/ 10),255)));
    }
  }
  private function codeDots($img){
    //添加噪点
    for($i=0;$i<$this->config['dots'];$i++){
      //噪点颜色
      $color = imagecolorallocate($img,180),180));
      imageString($img,mt_rand(1,3),170),30),'*',$color);
}@H_675_135@

}
/画布边框/
private function tangle($img){
imagerectangle($img,$this->config['width']-1,$this->config['height']-1,$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,$pos3,3);
}
//每次绘制一个字符
for($i=1;$i<=$this->config['length'];$i++){
imagettftext($img,16,mt_rand(-30,60),$i
$span,$this->config['height']3/5,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(R_383_11845@t_rand(65,90));
break;
case 1:
$str .= chr(R_383_11845@t_rand(97,122));
break;
case 2:
$str .= chr(R_383_11845@t_rand(48,57));
}
}
//每次绘制一个字符
for($i=1;$i<=$this->config['length'];$i++){
imageString($img,$this->config['size'],$str[$i-1],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();

大佬总结

以上是大佬教程为你收集整理的php封装的验证码类分享全部内容,希望文章能够帮你解决php封装的验证码类分享所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。