当前位置:首页 > PHP教程 > php类库 > 列表

个人写的PHP验证码生成类分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-07 16:20:07 浏览: 评论:0 

这篇文章主要介绍了个人写的PHP验证码生成类分享,此验证码类直接拿去就可以用,也可以用来学习和分析,需要的朋友可以参考下。

此验证码类直接拿去就可以用,也可以参考!

其中类成员codestr是生成的验证码字符串:

  1. <?php 
  2. /** 
  3.  * 验证码 
  4.  */ 
  5. class Code{ 
  6.    
  7.   // 1. 定义各个成员 有宽、高、画布、字数、类型、画类型 
  8.      
  9.   private $width//宽度 
  10.   private $height//高度 
  11.   private $num//验证码字数 
  12.   private $imgType//生成图片类型 
  13.   private $Type//字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合 
  14.   private $hb//画布 
  15.   public $codestr// 验证码字串 
  16.    
  17.   public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){ 
  18.     $this->width = $num*20; 
  19.     $this->height = $height
  20.     $this->num = $num
  21.     $this->imgType = $imgType;   
  22.     $this->Type = $Type;  
  23.     $this->codestr = $this->codestr(); 
  24.     $this->zuhe(); 
  25.   } 
  26.    
  27.   // 2. 定义随机获取字符串函数 
  28.   private function codestr(){ 
  29.     switch($this->Type){ 
  30.        
  31.       case 1:   // 类型为1 获取1-9随机数 
  32.         $str = implode("",array_rand(range(0,9),$this->num)); 
  33.         break
  34.       case 2:   // 类型为2 获取a-z随机小写字母 
  35.         $str = implode("",array_rand(array_flip(range(a,z)),$this->num)); 
  36.         break
  37.       case 3:   // 类型为3 获取数字,小写字母,大写字母 混合 
  38.         for($i=0;$i<$this->num;$i++){ 
  39.           $m = rand(0,2); 
  40.           switch($m){ 
  41.             case 0: 
  42.               $o = rand(48,57); 
  43.               break
  44.             case 1: 
  45.               $o = rand(65,90); 
  46.               break
  47.             case 2: 
  48.               $o = rand(97,122); 
  49.               break;  
  50.           } 
  51.           $str .= sprintf("%c",$o); 
  52.         } 
  53.         break;      
  54.     } 
  55.    
  56.        
  57.     return $str;   
  58.   } 
  59.    
  60.    
  61.   // 3. 初始化画布图像资源 
  62.   private function Hb(){ 
  63.     $this->hb = imagecreatetruecolor($this->width,$this->height);  
  64.   } 
  65.    
  66.   // 4. 生成背景颜色 
  67.   private function Bg(){ 
  68.     return imagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));  
  69.   } 
  70.    
  71.   // 5. 生成字体颜色 
  72.   private function Font(){ 
  73.     return imagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));   
  74.   } 
  75.    
  76.   // 6. 填充背景颜色 
  77.   private function BgColor(){ 
  78.     imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());  
  79.   } 
  80.    
  81.   // 7. 干扰点 
  82.   private function ganrao(){ 
  83.     $sum=floor(($this->width)*($this->height)/3); 
  84.     for($i=0;$i<$sum;$i++){ 
  85.       imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());   
  86.     } 
  87.   } 
  88.    
  89.   // 8. 随机直线 弧线 
  90.   private function huxian(){ 
  91.     for($i=0;$i<$this->num;$i++){ 
  92.       imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());     
  93.     }   
  94.   } 
  95.    
  96.   // 9. 写字 
  97.   private function xiezi(){ 
  98.     for($i=0;$i<$this->num;$i++){ 
  99.       $x=ceil($this->width/$this->num)*$i;  
  100.       $y=rand(1,$this->height-15); 
  101.       imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font()); 
  102.     }   
  103.   } 
  104.    
  105.   // 10. 输出 
  106.   private function OutImg(){ 
  107.     $shuchu="image".$this->imgType;  
  108.     $header="Content-type:image/".$this->imgType; 
  109.     if(function_exists($shuchu)){ 
  110.       header($header); 
  111.       $shuchu($this->hb);  
  112.     }else
  113.       exit("GD库没有此类图像");  
  114.     } 
  115.   } 
  116.    
  117.   // 11. 拼装 
  118.   private function zuhe(){ 
  119.     $this->Hb(); 
  120.     $this->BgColor(); 
  121.     $this->ganrao(); 
  122.     $this->huxian(); 
  123.     $this->xiezi(); 
  124.     $this->OutImg();  
  125.   }   
  126.    
  127.   public function getCodeStr(){ 
  128.     return $this->codestr;     
  129.   } 
  130. ?> 

Tags: PHP验证码生成类

分享到: