生成数字验证码的功能

把该文件  http://pan.baidu.com/s/1dFnO6nZ  放入www目录,然后新建一个test.php文件,复制以下代码,可以进行测试

 

  1 <?php
  2 /**
  3  * 简单的图片验证码
  4  */
  5 class PictureCaptcha
  6 {
  7     /**
  8      * @var int 图片像素宽度
  9      */
 10     protected $imageWidth;
 11 
 12     /**
 13      * @var int 图片像素高度
 14      */
 15     protected $imageHeight;
 16 
 17 
 18     /**
 19      * @var resource 图片句柄
 20      */
 21     protected $img;
 22 
 23 
 24     /**
 25      * @var string 字符集
 26      */
 27     protected $font;
 28 
 29     /**
 30      * @var string 字体文件路径
 31      */
 32     protected $fontDir;
 33 
 34     /**
 35      * @var int 字符尺寸大小
 36      */
 37     protected $fontSize;
 38 
 39     /**
 40      * @var string 验证码
 41      */
 42     protected $code;
 43 
 44     /**
 45      * @var int 验证码字符数
 46      */
 47     protected $codeCount;
 48 
 49     /**
 50      * @var array 颜色数组
 51      */
 52     protected $colorArr;
 53 
 54     /**
 55      * @var int 图片中线的数量
 56      */
 57     protected $lineCount;
 58 
 59     /**
 60      * @var int 图片中点的数量
 61      */
 62     protected $pixCount;
 63     /**
 64      * @var int 颜色的数量
 65      */
 66     protected $colorCount;
 67 
 68     /**
 69      * @var array 颜色值
 70      */
 71     protected $colorStyle;
 72 
 73     /**
 74      * @var array 配置参数
 75      */
 76     protected $config;
 77 
 78     /**
 79      * 生成图片验证码需GD库支持
 80      */
 81     public function __construct()
 82     {
 83         if (!function_exists('imagecreate')) {
 84             throw new \RuntimeException('GD extension is not exist!');
 85         }
 86 
 87         $this->init();
 88     }
 89 
 90 
 91     public function init()
 92     {
 93         $this->config = $this->getConfig();
 94 
 95         $this->font = isset($this->config['font']) ? $this->config['font'] : '0123456789abcdefghijklmnopqrstuvwxyz0123456789';  //想要纯数字, 这边可以替换成0123456789
 96         $this->fontDir = dirname(__FILE__) . '/' . (isset($this->config['fontFile']) ? $this->config['fontFile'] : 'BELL.TTF');
 97         $this->imageWidth = isset($this->config['width']) ? $this->config['width'] : 100;
 98         $this->imageHeight = isset($this->config['height']) ? $this->config['height'] : 30;
 99         $this->codeCount = isset($this->config['codeCount']) ? $this->config['codeCount'] : 4;
100         $this->fontSize = isset($this->config['fontSize']) ? $this->config['fontSize'] : 30;
101         $this->lineCount = isset($this->config['lineCount']) ? $this->config['lineCount'] : 2;
102         $this->pixCount = isset($this->config['pixCount']) ? $this->config['pixCount'] : 200;
103         $this->colorCount = isset($this->config['colorCount']) ? $this->config['colorCount'] : 1;
104         $this->colorStyle = isset($this->config['colorStyle']) ? $this->config['colorStyle'] : [];
105 
106     }
107 
108 
109     /**
110      * 载入配置参数
111      * @return array
112      */
113     public function getConfig()
114     {
115         $fileDir = dirname(__FILE__) . '/config.php';
116 
117         if (file_exists($fileDir)) {
118             return require $fileDir;
119         }
120 
121         return [];
122     }
123 
124     /**
125      * 返回创建好的图像句柄,
126      * 还需要进行头信息的设置与销毁
127      *
128      * @return resource $this->img
129      */
130     public function create()
131     {
132         $this->createImage();
133         $this->createColor($this->colorCount);
134         $this->createPix($this->pixCount);
135         $this->createLine($this->lineCount);
136         $this->createText();
137 
138         return $this->img;
139     }
140 
141 
142     /**
143      * 创建字符所用颜色
144      * 可以指定颜色的个数
145      * 默认为4个颜色
146      * @param int $colorCount
147      * @return array
148      */
149     protected function createColor($colorCount = 0)
150     {
151         $this->colorArr = [];
152 
153         // 第一次调用该函数则 为画布创建背景色
154         imagecolorallocate($this->img, 255, 255, 255);
155 
156         if ($colorCount > 0) {
157 
158             $i = 0;
159 
160             $styleIndex = 0;
161             $styleCount = count($this->colorStyle) - 1;
162 
163 
164             while ($i < $colorCount){
165 
166                 if (isset($this->colorStyle[$styleIndex])) {
167                     $color1 = $this->colorStyle[$styleIndex][0];
168                     $color2 = $this->colorStyle[$styleIndex][1];
169                     $color3 = $this->colorStyle[$styleIndex][2];
170 
171                     // 当设置的颜色不够时,循环赋值
172                     if ($styleIndex == $styleCount) {
173                         $styleIndex = 0;
174                     } else {
175                         $styleIndex++;
176                     }
177                 } else {
178                     $color1 = rand(0, 255);
179                     $color2 = rand(0, 255);
180                     $color3 = rand(0, 255);
181                 }
182 
183                 $this->colorArr[] = imagecolorallocate($this->img, $color1, $color2, $color3);
184                 $i++;
185             }
186 
187             return $this->colorArr;
188         }
189 
190         $this->colorArr[] = imagecolorallocate($this->img, 0, 0, 0);
191         $this->colorArr[] = imagecolorallocate($this->img, 0, 0, 0);
192         $this->colorArr[] = imagecolorallocate($this->img, 0, 0, 0);
193         $this->colorArr[] = imagecolorallocate($this->img, 0, 0, 0);
194 
195         return $this->colorArr;
196     }
197 
198 
199     /**
200      * 创建基础画布
201      */
202     protected function createImage()
203     {
204         $this->img = imagecreate($this->imageWidth,$this->imageHeight);
205     }
206 
207     /**
208      * 在画布上生成若干点
209      * (颜色随机,位置随机)
210      * @param int $pixCount 生成点的数量
211      */
212     protected function createPix($pixCount = 400)
213     {
214         $i = 0;
215         $colorIndex = count($this->colorArr) - 1;
216 
217         while ($i < $pixCount) {
218             imagesetpixel($this->img, rand(0,$this->imageWidth), rand(0,$this->imageHeight), $this->colorArr[rand(0,$colorIndex)]);
219             $i++;
220         }
221     }
222 
223 
224     /**
225      * 画线
226      * @param int $lineCount 线的数量
227      */
228     protected function createLine($lineCount = 2)
229     {
230         $i = 0;
231         imagesetstyle($this->img ,$this->colorArr);
232 
233         while ($i < $lineCount) {
234             imageline($this->img, 0, rand(0, $this->imageHeight), $this->imageWidth, rand(0, $this->imageHeight), IMG_COLOR_STYLED);
235             $i++;
236         }
237     }
238 
239     /**
240      * 设置图片中文字
241      */
242     protected function createText()
243     {
244         $i = 0;
245         $fontIndex = strlen($this->font) - 1;
246         $colorIndex = count($this->colorArr) - 1;
247         $codes = '';
248         $x = 0;
249 
250         while ($i < $this->codeCount) {
251 
252             $code = $this->font{rand(0, $fontIndex)};
253             $color = $this->colorArr[rand(0, $colorIndex)];
254 
255             imagettftext($this->img, $this->fontSize, 0, $x, rand($this->fontSize - 10, $this->fontSize * 2 - $this->imageHeight), $color, $this->fontDir, $code);
256             $x += intval($this->imageWidth / $this->codeCount);
257 
258             $i++;
259             $codes .= $code;
260         }
261         $this->code = $codes;
262     }
263 
264     public function getCode()
265     {
266         return $this->code;
267     }
268 
269 }
270 
271 $pictureCaptcha = new PictureCaptcha();
272 
273 $image = $pictureCaptcha->create();
274 $code = $pictureCaptcha->getCode();
275 
276 $imageFile = 'D:\123' .  '.png';
277 
278 imagepng($image, $imageFile);
279 imagedestroy($image);//显示和销毁是必需的
280 
281 $base64Content = base64_encode(file_get_contents($imageFile));//base64编码
282 
283 unlink($imageFile);  //本地测试的时候可以注释, 可以看见图片内容, 正式环境需要unlink,因为返回了base64码,会形成图片,图片占资源
284 
285 $base64 = 'data:image/png;base64,'.$base64Content;
286 
287 print <<<EOT
288     <img src=$base64 />
289 EOT;
290 
291 // 图片转换Base64在线工具[http://imgbase64.duoshitong.com]
292 ?>

 

posted @ 2017-11-15 14:58  spectrelb  阅读(794)  评论(0编辑  收藏  举报