forked from sostuts/Vcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVcode.php
More file actions
487 lines (398 loc) · 11.7 KB
/
Vcode.php
File metadata and controls
487 lines (398 loc) · 11.7 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* Created by PhpStorm.
* User: zeffee
* Date: 2016/8/12
* Time: 18:59
*/
/**
* Class Vcode
* @package App\Lib
*
* Usage:
* 1.$verify = (new Vcode);
* 2.$verify->show(); //显示图片
* 3.$data = $verify->getData(); //返回验证码类型type和文本text
*/
class Vcode
{
/**验证模式
*
* @var string
*/
private $type;
/**图片高度
*
* @var int
*/
private $img_height;
/**图片宽度
*
* @var float
*/
private $img_width;
/**文本长度
*
* @var int
*/
private $text_num;
/**最终验证的文本
*
* @var string
*/
private $verify_text;
/**字体颜色
*
* @var string
*/
private $font_color;
/**字体大小
*
* @var int
*/
private $font_size;
/**字体
*
* @var string
*/
private $font_family;
/**背景颜色
*
* @var string
*/
private $bg_color;
/**噪点
*
* @var int
*/
private $noise_point;
/**干扰线
*
* @var int
*/
private $noise_line;
/**扭曲文字
*
* @var bool
*/
private $text_distortion;
private $img_distortion;
/**图片边框
*
* @var bool
*/
private $img_border;
/**
* 实例
*/
private $image;
/**
* Vcode constructor.
*
* @param string $type "img" or "mix" or "num", 图文点击模式 或者 混合字模式 或者 数字模式
* @param int $num 验证码长度
* @param int $size 字体大小
* @param null $height 图片高度
* @param null $width 图片宽度
* @param bool $distortion 是否扭曲文字
* @param bool $border 是否生成边框
* @param int $noise_point_num 噪点
* @param int $noise_line_num 干扰线
* @param string $bg_img 背景图
* @param string $font_family 字体文件
* @param array $font_color 字体颜色
*/
public function __construct($type = "mix", $num = 5, $size = 16, $height = NULL, $width = NULL, $distortion = false, $border = false, $noise_point_num = 30, $noise_line_num = 3, $bg_img = "", $font_family = "", array $font_color = [])
{
$this->type = $type;
$this->text_num = $num;
$this->font_size = $size;
$this->img_height = $height ?: $this->getDefaultImgHeight();
$this->img_width = $width ?: $this->getDefaultImgWidth();
$this->noise_point = $noise_point_num;
$this->noise_line = $noise_line_num;
$this->text_distortion = $distortion;
$this->img_border = $border;
$this->bg_color = $bg_img;
$this->font_color = $font_color;
//字体自定义
$this->font_family = $font_family ?: "c:\\windows\\fonts\SIMYOU.ttf";
$this->run();
}
/**获取图片宽度默认值
*
* @return float
*/
private function getDefaultImgWidth()
{
return floor($this->font_size * 1.3) * $this->text_num + 10;
}
/**获取图片高度默认值
*
* @return int
*/
private function getDefaultImgHeight()
{
return $this->font_size * 2;
}
protected function run()
{
$this->initImg()->textMergeImg()->distortText()->createNoisePoint()->createNoiseLine()->createBorder();
}
/**初始化图片
*
* @return $this
*/
protected function initImg()
{
$this->image = imagecreatetruecolor($this->img_width, $this->img_height);
if ($this->font_color) {
$this->font_color = imagecolorallocate($this->image, $this->font_color[0], $this->font_color[1], $this->font_color[2]);
} else {
$this->font_color = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
}
if ($this->bg_color) {
$this->bg_color = imagecreatefromjpeg($this->bg_color);
imagecopy($this->image, $this->bg_color, 0, 0, 0, 0, $this->img_width, $this->img_height);
} else {
$this->bg_color = imagecolorallocate($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
imagefill($this->image, 0, 0, $this->bg_color);
}
return $this;
}
/**文字写进图片
*
* @return $this
*/
protected function textMergeImg()
{
$text_arr = $this->getText();
foreach ($text_arr as $i => $text) {
if ("img" === $this->type) {
$angle = [0, 90, 270][ mt_rand(0, 2) ];
$position = imagettftext($this->image, $this->font_size, $angle, mt_rand($this->font_size + 4, $this->img_width - $this->font_size - 4), mt_rand($this->font_size + 4, $this->img_height - $this->font_size - 4), $this->font_color, $this->font_family, $text);
$this->addImgVerifyText($angle, $position);
} else {
imagettftext($this->image, $this->font_size, mt_rand(-1, 1) * mt_rand(1, 20), 5 + $i * floor($this->font_size * 1.3), floor($this->img_height * 0.75), $this->font_color, $this->font_family, $text);
}
}
return $this;
}
/**添加坐标验证文本
*
* @param $angle
* @param array $position
*/
private function addImgVerifyText($angle, array $position)
{
switch ($angle) {
case 0:
$this->verify_text["text"][] = [
"min_x" => $position[6],
"max_x" => $position[2],
"min_y" => $position[7],
"max_y" => $position[3]
];
break;
case 90:
$this->verify_text["text"][] = [
"min_x" => $position[4],
"max_x" => $position[0],
"min_y" => $position[5],
"max_y" => $position[1]
];
break;
case 270:
$this->verify_text["text"][] = [
"min_x" => $position[0],
"max_x" => $position[4],
"min_y" => $position[1],
"max_y" => $position[5]
];
break;
}
}
/**获取随机文字
*
* @return array
*/
private function getText()
{
$img_text = [];
switch ($this->type) {
case "mix":
$this->getMixVerifyText($img_text);
break;
case "num":
$this->getNumVerifyText($img_text);
break;
case "img":
$this->getImgVerifyText($img_text);
break;
}
return $img_text;
}
/**获取图片型验证文本
*
* @param $img_text
*/
private function getImgVerifyText(&$img_text)
{
for ($i = 0; $i < $this->text_num; $i++) {
$this->verify_text["type"][] = $img_text[] = $this->getRandText(2);
}
}
/**生成数字型验证文本
*
* @param $img_text
*/
private function getNumVerifyText(&$img_text)
{
$this->verify_text = ["type" => "结果"];
$img_text[0] = mt_rand(1, 99);
if ("-" === ($img_text[1] = "+-"[ mt_rand(0, 1) ])) {
$img_text[2] = mt_rand(0, $img_text[0]);
$this->verify_text["text"] = $img_text[0] - $img_text[2];
} else {
$img_text[2] = mt_rand(0, 99);
$this->verify_text["text"] = $img_text[0] + $img_text[2];
}
}
/**生成混合型验证文本
*
* @param $img_text
*/
private function getMixVerifyText(&$img_text)
{
$final_verify_text = [];
for ($i = 0; $i < $this->text_num; $i++) {
$rand_num = mt_rand(0, 3);
$final_verify_text[ $rand_num ][] = $img_text[] = $this->getRandText($rand_num);
}
$this->verify_text = $this->getVerifyText($final_verify_text);
}
/**获取指定类型的随机字符
*
* @param int $mode
*
* @return string
*/
private function getRandText($mode)
{
switch ($mode) {
//no break at all
case 0:
return "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[ mt_rand(0, 49) ];
case 1:
return "0123456789"[ mt_rand(0, 9) ];
case 2:
return iconv('GB2312', 'UTF-8', chr(mt_rand(179, 218)) . chr(mt_rand(179, 218)));
case 3:
return "!@#$%&*+-="[ mt_rand(0, 9) ];
}
}
/**获取最终验证的文字
*
* @param $text_arr
*
* @return array
*/
private function getVerifyText($text_arr)
{
$verify_text = [];
$rand_num = array_keys($text_arr)[ mt_rand(0, count($text_arr) - 1) ];
switch ($rand_num) {
case 0:
$verify_text["type"] = "英文";
break;
case 1:
$verify_text["type"] = "数字";
break;
case 2:
$verify_text["type"] = "中文";
break;
case 3:
$verify_text["type"] = "符号";
break;
}
$verify_text["text"] = join("", $text_arr[ $rand_num ]);
return $verify_text;
}
/**干扰文字
*
* @return $this
*/
protected function distortText()
{
if ($this->text_distortion) {
$this->img_distortion = imagecreatetruecolor($this->img_width, $this->img_height);
imagefill($this->img_distortion, 0, 0, $this->bg_color);
for ($x = 0; $x < $this->img_width; $x++) {
for ($y = 0; $y < $this->img_height; $y++) {
imagesetpixel($this->img_distortion, (int)($x + sin($y / $this->img_height * 2 * M_PI - M_PI * 0.5) * 3), $y, imagecolorat($this->image, $x, $y));
}
}
$this->image = $this->img_distortion;
}
return $this;
}
/**生成干扰点
*
* @return $this
*/
protected function createNoisePoint()
{
for ($i = 0; $i < $this->noise_point; $i++) {
$pointColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($this->image, mt_rand(0, $this->img_width), mt_rand(0, $this->img_height), $pointColor);
}
return $this;
}
/**生成干扰线
*
* @return $this
*/
protected function createNoiseLine()
{
for ($i = 0; $i < $this->noise_line; $i++) {
$lineColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), 20);
imageline($this->image, 0, mt_rand(0, $this->img_width), $this->img_width, mt_rand(0, $this->img_height), $lineColor);
}
return $this;
}
/**
* 生成边框
*/
protected function createBorder()
{
if ($this->img_border) {
imagerectangle($this->image, 1, 1, $this->img_width - 1, $this->img_height - 1, $this->font_color);
}
}
/**获取验证文本
*
* @return mixed
*/
public function getData()
{
return $this->verify_text;
}
/**
* 生成png
*/
public function show()
{
Header("Content-type: image/PNG; charset=utf-8");
imagepng($this->image);
}
/**
* 销毁
*/
public function __destruct()
{
imagedestroy($this->image);
if ($this->text_distortion) {
imagedestroy($this->img_distortion);
}
}
}