Skip to content

Commit aa5487f

Browse files
committed
适配php8
1 parent 5c695cc commit aa5487f

File tree

2 files changed

+65
-110
lines changed

2 files changed

+65
-110
lines changed

src/Config.php

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class Config extends SplBean
3030
public $imageH = null; // 图片高度
3131
public $fonts = []; // 额外字体
3232
public $fontSize = 25; // 字体大小
33-
public $length = 4; // 生成位数
3433
public $mime = MIME::PNG; // 设置类型
3534
public $temp = '/tmp'; // 设置缓存目录
3635

@@ -147,14 +146,16 @@ public function setImageHeight($imageH)
147146
* @param array|string $fonts
148147
* @return Config
149148
*/
150-
public function setFonts($fonts)
149+
public function addFonts($fonts)
151150
{
152-
if (is_string($fonts)) array_push($this->fonts, $fonts);
151+
if (is_string($fonts)) {
152+
$this->fonts[] = $fonts;
153+
}
153154
if (is_array($fonts) && !empty($fonts)) {
154155
if (empty($this->fonts)) {
155156
$this->fonts = $fonts;
156157
} else {
157-
array_merge($this->fonts, $fonts);
158+
$this->fonts = array_merge($this->fonts, $fonts);
158159
}
159160
}
160161
return $this;
@@ -171,27 +172,6 @@ public function setFontSize($fontSize)
171172
return $this;
172173
}
173174

174-
/**
175-
* 设置验证码长度
176-
* @param int $length
177-
* @return Config
178-
*/
179-
public function setLength($length)
180-
{
181-
$this->length = intval($length);
182-
return $this;
183-
}
184-
185-
/**
186-
* 获取配置值
187-
* @param $name
188-
* @author : evalor <master@evalor.cn>
189-
* @return mixed
190-
*/
191-
public function __get($name)
192-
{
193-
return $this->$name;
194-
}
195175

196176
/**
197177
* 十六进制转RGB

src/VerifyCode.php

Lines changed: 60 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,89 +7,75 @@
77
*/
88
class VerifyCode
99
{
10-
protected $conf;
10+
protected Config $conf;
1111
protected $imInstance;
12-
private int $length;
1312

14-
/**
15-
* VerifyCode constructor.
16-
* @param null $options
17-
*/
18-
public function __construct($options = null)
13+
protected mixed $useFont;
14+
15+
public function __construct(Config $option = null)
1916
{
20-
// 传入了配置则使用配置文件
21-
$this->conf = $options instanceof Config ? $options : new Config;
22-
$assetsPath = __DIR__ . '/assets/';
23-
24-
// 合并字体库
25-
$fonts = $this->loadFonts($assetsPath . 'ttf/');
26-
if ($this->fonts) $fonts = array_merge($fonts, $this->fonts);
27-
28-
// 初始化配置项
29-
$this->useFont || $this->useFont = $fonts[array_rand($fonts)];
30-
$this->imageL || $this->imageL = $this->length * $this->fontSize * 1.5 + $this->fontSize / 2;
31-
$this->imageH || $this->imageH = $this->fontSize * 2;
32-
$this->fontColor || $this->fontColor = [mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)];
33-
$this->backColor || $this->backColor = [255, 255, 255];
17+
if($option == null){
18+
$this->conf = new Config();
19+
}else{
20+
$this->conf = $option;
21+
}
22+
23+
if($this->conf->useFont){
24+
$this->useFont = $this->conf->useFont;
25+
}else{
26+
$assetsPath = __DIR__ . '/assets/';
27+
$fonts = $this->loadFonts($assetsPath . 'ttf/');
28+
$this->conf->addFonts($fonts);
29+
$fonts = $this->conf->fonts;
30+
$this->useFont = $fonts[array_rand($fonts)];
31+
}
32+
33+
$this->conf->fontColor || $this->conf->fontColor = [mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)];
34+
$this->conf->backColor || $this->conf->backColor = [255, 255, 255];
3435
}
3536

