mirror of
https://gitee.com/fastadminnet/think-captcha.git
synced 2026-07-08 16:52:49 +08:00
增加计算类验证
This commit is contained in:
@@ -8,7 +8,10 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"require": {},
|
"require": {
|
||||||
|
"topthink/think-installer": ">=1.0.10",
|
||||||
|
"topthink/framework": "~5.0.0"
|
||||||
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"think\\captcha\\": "src/"
|
"think\\captcha\\": "src/"
|
||||||
|
|||||||
@@ -13,6 +13,23 @@ namespace think\captcha;
|
|||||||
|
|
||||||
use think\Session;
|
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
|
||||||
|
*/
|
||||||
class Captcha
|
class Captcha
|
||||||
{
|
{
|
||||||
protected $config = [
|
protected $config = [
|
||||||
@@ -46,6 +63,7 @@ class Captcha
|
|||||||
// 背景颜色
|
// 背景颜色
|
||||||
'reset' => true,
|
'reset' => true,
|
||||||
// 验证成功后是否重置
|
// 验证成功后是否重置
|
||||||
|
'useArithmetic' => false //是否使用算术验证码
|
||||||
];
|
];
|
||||||
|
|
||||||
private $im = null; // 验证码图片实例
|
private $im = null; // 验证码图片实例
|
||||||
@@ -146,21 +164,7 @@ class Captcha
|
|||||||
|
|
||||||
// 验证码字体随机颜色
|
// 验证码字体随机颜色
|
||||||
$this->color = imagecolorallocate($this->im, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150));
|
$this->color = imagecolorallocate($this->im, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150));
|
||||||
// 验证码使用随机字体
|
|
||||||
$ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';
|
|
||||||
|
|
||||||
if (empty($this->fontttf)) {
|
|
||||||
$dir = dir($ttfPath);
|
|
||||||
$ttfs = [];
|
|
||||||
while (false !== ($file = $dir->read())) {
|
|
||||||
if ('.' != $file[0] && substr($file, -4) == '.ttf') {
|
|
||||||
$ttfs[] = $file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$dir->close();
|
|
||||||
$this->fontttf = $ttfs[array_rand($ttfs)];
|
|
||||||
}
|
|
||||||
$this->fontttf = $ttfPath . $this->fontttf;
|
|
||||||
|
|
||||||
if ($this->useImgBg) {
|
if ($this->useImgBg) {
|
||||||
$this->background();
|
$this->background();
|
||||||
@@ -175,9 +179,34 @@ class Captcha
|
|||||||
$this->writeCurve();
|
$this->writeCurve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';
|
||||||
|
|
||||||
// 绘验证码
|
// 绘验证码
|
||||||
$code = []; // 验证码
|
$code = []; // 验证码
|
||||||
$codeNX = 0; // 验证码第N个字符的左边距
|
$codeNX = 0; // 验证码第N个字符的左边距
|
||||||
|
if ($this->useArithmetic) {
|
||||||
|
$this->fontttf = $ttfPath . '6.ttf';
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
$code = $this->authcode($code[0] + $code[2]);
|
||||||
|
} else {
|
||||||
|
// 验证码使用随机字体
|
||||||
|
if (empty($this->fontttf)) {
|
||||||
|
$dir = dir($ttfPath);
|
||||||
|
$ttfs = [];
|
||||||
|
while (false !== ($file = $dir->read())) {
|
||||||
|
if ('.' != $file[0] && substr($file, -4) == '.ttf') {
|
||||||
|
$ttfs[] = $file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$dir->close();
|
||||||
|
$this->fontttf = $ttfs[array_rand($ttfs)];
|
||||||
|
}
|
||||||
|
$this->fontttf = $ttfPath . $this->fontttf;
|
||||||
if ($this->useZh) {
|
if ($this->useZh) {
|
||||||
// 中文验证码
|
// 中文验证码
|
||||||
for ($i = 0; $i < $this->length; $i++) {
|
for ($i = 0; $i < $this->length; $i++) {
|
||||||
@@ -191,10 +220,12 @@ class Captcha
|
|||||||
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize * 1.6, $this->color, $this->fontttf, $code[$i]);
|
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize * 1.6, $this->color, $this->fontttf, $code[$i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$code = $this->authcode(strtoupper(implode('', $code)));
|
||||||
|
}
|
||||||
|
|
||||||
// 保存验证码
|
// 保存验证码
|
||||||
$key = $this->authcode($this->seKey);
|
$key = $this->authcode($this->seKey);
|
||||||
$code = $this->authcode(strtoupper(implode('', $code)));
|
|
||||||
$secode = [];
|
$secode = [];
|
||||||
$secode['verify_code'] = $code; // 把校验码保存到session
|
$secode['verify_code'] = $code; // 把校验码保存到session
|
||||||
$secode['verify_time'] = time(); // 验证码创建时间
|
$secode['verify_time'] = time(); // 验证码创建时间
|
||||||
@@ -279,7 +310,7 @@ class Captcha
|
|||||||
$noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
|
$noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
|
||||||
for ($j = 0; $j < 5; $j++) {
|
for ($j = 0; $j < 5; $j++) {
|
||||||
// 绘杂点
|
// 绘杂点
|
||||||
imagestring($this->im, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
|
imagestring($this->im, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, strlen($codeSet) - 1)], $noiseColor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user