ClickTrait.php
6.4 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
<?php
/**
* Author: lang
* Email: 732853989@qq.com
* Date: 2023/3/29
* Time: 14:50
*/
namespace Kkokk\Poster\Captcha\Traits;
trait ClickTrait
{
public function draw()
{
$im_width = $this->configs['im_width'];
$im_height = $this->configs['im_height'];
$bg_width = $this->configs['bg_width'];
$bg_height = $this->configs['bg_height'];
$this->im = $this->PosterDriver->createIm($bg_width, $bg_height, [], true);
$bg = $this->PosterDriver->createIm($im_width, $im_height, [], true);
$this->drawImage($this->configs['src'], true);
imagecopy($bg, $this->im, 0, 0, 0, 0, $bg_width, $bg_height);
$this->im = $bg;
$this->drawLine($bg_width, $bg_height); // 干扰线
$this->drawChar($bg_width, $bg_height); // 干扰字符
$data = $this->drawText(); // 字
return $data;
}
// 计算 三个点的叉乘 |p1 p2| X |p1 p|
public function getCross($p1, $p2, $p)
{
// (p2.x - p1.x) * (p.y - p1.y) -(p.x - p1.x) * (p2.y - p1.y);
return ($p1[0] - $p[0]) * ($p2[1] - $p[1]) - ($p2[0] - $p[0]) * ($p1[1] - $p[1]);
}
public function getContents($contentsLen)
{
$contents = [];
if ($this->configs['contents']) {
for ($i = 0; $i < $contentsLen; $i++) {
$contents[$i]['contents'] = mb_substr($this->configs['contents'], $i, 1);
}
} else {
$str = $this->getChar('text');
for ($i = 0; $i < $contentsLen; $i++) {
$contents[$i]['contents'] = mb_substr($str, mt_rand(0, 299), 1);
}
}
return $contents;
}
public function getSpace($contentsLen)
{
$font = $this->configs['font_size'] + 15;
$bg_width = $this->configs['bg_width'];
$bg_height = $this->configs['bg_width'];
switch ($contentsLen) {
case 2:
$space[] = [
mt_rand($font, $bg_width / 2 - $font),
mt_rand($font, $bg_height - $font / 2 - 12),
];
$space[] = [
mt_rand($bg_width / 2, $bg_width - $font),
mt_rand($font, $bg_height - $font / 2 - 12),
];
break;
case 3:
$space[] = [
mt_rand($font, $bg_width / 2 - $font),
mt_rand($font, $bg_height / 2),
];
$space[] = [
mt_rand($bg_width / 2, $bg_width - $font),
mt_rand($font, $bg_height / 2),
];
$space[] = [
mt_rand($font, $bg_width - $font),
mt_rand($bg_height / 2 + $font, $bg_height - $font / 2 - 12),
];
break;
default:
$space[] = [
mt_rand($font, $bg_width / 2 - $font),
mt_rand($font, $bg_height / 2),
];
$space[] = [
mt_rand($bg_width / 2, $bg_width - $font),
mt_rand($font, $bg_height / 2),
];
$space[] = [
mt_rand($font, $bg_width / 2 - $font),
mt_rand($bg_height / 2 + $font, $bg_height - $font / 2 - 12),
];
$space[] = [
mt_rand($bg_width / 2, $bg_width - $font),
mt_rand($bg_height / 2 + $font, $bg_height - $font / 2 - 12),
];
break;
}
return $space;
}
public function drawText()
{
$font_family = $this->configs['font_family'];
$font = $this->configs['font_size'];
$contentsLen = $this->configs['font_count'] ?: mt_rand(2, 4);
$contentsLen = $contentsLen < 2 ? 2 : ($contentsLen > 4 ? 4 : $contentsLen);
$contents = $this->getContents($contentsLen);
$color = $this->PosterDriver->createColorAlpha($this->im, [255, 255, 255, 1]);
$spaces = $this->getSpace($contentsLen);
$content = "";
foreach ($contents as $k => $v) {
$content .= $v['contents'];
// 随机获取位置
$spaceKey = mt_rand(0, count($spaces) - 1);
$space = array_splice($spaces, $spaceKey, 1);
$angle = mt_rand(-80, 80); // 旋转角度
$fontBox = imagettfbbox($font, $angle, $font_family, $v['contents']); // 计算文字方框坐标
$x = $space[0][0]; // 起始x坐标
$y = $space[0][1]; // 起始y坐标
$contents[$k]['point'] = [
$x + $fontBox[0], // 左下角,X 位置
$y + $fontBox[1], // 左下角,Y 位置
$x + $fontBox[2], // 右下角,X 位置
$y + $fontBox[3], // 右下角,Y 位置
$x + $fontBox[4], // 右上角,X 位置
$y + $fontBox[5], // 右上角,Y 位置
$x + $fontBox[6], // 左上角,X 位置
$y + $fontBox[7], // 左上角,Y 位置
$angle, // 旋转角度
];
imagettftext($this->im, $font, $angle, $x, $y, $color, $font_family, $v['contents']);
// 加粗字体
$ttfCount = 6;
for ($j = 1; $j <= $ttfCount; $j++) {
// 随机颜色
$ttfColor = $this->PosterDriver->createColorAlpha($this->im, [mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), 1]);
imagettftext($this->im, $font - ($j * 2), $angle, $x + $j, $y - $j, $ttfColor, $font_family, $v['contents']);
}
}
// 显示字体为黑色
$color = $this->PosterDriver->createColorAlpha($this->im, [0, 0, 0, 1]);
$viewFont = 22; // 显示字体大小
$fontBox = imagettfbbox($viewFont, 0, $font_family, $content); // 计算文字长宽
$viewHeight = 296; // 显示字体y坐标
imagettftext($this->im, $viewFont, 0, 10, $viewHeight, $color, $font_family, $content);
$content_height = abs($fontBox[7]) + 1;
return [
'content' => $content,
'content_width' => $fontBox[2],
'content_height' => $content_height,
'x' => 10,
'y' => $viewHeight - $content_height,
'contents' => $contents,
];
}
protected function getImBg()
{
return __DIR__ . '/../../style/rotate_bg/rotate0' . mt_rand(1, 5) . '.jpg';
}
}