1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Tools { |
4
|
|
|
|
5
|
|
|
use Explorer, Number; |
6
|
|
|
|
7
|
|
|
class Captcha { |
8
|
|
|
|
9
|
|
|
private $image = null, $width = 0, $height = 0; |
10
|
|
|
|
11
|
|
|
# Allocate color |
12
|
|
|
|
13
|
|
|
private function allocateColor(array $color = null) { |
14
|
|
|
|
15
|
|
|
if (null === $this->image) return false; |
16
|
|
|
|
17
|
|
|
if (null === $color) $color = [0, 0, 0]; else if (count($color) !== 3) return false; |
18
|
|
|
|
19
|
|
|
foreach (array_values($color) as $key => $value) $color[$key] = Number::forceInt($value, 0, 255); |
20
|
|
|
|
21
|
|
|
# ------------------------ |
22
|
|
|
|
23
|
|
|
return imagecolorallocate($this->image, ...$color); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
# Constructor |
27
|
|
|
|
28
|
|
|
public function __construct(int $width, int $height, array $bg_color = null) { |
29
|
|
|
|
30
|
|
|
if (false === ($image = imagecreatetruecolor($width, $height))) return; |
31
|
|
|
|
32
|
|
|
$this->image = $image; $this->width = $width; $this->height = $height; |
33
|
|
|
|
34
|
|
|
if (false !== ($bg_color = $this->allocateColor($bg_color))) { |
35
|
|
|
|
36
|
|
|
imagefilledrectangle($image, 0, 0, ($width - 1), ($height - 1), $bg_color); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
# Draw text |
41
|
|
|
|
42
|
|
|
public function text(string $font, int $size, int $indent, int $step, string $text, array $color = null) { |
43
|
|
|
|
44
|
|
|
if (null === $this->image) return false; |
45
|
|
|
|
46
|
|
|
if (!Explorer::isFile($font)) return false; |
47
|
|
|
|
48
|
|
|
if (false === ($color = $this->allocateColor($color))) return false; |
49
|
|
|
|
50
|
|
|
$base = (floor(($this->height - $size) / 2) + $size); |
51
|
|
|
|
52
|
|
|
foreach (str_split($text) as $index => $character) { |
53
|
|
|
|
54
|
|
|
$angle = random_int(-10, 10); $x = ($indent + ($index * $step)); $y = random_int(($base - 5), ($base + 5)); |
55
|
|
|
|
56
|
|
|
imagettftext($this->image, $size, $angle, $x, $y, $color, $font, $character); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
# ------------------------ |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
# Draw noise |
65
|
|
|
|
66
|
|
|
public function noise(int $rate, array $color = null) { |
67
|
|
|
|
68
|
|
|
if (null === $this->image) return false; |
69
|
|
|
|
70
|
|
|
if (false === ($color = $this->allocateColor($color))) return false; |
71
|
|
|
|
72
|
|
|
for ($x = 0; $x < $this->width; $x++) for ($y = 0; $y < $this->height; $y++) { |
73
|
|
|
|
74
|
|
|
if (random_int(1, $rate) === 1) imagesetpixel($this->image, $x, $y, $color); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
# ------------------------ |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
# Draw lines |
83
|
|
|
|
84
|
|
|
public function lines(int $count, array $color = null) { |
85
|
|
|
|
86
|
|
|
if (null === $this->image) return false; |
87
|
|
|
|
88
|
|
|
if (false === ($color = $this->allocateColor($color))) return false; |
89
|
|
|
|
90
|
|
|
for ($i = 0; $i < $count; $i++) { |
91
|
|
|
|
92
|
|
|
$point_0 = [0, random_int(0, ($this->height - 1))]; |
93
|
|
|
|
94
|
|
|
$point_1 = [($this->width - 1), random_int(0, ($this->height - 1))]; |
95
|
|
|
|
96
|
|
|
imageline($this->image, $point_0[0], $point_0[1], $point_1[0], $point_1[1], $color); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
# ------------------------ |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
# Return image |
105
|
|
|
|
106
|
|
|
public function image() { |
107
|
|
|
|
108
|
|
|
return $this->image; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|