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

PHP code 验证码生成类定义和简单使用示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-10 08:46:58 浏览: 评论:0 

这篇文章主要介绍了PHP code 验证码生成类定义和简单使用,结合实例形式分析了PHP code 验证码生成类的基本功能定义、简单使用方法及操作注意事项,需要的朋友可以参考下。

本文实例讲述了PHP code 验证码生成类定义和简单使用,分享给大家供大家参考,具体如下:

code.php

  1. <?php 
  2. namespace code; 
  3. /** 
  4.  * Class Code 
  5.  */ 
  6. class Code 
  7. { 
  8.   protected $number;//验证码内字符个数 
  9.   protected $codeType;//验证码样式 
  10.   protected $width;//图像宽 
  11.   protected $height;//图像高 
  12.   protected $code;//验证码 
  13.   protected $image;//图像资源 
  14.    
  15.   /** 
  16.    * Code constructor. 
  17.    * @param int $number 
  18.    * @param int $codeType 
  19.    * @param int $width 
  20.    * @param int $height 
  21.    */ 
  22.   public function __construct($number=5, $codeType=2, $width=100, $height=40) 
  23.   { 
  24.     $this->number = $number; 
  25.     $this->codeType = $codeType; 
  26.     $this->width = $width; 
  27.     $this->height = $height; 
  28.     $this->code = $this->createCode(); 
  29.   } 
  30.    
  31.   /** 
  32.    * 销毁资源 
  33.    */ 
  34.   public function __destruct() 
  35.   { 
  36.     imagedestroy($this->image); 
  37.   } 
  38.    
  39.   /** 
  40.    * 外部调用code时触发 
  41.    * @param $name 
  42.    * @return bool 
  43.    */ 
  44.   public function __get($name) 
  45.   { 
  46.     if ('code' == $name) { 
  47.       return $this->$name; 
  48.     } else { 
  49.       return false; 
  50.     } 
  51.   } 
  52.    
  53.   /** 
  54.    * 生成code 
  55.    */ 
  56.   protected function createCode() 
  57.   { 
  58.     switch ($this->codeType) { 
  59.       case 0: 
  60.         $code = $this->getNum(); 
  61.         break; 
  62.       case 1: 
  63.         $code = $this->getChar(); 
  64.         break; 
  65.       case 2: 
  66.         $code = $this->getNumChar(); 
  67.         break; 
  68.       default: 
  69.         die('样式不对'); 
  70.     } 
  71.     return $code; 
  72.   } 
  73.    
  74.   /** 
  75.    * 数字验证码 
  76.    * @return string 
  77.    */ 
  78.   protected function getNum() 
  79.   { 
  80.     $str = join('', range(0,9)); 
  81.     return substr(str_shuffle($str), 0, $this->number); 
  82.   } 
  83.    
  84.   /** 
  85.    * 字符验证码 
  86.    * @return string 
  87.    */ 
  88.   protected function getChar() 
  89.   { 
  90.     $str = join('', range('a', 'z')); 
  91.     $str = $str . strtoupper($str); 
  92.     return substr(str_shuffle($str), 0, $this->number); 
  93.   } 
  94.    
  95.   /** 
  96.    * 字符和数字混合验证码 
  97.    * @return string 
  98.    */ 
  99.   protected function getNumChar() 
  100.   { 
  101.     $num = join('', range(0, 9)); 
  102.     $str = join('', range('a', 'z')); 
  103.     $str_big = strtoupper($str); 
  104.     $numChar = $num . $str . $str_big; 
  105.     return substr(str_shuffle($numChar), 0, $this->number); 
  106.   } 
  107.    
  108.   /** 
  109.    * 生成图像 
  110.    */ 
  111.   protected function createImage() 
  112.   { 
  113.     $this->image = imagecreatetruecolor($this->width, $this->height); 
  114.   } 
  115.    
  116.   /** 
  117.    * 填充背景色 
  118.    */ 
  119.   protected function fillColor() 
  120.   { 
  121.     imagefill($this->image, 0, 0, $this->lightColor()); 
  122.   } 
  123.    
  124.   /** 
  125.    * 浅颜色 
  126.    * @return int 
  127.    */ 
  128.   protected function lightColor() 
  129.   { 
  130.     return imagecolorallocate($this->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255)); 
  131.   } 
  132.    
  133.   /** 
  134.    * 深颜色 
  135.    * @return int 
  136.    */ 
  137.   protected function darkColor() 
  138.   { 
  139.     return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); 
  140.   } 
  141.    
  142.   /** 
  143.    * 添加验证码字符 
  144.    */ 
  145.   protected function drawChar() 
  146.   { 
  147.     $width = ceil($this->width/$this->number); 
  148.     for ($i = 0; $i < $this->number; $i++) { 
  149.       $x = mt_rand($i * ($width - 5), ($i + 1) * ($width - 5)); 
  150.       $y = mt_rand(0, $this->height - 15); 
  151.       imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor()); 
  152.     } 
  153.   } 
  154.    
  155.   /** 
  156.    * 添加干扰点 
  157.    */ 
  158.   protected function drawDisturb() 
  159.   { 
  160.     for ($i= 0; $i < 100; $i++) { 
  161.       imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor()); 
  162.     } 
  163.   } 
  164.    
  165.   /** 
  166.    * 添加干扰线 
  167.    */ 
  168.   protected function drawArc() 
  169.   { 
  170.     for ($i = 0; $i < $this->number - 3; $i++) { 
  171.       imagearc($this->image, mt_rand(5, $this->width), mt_rand(5, $this->height), mt_rand(5, $this->width), mt_rand(5, $this->height),mt_rand(0, 70), mt_rand(300, 360), $this->darkColor()); 
  172.     } 
  173.   } 
  174.    
  175.   /** 
  176.    * 输出显示 
  177.    */ 
  178.   protected function show() 
  179.   { 
  180.     header('Content-Type:image/png'); 
  181.     imagepng($this->image); 
  182.   } 
  183.    
  184.   /** 
  185.    * 外部image 
  186.    */ 
  187.   public function outImage() 
  188.   { 
  189.     $this->createImage();//创建画布 
  190.     $this->fillColor();//填充背景色 
  191.     $this->drawChar();//添加验证字符 
  192.     $this->drawDisturb();//添加干扰点 
  193.     $this->drawArc();//添加干扰线 
  194.     $this->show();//输出 
  195.   } 
  196. } 

展示验证码,保存验证码和过期时间:

  1. <?php 
  2. include './code/Code.php'; 
  3.    
  4. $code = new code\Code(); 
  5. $code->outImage(); 
  6. session_start(); 
  7. $_SESSION['code'] = [ 
  8.   'code' => $code->code, 
  9.   'exp_time' => time() + (60 * 60 * 10), 
  10. ];

Tags: PHP验证码生成类

分享到:

相关文章