兼容 php8

This commit is contained in:
F4nniu
2023-07-06 13:17:10 +08:00
parent 1d64363c81
commit 42e17addf4

View File

@@ -14,21 +14,21 @@ namespace think\captcha;
use think\Session;
/**
* @property mixed seKey
* @property mixed expire
* @property mixed length
* @property mixed fontSize
* @property float|mixed imageH
* @property float|int|mixed imageW
* @property mixed zhSet
* @property mixed codeSet
* @property mixed reset
* @property mixed fontttf
* @property mixed useZh
* @property mixed useNoise
* @property mixed useCurve
* @property mixed useImgBg
* @property mixed useArithmetic
* @property mixed $seKey
* @property mixed $expire
* @property mixed $length
* @property mixed $fontSize
* @property float|mixed $imageH
* @property float|int|mixed $imageW
* @property mixed $zhSet
* @property mixed $codeSet
* @property mixed $reset
* @property mixed $fontttf
* @property mixed $useZh
* @property mixed $useNoise
* @property mixed $useCurve
* @property mixed $useImgBg
* @property mixed $useArithmetic
*/
class Captcha
{
@@ -190,7 +190,7 @@ class Captcha
$code = [mt_rand(1, 9), '+', mt_rand(1, 9)];
foreach ($code as $key => $char) {
$codeNX += $this->fontSize * 1.5;
imagettftext($this->im, $this->fontSize, 0, $codeNX, $this->fontSize * 1.6, $this->color, $this->fontttf, $char);
imagettftext($this->im, $this->fontSize, 0, (int)$codeNX, (int)($this->fontSize * 1.6), $this->color, $this->fontttf, $char);
}
$code = $this->authcode($code[0] + $code[2]);
} else {
@@ -211,13 +211,13 @@ class Captcha
// 中文验证码
for ($i = 0; $i < $this->length; $i++) {
$code[$i] = iconv_substr($this->zhSet, floor(mt_rand(0, mb_strlen($this->zhSet, 'utf-8') - 1)), 1, 'utf-8');
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), $this->fontSize * ($i + 1) * 1.5, $this->fontSize + mt_rand(10, 20), $this->color, $this->fontttf, $code[$i]);
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), (int)($this->fontSize * ($i + 1) * 1.5), (int)($this->fontSize + mt_rand(10, 20)), $this->color, $this->fontttf, $code[$i]);
}
} else {
for ($i = 0; $i < $this->length; $i++) {
$code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet) - 1)];
$codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.6);
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize * 1.6, $this->color, $this->fontttf, $code[$i]);
$codeNX += mt_rand((int)($this->fontSize * 1.2), (int)($this->fontSize * 1.6));
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), (int)$codeNX, (int)($this->fontSize * 1.6), $this->color, $this->fontttf, $code[$i]);
}
}
$code = $this->authcode(strtoupper(implode('', $code)));
@@ -257,30 +257,30 @@ class Captcha
$px = $py = 0;
// 曲线前部分
$A = mt_rand(1, $this->imageH / 2); // 振幅
$b = mt_rand(-$this->imageH / 4, $this->imageH / 4); // Y轴方向偏移量
$f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量
$T = mt_rand($this->imageH, $this->imageW * 2); // 周期
$A = mt_rand(1, (int)($this->imageH / 2)); // 振幅
$b = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // Y轴方向偏移量
$f = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand((int)($this->imageH), (int)($this->imageW * 2)); // 周期
$w = (2 * M_PI) / $T;
$px1 = 0; // 曲线横坐标起始位置
$px2 = mt_rand($this->imageW / 2, $this->imageW * 0.8); // 曲线横坐标结束位置
$px2 = mt_rand((int)($this->imageW / 2), (int)($this->imageW * 0.8)); // 曲线横坐标结束位置
for ($px = $px1; $px <= $px2; $px = $px + 1) {
if (0 != $w) {
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int)($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->im, $px + $i, $py + $i, $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出不用这while循环性能要好很多
imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出不用这while循环性能要好很多
$i--;
}
}
}
// 曲线后部分
$A = mt_rand(1, $this->imageH / 2); // 振幅
$f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量
$T = mt_rand($this->imageH, $this->imageW * 2); // 周期
$A = mt_rand(1, (int)($this->imageH / 2)); // 振幅
$f = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand((int)($this->imageH), (int)($this->imageW * 2)); // 周期
$w = (2 * M_PI) / $T;
$b = $py - $A * sin($w * $px + $f) - $this->imageH / 2;
$px1 = $px2;
@@ -291,7 +291,7 @@ class Captcha
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int)($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->im, $px + $i, $py + $i, $this->color);
imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color);
$i--;
}
}
@@ -310,7 +310,7 @@ class Captcha
$noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
for ($j = 0; $j < 5; $j++) {
// 绘杂点
imagestring($this->im, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, strlen($codeSet) - 1)], $noiseColor);
imagestring($this->im, 5, mt_rand(-10, (int)($this->imageW)), mt_rand(-10, (int)($this->imageH)), $codeSet[mt_rand(0, strlen($codeSet) - 1)], $noiseColor);
}
}
}