4-5验证码类封装

1.Captcha.php

  1 <?php
  2 /**
  3  * Captcha.php
  4  
  5  * description 验证码类
  6  */
  7 
  8 namespace ImoocLib;
  9 
 10 require_once 'GDBasic.php';
 11 
 12 class Captcha extends GDBasic
 13 {
 14     //图像宽度
 15     protected $_width = 60;
 16     //图像高度
 17     protected $_height = 25;
 18 
 19     //随机串
 20     protected $_code = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjklmnpqrstuvwxyz';
 21 
 22     //字体文件
 23     protected $_font_file = './font/comicz.ttf';
 24 
 25     //图像
 26     protected $_im;
 27     //验证码
 28     protected $_captcha;
 29 
 30     public function __construct($width = null, $height = null)
 31     {
 32         self::check();
 33         $this->create($width, $height);
 34     }
 35 
 36     /**
 37      * 创建图像
 38      * @param $width
 39      * @param $height
 40      */
 41     public function create($width, $height)
 42     {
 43         $this->_width = is_numeric($width) ? $width : $this->_width;
 44         $this->_height = is_numeric($height) ? $height : $this->_height;
 45         //创建图像
 46         $im = imagecreatetruecolor($this->_width, $this->_height);
 47         $back = imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
 48         //填充底色
 49         imagefill($im, 0, 0, $back);
 50         $this->_im = $im;
 51     }
 52 
 53     /**
 54      * 混乱验证码
 55      */
 56     public function moll()
 57     {
 58         $back = imagecolorallocate($this->_im, 0, 0, 0);
 59         //在图像中随机生成50个点
 60         for($i = 0; $i < 50; $i++)
 61         {
 62             imagesetpixel($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
 63         }
 64 
 65         imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
 66 
 67         imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
 68     }
 69 
 70 
 71     /**
 72      * 生成验证码随机串
 73      * @param int $length 验证码的个数
 74      * @param int $fontSize 字符串的字体大小
 75      * @return Captcha
 76      */
 77     public function string($length = 4, $fontSize = 15)
 78     {
 79         $this->moll();
 80         $code = $this->_code;
 81         $captcha = '';
 82         for($i = 0; $i < $length; $i++)
 83         {
 84             $string = $code[mt_rand(0, strlen($code) - 1)];
 85             $strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150));
 86             imagettftext($this->_im, $fontSize, mt_rand(-10, 10), mt_rand(3, 6) + $i * (($this->_width - 10) / $length), ($this->_height / 3) * 2, $strColor, $this->_font_file, $string);
 87             $captcha .= $string;
 88         }
 89 
 90         $this->_captcha = $captcha;
 91         return $this;
 92     }
 93 
 94 
 95     /**
 96      * 验证码存入session
 97      */
 98     public function setSession()
 99     {
100         if(!isset($_SESSION))
101         {
102             session_start();
103         }
104         $_SESSION['captcha_code'] = $this->_captcha;
105     }
106 
107     /**
108      * 逻辑运算符验证码
109      * @param int $fontSize 字体大小
110      * @return $this
111      */
112     public function logic($fontSize = 12)
113     {
114         $this->moll();
115         $codeArray = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9);
116         $operatorArray = array('+' => '+', '-' => '-', 'x' => '*');
117         list($first, $second) = array_rand($codeArray, 2);
118         $operator = array_rand($operatorArray);
119         $captcha = 0;
120         $string = '';
121         switch($operator)
122         {
123             case '+':
124                 $captcha = $first + $second;
125                 break;
126             case '-':
127                 //当第一个数小于第二个数
128                 if($first < $second)
129                 {
130                     list($first, $second) = array($second, $first);
131                 }
132                 $captcha = $first - $second;
133                 break;
134 
135             case 'x':
136                 $captcha = $first * $second;
137                 break;
138         }
139         //设置验证码类变量
140         $this->_captcha = $captcha;
141         //要输出到图像中的字符串
142         $string = sprintf('%s%s%s=?', $first, $operator, $second);
143 
144         $strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150));
145         imagettftext($this->_im, $fontSize, 0, 5, ($this->_height / 3) * 2, $strColor, $this->_font_file, $string);
146 
147         return $this;
148     }
149 
150 
151     /**
152      * 输出验证码
153      */
154     public function show()
155     {
156         //生成session
157         $this->setSession();
158         header('Content-Type:image/jpeg');
159         imagejpeg($this->_im);
160         imagedestroy($this->_im);
161     }
162 }

调用

1 require_once './lib/Captcha.php';
2 
3 $captcha = new ImoocLibCaptcha(80,30);
4 
5 $captcha->string(ii6,14)->show();
6 $captcha->logic(12)->show();
原文地址:https://www.cnblogs.com/kay-learning/p/8973230.html