[PHP] php验证码类 →→→→→进入此内容的聊天室

来自 , 2020-09-29, 写在 PHP, 查看 106 次.
URL http://www.code666.cn/view/0172d289
  1. <?php
  2. /**
  3.  * 验证码类
  4.  */
  5. class imgCreate {
  6.  
  7.     private $charset='abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ23456789';//随机因子
  8.     private $code=""; //验证码
  9.     private $codelen; //验证码长度
  10.     private $width; //宽度
  11.     private $height; //高度
  12.     private $img; //图形资源句柄
  13.     private $font='/font/simhei.ttf'; //指定字体
  14.     private $fontsize; //指定字体大小
  15.    
  16.     //构造方法初始化
  17.     public function __construct($width='100',$height='30',$codelen='4',$fontsize='5') {
  18.         $this->width=$width;
  19.         $this->height=$height;
  20.         $this->codelen=$codelen;
  21.         $this->fontsize=$fontsize;
  22.     }
  23.    
  24.     //生成随机码
  25.     private function crecteCode(){
  26.         $_len= strlen($this->charset)-1;
  27.         for($i=0;$i<$this->codelen;$i++){
  28.             $this->code.=$this->charset[mt_rand(0, $_len)];
  29.         }
  30.                 return $this->code;
  31.     }
  32.  
  33.     //生成背景
  34.     private function createBg(){
  35.         $this->img=  imagecreatetruecolor($this->width, $this->height);
  36.         $Bgcolor=  imagecolorallocate($this->img, mt_rand(0, 150), mt_rand(50, 100), mt_rand(0, 150));
  37.         imagefill($this->img, 0, 0, $Bgcolor);
  38.     }
  39.    
  40.     //生成文字
  41.     private function createFont(){
  42.                 $fontcolor=imagecolorallocate($this->img, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));
  43.                 imagefttext ($this->img, $this->fontsize, mt_rand(-10,10), $this->width/6, $this->height/1.2, $fontcolor, $this->font, $this->crecteCode());
  44.     }
  45.    
  46.     //生成线条、雪花
  47.     private function createLine(){
  48.         for($i=0;$i<6;$i++){
  49.             $color=  imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  50.             imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
  51.         }
  52.         for($i=0;$i<20;$i++){
  53.             $color=  imagecolorallocate($this->img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
  54.             imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
  55.         }
  56.     }
  57.    
  58.     //输出
  59.     private function outPut(){
  60.         header('Content-type:image/png');
  61.         imagepng($this->img);
  62.         imagedestroy($this->img);
  63.     }
  64.    
  65.     //对外生成
  66.     public function doimg(){
  67.         $this->createBg();
  68.         $this->createLine();
  69.         $this->createFont();
  70.         $this->outPut();
  71.     }
  72.        
  73.         //获取验证码
  74.     public function getCode(){
  75.         echo $this->crecteCode();
  76.     }
  77.  
  78. }
  79. ?>

回复 "php验证码类"

这儿你可以回复上面这条便签

captcha