36-
/**
37-
* 画验证码
38-
* @author : evalor <master@evalor.cn>
39-
* @param string $Code 画指定的验证码
40-
* @return Result
41-
*/
42-
function DrawCode($Code = null)
37+
38+
function drawCode(string $code): Result
4339
{
44-
// 如果传入了验证码则要重置参数
45-
if (!is_null($Code)) {
46-
$this->length = strlen($Code);
47-
$this->imageL || $this->imageL = $this->length * $this->fontSize * 1.5 + $this->fontSize / 2;
48-
$this->imageH || $this->imageH = $this->fontSize * 2;
49-
} else {
50-
$Code = substr(str_shuffle($this->charset), 0, $this->length);
51-
}
40+
$codeLen = strlen($code);
41+
$this->conf->imageL = $codeLen * $this->conf->fontSize * 1.5 + $this->conf->fontSize / 2;
42+
$this->conf->imageH = $this->conf->fontSize * 2;
43+
5244

53-
$Code = strval($Code);
5445

5546
// 创建空白画布
56-
$this->imInstance = imagecreate((int)$this->imageL, (int)$this->imageH);
47+
$this->imInstance = imagecreate((int)$this->conf->imageL, (int)$this->conf->imageH);
5748
// 设置背景颜色
58-
$this->backColor = imagecolorallocate($this->imInstance, $this->backColor[0], $this->backColor[1], $this->backColor[2]);
49+
$this->conf->backColor = imagecolorallocate($this->imInstance, $this->conf->backColor[0], $this->conf->backColor[1], $this->conf->backColor[2]);
5950
// 设置字体颜色
60-
$this->fontColor = imagecolorallocate($this->imInstance, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]);
51+
$this->conf->fontColor = imagecolorallocate($this->imInstance, $this->conf->fontColor[0], $this->conf->fontColor[1], $this->conf->fontColor[2]);
6152
// 画干扰噪点
62-
if ($this->useNoise) $this->writeNoise();
53+
if ($this->conf->useNoise) $this->writeNoise();
6354
// 画干扰曲线
64-
if ($this->useCurve) $this->writeCurve();
55+
if ($this->conf->useCurve) $this->writeCurve();
6556

6657
// 绘验证码
6758
$codeNX = 0; // 验证码第N个字符的左边距
68-
for ($i = 0; $i < $this->length; $i++) {
69-
$codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.4);
59+
for ($i = 0; $i < $codeLen; $i++) {
60+
$codeNX += mt_rand($this->conf->fontSize * 1.2, $this->conf->fontSize * 1.4);
7061
// 写一个验证码字符
71-
imagettftext($this->imInstance, $this->fontSize, mt_rand(-50, 50), $codeNX, (int)($this->fontSize * 1.5), $this->fontColor, $this->useFont, $Code[$i]);
62+
imagettftext($this->imInstance, $this->conf->fontSize, mt_rand(-50, 50), $codeNX, (int)($this->conf->fontSize * 1.5), $this->conf->fontColor, $this->useFont, $code[$i]);
7263
}
7364

7465
// 输出验证码结果集
75-
$this->temp = rtrim(str_replace('\\', '/', $this->temp), '/') . '/';
66+
$this->conf->temp = rtrim(str_replace('\\', '/', $this->conf->temp), '/') . '/';
7667
mt_srand();
77-
$func = 'image' . MIME::getExtensionName($this->mime);
68+
$func = 'image' . MIME::getExtensionName($this->conf->mime);
7869
ob_start();
7970
$func($this->imInstance);
8071
$file = ob_get_contents();
8172
ob_end_clean();
8273
imagedestroy($this->imInstance);
83-
return new Result($file, $Code, $this->mime);
74+
return new Result($file, $code, $this->conf->mime);
8475
}
8576

