InputTrait.php
4.2 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
<?php
/**
* Author: lang
* Email: 732853989@qq.com
* Date: 2023/3/29
* Time: 14:51
*/
namespace Kkokk\Poster\Captcha\Traits;
trait InputTrait
{
public function draw()
{
$im_width = $this->configs['im_width'];
$im_height = $this->configs['im_height'];
$this->im = $this->PosterDriver->createIm($im_width, $im_height, [mt_rand(125, 255), 255, mt_rand(125, 255), 1], false);
if ($this->configs['src']) { // 如果指定背景则用背景
$this->drawImage($this->configs['src'], false, 0, 0, 0, 0, $im_width, $im_height);
}
$this->drawLine(); // 干扰线
$this->drawChar(); // 干扰字
$res = $this->getContents();
$this->drawText($res['contents']); // 字
return $res;
}
public function getContents()
{
// type = number 数字 alpha_num 字母和数字 math 计算 text 文字
$fontCount = $this->configs['font_count'];
$contents = '';
switch ($this->configs['type']) {
case 'math':
$formula = '+-x';
$a = mt_rand(0, 20);
$b = mt_rand(0, 20);
$formula = substr($formula, mt_rand(0, 2), 1);
if ($formula == '+') $mul = $a + $b;
if ($formula == '-') $mul = $a - $b;
if ($formula == 'x') $mul = $a * $b;
$res = [
'contents' => [
$a,
$formula,
$b,
'='
],
'value' => $mul,
];
break;
case 'text':
$str = $this->getChar($this->configs['type']);
for ($i = 0; $i < $fontCount; $i++) {
$contents .= mb_substr($str, mt_rand(0, 499), 1);
}
$res = [
'contents' => $contents,
'value' => $contents,
];
break;
case 'alpha_num':
$str = $this->getChar($this->configs['type']);
for ($i = 0; $i < $fontCount; $i++) {
$contents .= substr($str, mt_rand(0, 61), 1);
}
$res = [
'contents' => $contents,
'value' => $contents,
];
break;
default:
$str = $this->getChar($this->configs['type']);
for ($i = 0; $i < $fontCount; $i++) {
$contents .= substr($str, mt_rand(0, 9), 1);
}
$res = [
'contents' => $contents,
'value' => $contents,
];
break;
}
return $res;
}
public function drawText($contents)
{
$font_family = $this->configs['font_family'];
$font = $this->configs['font_size'];
$im_width = $this->configs['im_width'];
$im_height = $this->configs['im_height'];
if (is_array($contents)) {
$equally = $im_width / count($contents);
foreach ($contents as $k => $v) {
$color = $this->PosterDriver->createColorAlpha($this->im, [mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), 1]);
$content = $v;
$x = mt_rand($k * $equally + 10, ($k + 1) * $equally - $font);
$y = mt_rand($font + 10, $im_height);
$angle = $this->configs['type'] === 'math' ? 0 : mt_rand(0, 45);
imagettftext($this->im, $font, $angle, $x, $y, $color, $font_family, $content);
}
} else {
$equally = $im_width / mb_strlen($contents);
for ($i = 0; $i < mb_strlen($contents); $i++) {
$color = $this->PosterDriver->createColorAlpha($this->im, [mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), 1]);
$content = mb_substr($contents, $i, 1);
$x = mt_rand($i * $equally + 10, ($i + 1) * $equally - $font);
$y = mt_rand($font + 10, $im_height);
$angle = mt_rand(0, 45);
imagettftext($this->im, $font, $angle, $x, $y, $color, $font_family, $content);
}
}
}
}