Captcha.php
6.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace addons\captcha\library;
use addons\captcha\library\gif\GIFEncoder;
/*
* 验证码类库
* @Author: lovefc
* @Email:fcphp@qq.com
* @Date: 2019-09-27 14:35:05
* @Last Modified by: lovefc
* @Last Modified time: 2019-09-28 15:28:20
*/
class Captcha
{
// 验证码宽度
public $width = 200;
// 验证码高度
public $height = 60;
// 验证码个数
public $nums = 4;
// 随机字符串
public $random = '2345689zxcvbnmasdfhjkqwertyup';
// 随机数大小
public $font_size = 36;
// 字体路径
public $font_path = __DIR__.'/font/zhankukuhei.ttf';
// 是否为动态验证码
public $is_gif = true;
// 动图帧数
public $gif_fps = 10;
/**
* 获取验证码
*
* @return void
*/
public function getCode()
{
return $this->code = strtolower($this->setVerNumber());
}
/**
* 生成验证码
*
* @param integer $w 宽度
* @param integer $h 高度
* @param integer $nums 数量
* @param string $random 随机字符串
* @return void
*/
public function doImg($code = '')
{
if (!$code) {
return false;
}
$code = strtoupper($code);
$imagedata = [];
if ($this->is_gif) {
for ($i = 0; $i < $this->gif_fps; $i++) {
$imagedata[] = $this->creBackGIF($code);
++$i;
}
} else {
$imagedata[] = $this->creBackGIF($code);
}
$gif = new GIFEncoder($imagedata);
header('Content-type:image/gif');
echo $gif->GetAnimation();
exit;
}
/**
* 创建动态背景
*
* @return void
*/
private function creBackGIF($code)
{
ob_start();
$im = $this->createImageSource();
$this->setBackGroundColor($im);
$this->setCode($im, $code);
$this->setRandomCode($im);
imagegif($im);
imagedestroy($im);
$imagedata = ob_get_contents();
ob_clean();
return $imagedata;
}
/**
* 创建个画布
*
* @return void
*/
private function createImageSource()
{
return imagecreate($this->width, $this->height);
}
/**
* 设置背景颜色
*
* @param [type] $im
* @return void
*/
private function setBackGroundColor($im)
{
$bgcolor = imagecolorallocate($im, rand(200, 255), rand(200, 255), rand(200, 255));
imagefill($im, 0, 0, $bgcolor);
}
/**
* 加入随机数
*
* @param [type] $im
* @return string
*/
private function setRandomCode($im)
{
$count_h = $this->height;
$cou = floor($count_h * 1);
for ($i = 0; $i < $cou; $i++) {
$x = rand(0, $this->width);
$y = rand(0, $this->height);
$jiaodu = rand(0, 360); //设置角度
$fonturl = $this->font_path; //使用的字体
// 检测中文
if (preg_match("/[\x7f-\xff]/", $this->random)) {
$fontsize = $this->font_size / 4;
$dscode = $this->getChar(1);
} else {
$size = $this->font_size / 3.5;
$fontsize = rand($size, $size + 3);
$originalcode = 'zxcvbnmasdfghjklqwertyuiop1234567890'; //随机字符串
$countdistrub = mb_strlen($originalcode);
$dscode = $originalcode[rand(0, $countdistrub - 1)];
}
$color = imagecolorallocate($im, rand(40, 140), rand(40, 140), rand(40, 140));
imagettftext($im, $fontsize, $jiaodu, $x, $y, $color, $fonturl, $dscode);
}
}
/**
* 随机中文字符串
*
* @param [type] $num 返回数量
* @return string
*/
private function getChar($num)
{
$b = '';
for ($i = 0; $i < $num; $i++) {
// 使用chr()函数拼接双字节汉字,前一个chr()为高位字节,后一个为低位字节
$a = chr(mt_rand(0xB0, 0xD0)) . chr(mt_rand(0xA1, 0xF0));
// 转码
$b .= iconv('GB2312', 'UTF-8', $a);
}
return $b;
}
/**
* 画布上生成验证码
*
* @param [type] $im
* @param [type] $string
* @return void
*/
private function setCode($im, $string)
{
$width = $this->width;
$height = $this->height;
$len = 1;
$start = 0;
$strlen = $count = mb_strlen($string, 'utf-8');
while ($strlen) {
$array[] = mb_substr($string, $start, $len, "utf8");
$string = mb_substr($string, $len, $strlen, "utf8");
$strlen = mb_strlen($string);
}
$y = floor($height / 2) + floor($height / 4);
$fontsize = rand($this->font_size - 2, $this->font_size);
$fonturl = $this->font_path;
$counts = $count;
for ($i = 0; $i < $counts; $i++) {
$char = $array[$i];
$x = floor($width / $counts) * $i + ($width / 15);
$jiaodu = rand(-30, 30);
$color = imagecolorallocate($im, rand(0, 50), rand(50, 100), rand(100, 140));
imagettftext($im, $fontsize, $jiaodu, $x, $y, $color, $fonturl, $char);
}
}
/**
* 生成随机码,(支持中文)
*
* @return void
*/
private function setVerNumber()
{
$len = 1;
$start = 0;
$string = $this->random;
$strlen = $num = mb_strlen($string, 'utf-8');
while ($strlen) {
$array[] = mb_substr($string, $start, $len, "utf-8");
$string = mb_substr($string, $len, $strlen, "utf-8");
$strlen = mb_strlen($string, 'utf-8');
}
$originalcode = $array;
$_dscode = "";
$counts = $this->nums;
for ($j = 0; $j < $counts; $j++) {
$rand = rand(0, $num - 1);
$dscode = $originalcode[$rand];
$_dscode .= $dscode;
}
return $_dscode;
}
}