86-
/**
87-
* 加载字体资源文件
88-
* @param $fontsPath
89-
* @author : evalor <master@evalor.cn>
90-
* @return array
91-
*/
92-
private function loadFonts($fontsPath)
77+
78+
private function loadFonts($fontsPath): array
9379
{
9480
$dir = dir($fontsPath);
9581
$fonts = [];
@@ -106,14 +92,14 @@ private function loadFonts($fontsPath)
10692
* 画干扰杂点
10793
* @author : evalor <master@evalor.cn>
10894
*/
109-
private function writeNoise()
95+
private function writeNoise(): void
11096
{
11197
$codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
11298
for ($i = 0; $i < 10; $i++) {
11399
$noiseColor = imagecolorallocate($this->imInstance, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
114100
for ($j = 0; $j < 5; $j++) {
115101
// 绘杂点
116-
imagestring($this->imInstance, 5, mt_rand(-10, $this->imageL), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
102+
imagestring($this->imInstance, 5, mt_rand(-10, (int)$this->conf->imageL), mt_rand(-10, (int)$this->conf->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
117103
}
118104
}
119105
}
@@ -122,56 +108,45 @@ private function writeNoise()
122108
* 画干扰曲线
123109
* @author : evalor <master@evalor.cn>
124110
*/
125-
private function writeCurve()
111+
private function writeCurve(): void
126112
{
127113
$px = $py = 0;
128114
// 曲线前部分
129-
$A = mt_rand(1, $this->imageH / 2); // 振幅
130-
$b = mt_rand(-$this->imageH / 4, $this->imageH / 4); // Y轴方向偏移量
131-
$f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量
132-
$T = mt_rand($this->imageH, $this->imageL * 2); // 周期
115+
$A = mt_rand(1, $this->conf->imageH / 2); // 振幅
116+
$b = mt_rand(-$this->conf->imageH / 4, $this->conf->imageH / 4); // Y轴方向偏移量
117+
$f = mt_rand(-$this->conf->imageH / 4, $this->conf->imageH / 4); // X轴方向偏移量
118+
$T = mt_rand($this->conf->imageH, $this->conf->imageL * 2); // 周期
133119
$w = (2 * M_PI) / $T;
134120
$px1 = 0; // 曲线横坐标起始位置
135-
$px2 = mt_rand($this->imageL / 2, $this->imageL * 0.8); // 曲线横坐标结束位置
121+
$px2 = mt_rand($this->conf->imageL / 2, $this->conf->imageL * 0.8); // 曲线横坐标结束位置
136122
for ($px = $px1; $px <= $px2; $px = $px + 1) {
137123
if (0 != $w) {
138-
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
139-
$i = (int)($this->fontSize / 5);
124+
$py = $A * sin($w * $px + $f) + $b + $this->conf->imageH / 2; // y = Asin(ωx+φ) + b
125+
$i = (int)($this->conf->fontSize / 5);
140126
while ($i > 0) {
141127
// 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多
142-
imagesetpixel($this->imInstance, $px + $i, $py + $i, $this->fontColor);
128+
imagesetpixel($this->imInstance, $px + $i, $py + $i, $this->conf->fontColor);
143129
$i--;
144130
}
145131
}
146132
}
147133
// 曲线后部分
148-
$A = mt_rand(1, $this->imageH / 2); // 振幅
149-
$f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量
150-
$T = mt_rand($this->imageH, $this->imageL * 2); // 周期
134+
$A = mt_rand(1, $this->conf->imageH / 2); // 振幅
135+
$f = mt_rand(-$this->conf->imageH / 4, $this->conf->imageH / 4); // X轴方向偏移量
136+
$T = mt_rand($this->conf->imageH, $this->conf->imageL * 2); // 周期
151137
$w = (2 * M_PI) / $T;
152-
$b = $py - $A * sin($w * $px + $f) - $this->imageH / 2;
138+
$b = $py - $A * sin($w * $px + $f) - $this->conf->imageH / 2;
153139
$px1 = $px2;
154-
$px2 = $this->imageL;
140+
$px2 = $this->conf->imageL;
155141
for ($px = $px1; $px <= $px2; $px = $px + 1) {
156142
if (0 != $w) {
157-
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
158-
$i = (int)($this->fontSize / 5);
143+
$py = $A * sin($w * $px + $f) + $b + $this->conf->imageH / 2; // y = Asin(ωx+φ) + b
144+
$i = (int)($this->conf->fontSize / 5);
159145
while ($i > 0) {
160-
imagesetpixel($this->imInstance, $px + $i, $py + $i, $this->fontColor);
146+
imagesetpixel($this->imInstance, $px + $i, $py + $i, $this->conf->fontColor);
161147
$i--;
162148
}
163149
}
164150
}
165151
}
166-
167-
function __get($name)
168-
{
169-
return $this->conf->$name;
170-
}
171-
172-
function __set($name, $value)
173-
{
174-
$this->conf->$name = $value;
175-
}
176-
177152
}

0 commit comments

Comments
 (0)