1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vicens\Captcha; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Session; |
6
|
|
|
|
7
|
|
|
class Captcha |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* 存储在session中的key |
11
|
|
|
*/ |
12
|
|
|
const SESSION_NAME = '_captcha'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* 验证码配置 |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $config = [ |
19
|
|
|
/** |
20
|
|
|
* 调试模型 |
21
|
|
|
*/ |
22
|
|
|
'debug' => false, |
23
|
|
|
/** |
24
|
|
|
* 默认验证码长度 |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
'length' => 4, |
28
|
|
|
/** |
29
|
|
|
* 验证码字符集 |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
'charset' => 'abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789', |
33
|
|
|
/** |
34
|
|
|
* 是否开启严格模式(区分大小写) |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
'strict' => false, |
38
|
|
|
/** |
39
|
|
|
* 默认验证码宽度 |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
'width' => 150, |
43
|
|
|
/** |
44
|
|
|
* 默认验证码高度 |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
'height' => 40, |
48
|
|
|
/** |
49
|
|
|
* 指定文字颜色 |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
'textColor' => null, |
53
|
|
|
/** |
54
|
|
|
* 文字字体文件 |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
'textFont' => null, |
58
|
|
|
/** |
59
|
|
|
* 指定图片背景色 |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
'backgroundColor' => null, |
63
|
|
|
/** |
64
|
|
|
* 开启失真模式 |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
'distortion' => true, |
68
|
|
|
/** |
69
|
|
|
* 最大前景线条数 |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
'maxFrontLines' => null, |
73
|
|
|
/** |
74
|
|
|
* 最大背景线条数 |
75
|
|
|
* @val int |
76
|
|
|
*/ |
77
|
|
|
'maxBehindLines' => null, |
78
|
|
|
/** |
79
|
|
|
* 文字最大角度 |
80
|
|
|
* @var int |
81
|
|
|
*/ |
82
|
|
|
'maxAngle' => 8, |
83
|
|
|
/** |
84
|
|
|
* 文字最大偏移量 |
85
|
|
|
* @var int |
86
|
|
|
*/ |
87
|
|
|
'maxOffset' => 5 |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Captcha constructor. |
92
|
|
|
* @param array $config |
93
|
|
|
*/ |
94
|
|
|
public function __construct(array $config = []) |
95
|
|
|
{ |
96
|
|
|
$this->setConfig($config); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* 设置验证码配置 |
101
|
|
|
* |
102
|
|
|
* @param array|string $config 配置数组或配置项key |
103
|
|
|
* @param mixed $value 配置项值 |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setConfig($config, $value = null) |
107
|
|
|
{ |
108
|
|
|
|
109
|
|
|
if (!is_array($config)) { |
110
|
|
|
$config = [$config => $value]; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($config as $key => $value) { |
114
|
|
|
if (array_key_exists($key, $this->config)) { |
115
|
|
|
$this->config[$key] = $value; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 获取配置 |
124
|
|
|
* |
125
|
|
|
* @param string|null $key 配置项key |
126
|
|
|
* @return string|number|array |
127
|
|
|
*/ |
128
|
|
|
public function getConfig($key = null) |
129
|
|
|
{ |
130
|
|
|
if ($key !== null) { |
131
|
|
|
return $this->config[$key]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->config; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* 生成验证码 |
139
|
|
|
* |
140
|
|
|
* @return Image |
141
|
|
|
*/ |
142
|
|
|
public function make() |
143
|
|
|
{ |
144
|
|
|
$code = $this->generate(); |
145
|
|
|
|
146
|
|
|
$hash = password_hash($code, PASSWORD_BCRYPT, array('cost' => 10)); |
147
|
|
|
|
148
|
|
|
if ($hash === false) { |
149
|
|
|
throw new \RuntimeException('Bcrypt hashing not supported.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
Session::put(self::SESSION_NAME, $hash); |
153
|
|
|
|
154
|
|
|
return new Image($this->build($code)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* 仅测试正确性, 不删除验证码 |
159
|
|
|
* |
160
|
|
|
* @param string $input |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function test($input) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
if ($this->config['debug']) { |
166
|
|
|
return true; |
167
|
|
|
} elseif (!(Session::has(self::SESSION_NAME) && $input)) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$code = Session::get(self::SESSION_NAME); |
172
|
|
|
|
173
|
|
|
if ($this->config['strict']) { |
174
|
|
|
// 开启严格模式, 区分大小写 |
175
|
|
|
return password_verify($input, $code); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
//返回验证结果 |
179
|
|
|
return password_verify(strtolower($input), strtolower($code)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* 检测正确性,并删除验证码 |
184
|
|
|
* |
185
|
|
|
* @param string $input |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function check($input) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$result = $this->test($input); |
191
|
|
|
Session::forget(self::SESSION_NAME); |
192
|
|
|
|
193
|
|
|
return $result; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* 生成验证码 |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
protected function generate() |
202
|
|
|
{ |
203
|
|
|
$characters = str_split($this->getConfig('charset')); |
204
|
|
|
$length = $this->getConfig('length'); |
|
|
|
|
205
|
|
|
|
206
|
|
|
$code = ''; |
207
|
|
|
for ($i = 0; $i < $length; $i++) { |
208
|
|
|
$code .= $characters[rand(0, count($characters) - 1)]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $code; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* 创建验证码图片 |
216
|
|
|
* |
217
|
|
|
* @param string $code |
218
|
|
|
* @return resource |
219
|
|
|
*/ |
220
|
|
|
protected function build($code) |
221
|
|
|
{ |
222
|
|
|
|
223
|
|
|
// 图片宽 |
224
|
|
|
$width = $this->getConfig('width'); |
225
|
|
|
// 图片高 |
226
|
|
|
$height = $this->getConfig('height'); |
227
|
|
|
// 背景颜色 |
228
|
|
|
$backgroundColor = $this->getConfig('backgroundColor'); |
229
|
|
|
|
230
|
|
|
// 随机取一个字体 |
231
|
|
|
$font = $this->getTextFont(); |
232
|
|
|
|
233
|
|
|
// 根据宽高创建一个背景画布 |
234
|
|
|
$image = imagecreatetruecolor($width, $height); |
235
|
|
|
|
236
|
|
|
if ($backgroundColor === null) { |
237
|
|
|
$backgroundColor = imagecolorallocate($image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); |
238
|
|
|
} else { |
239
|
|
|
$color = $backgroundColor; |
|
|
|
|
240
|
|
|
$backgroundColor = imagecolorallocate($image, $color[0], $color[1], $color[2]); |
241
|
|
|
} |
242
|
|
|
// 填充背景色 |
243
|
|
|
imagefill($image, 0, 0, $backgroundColor); |
244
|
|
|
|
245
|
|
|
// 绘制背景干扰线 |
246
|
|
|
$this->drawLines($image, $this->getConfig('maxBehindLines')); |
247
|
|
|
|
248
|
|
|
// 写入验证码文字 |
249
|
|
|
$color = $this->renderText($image, $code, $font); |
250
|
|
|
|
251
|
|
|
// 绘制前景干扰线 |
252
|
|
|
$this->drawLines($image, $this->getConfig('maxFrontLines'), $color); |
253
|
|
|
|
254
|
|
|
if ($this->getConfig('distortion')) { |
255
|
|
|
// 创建失真 |
256
|
|
|
$image = $this->createDistortion($image, $width, $height, $backgroundColor); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
//如果不指定字体颜色和背景颜色,则使用图像过滤器修饰 |
260
|
|
|
if (function_exists('imagefilter') && is_null($backgroundColor) && is_null($this->getConfig('textColor'))) { |
261
|
|
|
// 颜色翻转 - 1/2几率 |
262
|
|
|
if (mt_rand(0, 1) == 0) { |
263
|
|
|
imagefilter($image, IMG_FILTER_NEGATE); |
264
|
|
|
} |
265
|
|
|
// 用边缘检测来突出图像的边缘 - 1/11几率 |
266
|
|
|
if (mt_rand(0, 10) == 0) { |
267
|
|
|
imagefilter($image, IMG_FILTER_EDGEDETECT); |
268
|
|
|
} |
269
|
|
|
// 改变图像的对比度 |
270
|
|
|
imagefilter($image, IMG_FILTER_CONTRAST, mt_rand(-50, 10)); |
271
|
|
|
|
272
|
|
|
if (mt_rand(0, 5) == 0) { |
273
|
|
|
// 用高斯算法和指定颜色模糊图像 |
274
|
|
|
imagefilter($image, IMG_FILTER_COLORIZE, mt_rand(-80, 50), mt_rand(-80, 50), mt_rand(-80, 50)); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
return $image; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* 创建失真 |
282
|
|
|
* |
283
|
|
|
* @param resource $image |
284
|
|
|
* @param int $width |
285
|
|
|
* @param int $height |
286
|
|
|
* @param int $backgroundColor |
287
|
|
|
* @return resource |
288
|
|
|
*/ |
289
|
|
|
protected function createDistortion($image, $width, $height, $backgroundColor) |
290
|
|
|
{ |
291
|
|
|
//创建失真 |
292
|
|
|
$contents = imagecreatetruecolor($width, $height); |
293
|
|
|
$rWidth = mt_rand(0, $width); |
|
|
|
|
294
|
|
|
$rHeight = mt_rand(0, $height); |
|
|
|
|
295
|
|
|
$phase = mt_rand(0, 10); |
|
|
|
|
296
|
|
|
$scale = 1.1 + mt_rand(0, 10000) / 30000; |
|
|
|
|
297
|
|
|
|
298
|
|
|
for ($x = 0; $x < $width; $x++) { |
299
|
|
|
for ($y = 0; $y < $height; $y++) { |
300
|
|
|
$vX = $x - $rWidth; |
|
|
|
|
301
|
|
|
$vY = $y - $rHeight; |
|
|
|
|
302
|
|
|
$vN = sqrt($vX * $vX + $vY * $vY); |
|
|
|
|
303
|
|
|
|
304
|
|
|
if ($vN != 0) { |
305
|
|
|
$vN2 = $vN + 4 * sin($vN / 30); |
306
|
|
|
$nX = $rWidth + ($vX * $vN2 / $vN); |
|
|
|
|
307
|
|
|
$nY = $rHeight + ($vY * $vN2 / $vN); |
|
|
|
|
308
|
|
|
} else { |
309
|
|
|
$nX = $rWidth; |
310
|
|
|
$nY = $rHeight; |
311
|
|
|
} |
312
|
|
|
$nY = $nY + $scale * sin($phase + $nX * 0.2); |
313
|
|
|
|
314
|
|
|
$pixel = $this->getColor($image, round($nX), round($nY), $backgroundColor); |
315
|
|
|
|
316
|
|
|
if ($pixel == 0) { |
317
|
|
|
$pixel = $backgroundColor; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
imagesetpixel($contents, $x, $y, $pixel); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return $contents; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* 获取一个字体 |
329
|
|
|
* |
330
|
|
|
* @return string |
|
|
|
|
331
|
|
|
*/ |
332
|
|
|
protected function getTextFont() |
333
|
|
|
{ |
334
|
|
|
// 指定字体 |
335
|
|
|
if ($this->getConfig('textFont') && file_exists($this->getConfig('textFont'))) { |
336
|
|
|
return $this->getConfig('textFont'); |
337
|
|
|
} |
338
|
|
|
// 随机字体 |
339
|
|
|
return __DIR__ . '/../fonts/' . mt_rand(0, 5) . '.ttf'; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* 写入验证码到图片中 |
344
|
|
|
* |
345
|
|
|
* @param resource $image |
346
|
|
|
* @param string $phrase |
347
|
|
|
* @param string $font |
348
|
|
|
* @return int |
349
|
|
|
*/ |
350
|
|
|
protected function renderText($image, $phrase, $font) |
351
|
|
|
{ |
352
|
|
|
$length = strlen($phrase); |
353
|
|
|
if ($length === 0) { |
354
|
|
|
return imagecolorallocate($image, 0, 0, 0); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
// 计算文字尺寸 |
358
|
|
|
$size = $this->getConfig('width') / $length - mt_rand(0, 3) - 1; |
|
|
|
|
359
|
|
|
$box = imagettfbbox($size, 0, $font, $phrase); |
|
|
|
|
360
|
|
|
$textWidth = $box[2] - $box[0]; |
|
|
|
|
361
|
|
|
$textHeight = $box[1] - $box[7]; |
362
|
|
|
$x = ($this->getConfig('width') - $textWidth) / 2; |
|
|
|
|
363
|
|
|
$y = ($this->getConfig('height') - $textHeight) / 2 + $size; |
|
|
|
|
364
|
|
|
|
365
|
|
|
if (!count($this->getConfig('textColor'))) { |
366
|
|
|
$textColor = array(mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150)); |
367
|
|
|
} else { |
368
|
|
|
$textColor = $this->getConfig('textColor'); |
369
|
|
|
} |
370
|
|
|
$color = imagecolorallocate($image, $textColor[0], $textColor[1], $textColor[2]); |
371
|
|
|
|
372
|
|
|
// 循环写入字符,随机角度 |
373
|
|
|
for ($i = 0; $i < $length; $i++) { |
374
|
|
|
$box = imagettfbbox($size, 0, $font, $phrase[$i]); |
|
|
|
|
375
|
|
|
$w = $box[2] - $box[0]; |
|
|
|
|
376
|
|
|
$angle = mt_rand(-$this->getConfig('maxAngle'), $this->getConfig('maxAngle')); |
|
|
|
|
377
|
|
|
$offset = mt_rand(-$this->getConfig('maxOffset'), $this->getConfig('maxOffset')); |
378
|
|
|
imagettftext($image, $size, $angle, $x, $y + $offset, $color, $font, $phrase[$i]); |
379
|
|
|
$x += $w; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return $color; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* 画线 |
387
|
|
|
* |
388
|
|
|
* @param resource $image |
389
|
|
|
* @param int $width |
390
|
|
|
* @param int $height |
391
|
|
|
* @param int|null $color |
392
|
|
|
*/ |
393
|
|
|
protected function renderLine($image, $width, $height, $color = null) |
394
|
|
|
{ |
395
|
|
|
$color = $color ?: imagecolorallocate($image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255)); |
|
|
|
|
396
|
|
|
|
397
|
|
|
if (mt_rand(0, 1)) { |
398
|
|
|
// 横向 |
399
|
|
|
$xA = mt_rand(0, $width / 2); |
|
|
|
|
400
|
|
|
$yA = mt_rand(0, $height); |
|
|
|
|
401
|
|
|
$xB = mt_rand($width / 2, $width); |
|
|
|
|
402
|
|
|
$yB = mt_rand(0, $height); |
|
|
|
|
403
|
|
|
} else { |
404
|
|
|
// 纵向 |
405
|
|
|
$xA = mt_rand(0, $width); |
406
|
|
|
$yA = mt_rand(0, $height / 2); |
407
|
|
|
$xB = mt_rand(0, $width); |
408
|
|
|
$yB = mt_rand($height / 2, $height); |
409
|
|
|
} |
410
|
|
|
imagesetthickness($image, mt_rand(1, 3)); |
411
|
|
|
imageline($image, $xA, $yA, $xB, $yB, $color); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* 画线 |
416
|
|
|
* |
417
|
|
|
* @param resource $image |
418
|
|
|
* @param int $max |
419
|
|
|
* @param int|null $color |
420
|
|
|
*/ |
421
|
|
|
protected function drawLines($image, $max, $color = null) |
422
|
|
|
{ |
423
|
|
|
$square = $this->getConfig('width') * $this->getConfig('height'); |
|
|
|
|
424
|
|
|
$effects = mt_rand($square / 3000, $square / 2000); |
425
|
|
|
|
426
|
|
|
// 计算线条数 |
427
|
|
|
if ($max != null && $max > 0) { |
428
|
|
|
$effects = min($max, $effects); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
if ($max !== 0) { |
432
|
|
|
for ($e = 0; $e < $effects; $e++) { |
433
|
|
|
|
434
|
|
|
if ($color !== null) { |
435
|
|
|
$this->renderLine($image, $this->getConfig('width'), $this->getConfig('height'), $color); |
436
|
|
|
} else { |
437
|
|
|
$this->renderLine($image, $this->getConfig('width'), $this->getConfig('height')); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* 获取颜色 |
446
|
|
|
* |
447
|
|
|
* @param resource $image |
448
|
|
|
* @param int $width |
449
|
|
|
* @param int $height |
450
|
|
|
* @param int $background |
451
|
|
|
* @return int |
452
|
|
|
*/ |
453
|
|
|
protected function getColor($image, $width, $height, $background) |
454
|
|
|
{ |
455
|
|
|
$sWidth = imagesx($image); |
|
|
|
|
456
|
|
|
$sHeight = imagesy($image); |
457
|
|
|
if ($width < 0 || $width >= $sWidth || $height < 0 || $height >= $sHeight) { |
458
|
|
|
return $background; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return imagecolorat($image, $width, $height); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @param string $name |
466
|
|
|
* @param array $arguments |
467
|
|
|
* @return $this |
468
|
|
|
*/ |
469
|
|
|
public function __call($name, $arguments) |
470
|
|
|
{ |
471
|
|
|
if (array_key_exists($name, $this->config)) { |
472
|
|
|
$this->config[$name] = $arguments[0]; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
return $this; |
476
|
|
|
} |
477
|
|
|
} |