肥宅自学平台_人人成为技术开发者

肥宅人只等待您,联系我们吧,曾经的巅峰是否能回来?我们正在用心服务!

php如何生成验证码代码分享

 更新时间:2024-05-07 14:16:46   作者:肥宅-季波   我要评论(0)  

         利用PHP生成验证码需要用到GD2库。GD2库引用方法网络上有很多,不同操作系统导入方式也不同。
  1. <?php
  2. $iC = new idCode(5,60,30);
  3. $iC->createPNG();
  4. class idCode{
  5.   private $words = array('a','b',
  6.   'c','d','e','f','g','h','i','j','k','l',
  7.   'm','n','o','p','q','r','s','t','u','v',
  8.   'w','x','y','z','A','B','C','D','E','F',
  9.   'G','H','I','J','K','L','M','N','O','P',
  10.   'Q','R','S','T','U','V','W','X','Y','Z',
  11.   '0','1','2','3','4','5','6','7','8','9');
  12.   private $fonts;
  13.   private $count;//验证码字符数
  14.   private $height;
  15.   private $width;
  16.   private $path = '..\myfolder\fonts';
  17.   private $keys;
  18.   //构造函数
  19.   public function __construct($count,$width,$height){
  20.     $this->count = $count;
  21.     $this->getFonts();
  22.     $this->height = $height;
  23.     $this->width = $width;
  24.   }
  25.   private function getFonts(){
  26.     $dir = dir($this->path);
  27.     while(false !== ($file = $dir->read())){
  28.         if($file != '.' && $file != '..'){
  29.           $this->fonts[count($this->fonts)] = basename($file);
  30.         }
  31.     }
  32.     $dir->close();
  33.   }
  34.   private function createKeys(){
  35.     for($i = 0;$i < $this->count;$i++){
  36.       $this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)];
  37.       //使用字体路径标识
  38.       $this->keys[$i]['filename'] = $this->path.'\\'.$this->fonts[rand(0,count($this->fonts)-1)];
  39.     }
  40.   }
  41.   public function createPNG(){
  42.     $this->createKeys();
  43.     //创建画布以及颜色块儿
  44.     $bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//两边留10px空白,上下3px
  45.     $grey = imagecolorallocate($bg,155,155,155);
  46.     $blue = imagecolorallocate($bg,0x00,0x00,0xff);
  47.     //填充背景
  48.     imagefill($bg,0,0,$grey);
  49.     //添加字符
  50.     $pwidth = $this->width/$this->count;
  51.     $x;$y;
  52.     for($i = 0;$i < $this->count;$i++){
  53.       $rotation = rand(-40,40);//偏转角度±40°
  54.       $fontsize = 33;
  55.       $width_txt;
  56.       $height_txt;
  57.       do{
  58.         $fontsize--;
  59.         $bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']);
  60.         $width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上
  61.         $height_txt = $bbox[7] - $bbox[1];
  62.       }while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth));
  63.       $fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255));
  64.       $x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x坐标基本位置
  65.       $y = $this->height/2 - $height_txt/2;
  66.       imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']);
  67.     }
  68.     //绘制干扰线
  69.     //根据字体酌情增加干扰线
  70.     imageline($bg,0,15,40,10,$blue);
  71.     //图像输出头文件
  72.     header('Content-type:image/png');
  73.     //输出png图像
  74.     imagepng($bg);
  75.     //清除缓存资源
  76.     imagedestroy($bg);
  77.   }
  78.   public function checkKeys($input){
  79.     if(count($input)!=$this->count){
  80.       return 'ERROR:长度不正确.';
  81.     }else{
  82.       for($i=0;$i < $this->count;$i++){
  83.         //0 o O I l 1 校准,根据所选择的字体确定是否需要手动校准
  84.         if($input[$i] != $this->keys[$i]['char']){
  85.           return 'SUCCESS.';
  86.         }else{
  87.           return 'ERROR:请输入正确验证码.';
  88.         }
  89.       }
  90.     }
  91.   }
  92. }
  93. ?>
"小礼物走一走,来肥宅自学平台支持我"
评论区

评论

共条评论
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~
客服中心在线客服
全心全意为平台用户服务
Copyright © 2017-2023 自学平台网站地图:去查看>
  • 肥宅人自己的自学平台
  • 人人都能成为平台讲师
  • 贡献你的力量壮大肥宅自学平台
  • 将自己的能力变现
  • 实现人生的第二职业
网址收藏平台安卓APP
微信公众号微信公众号

本站部分图片或者资源来自程序自动采集或卖家(商家)发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!

肥宅自学教程网是一个主打IT视频教程、自媒体运行、摄影剪辑等内容的资源学习交流平台。