16 Commits

Author SHA1 Message Date
F4nniu
9be9dd7e61 规范版本匹配 2023-07-16 17:41:14 +08:00
F4nniu
66f47c8966 通配 framework dev-master 2023-07-06 13:17:50 +08:00
F4nniu
42e17addf4 兼容 php8 2023-07-06 13:17:10 +08:00
yunwuxin
1d64363c81 增加计算类验证 2019-01-28 12:48:36 +08:00
ThinkPHP
d1ca0cae74 Merge pull request #26 from cw1997/master
修正了模版拼接错误的BUG,应该将element变量拼接到img标签的id属性,而不是alt属性
2018-06-07 18:51:51 +08:00
changwei
246728b345 修正了模版拼接错误的BUG,应该将element变量拼接到img标签的id属性,而不是alt属性 2018-06-07 18:31:46 +08:00
changwei
951cdb0a39 Revert "规范readme格式,增加点击图片自动更换验证码的功能说明"
This reverts commit d7dcc6a615.
2018-06-07 18:31:09 +08:00
changwei
9af43612cc 修正了模版拼接错误的BUG,应该将element变量拼接到img标签的id属性,而不是alt属性
抱歉,昨天我提pr的时候忘记检查了,今天才发现这个BUG
2018-06-07 18:28:38 +08:00
ThinkPHP
deb6c8677b Merge pull request #25 from cw1997/master
增加支持验证码图片点击自动更换
2018-06-07 11:24:56 +08:00
changwei
d7dcc6a615 规范readme格式,增加点击图片自动更换验证码的功能说明 2018-06-07 02:43:57 +08:00
changwei
2a10155772 增加支持验证码图片点击自动更换
这算是一个比较常用的需求了
2018-06-07 02:38:39 +08:00
舒博
9572406f5e 修复手动验证方式验证的时候captcha相关配置项修改不生效的bug,同时规范了验证失败时的提示文本! 2016-12-28 16:22:04 +08:00
thinkphp
dd0e3cdbf2 代码规范 2016-07-25 10:07:36 +08:00
thinkphp
c639de37e2 代码规范 2016-07-25 10:05:57 +08:00
thinkphp
58a9437e94 readme修正 2016-07-17 22:50:13 +08:00
thinkphp
a0cb6687a6 修正背景图片显示 2016-07-17 22:44:47 +08:00
4 changed files with 124 additions and 79 deletions

View File

@@ -22,7 +22,7 @@ thinkphp5 验证码类库
使用TP5的内置验证功能即可 使用TP5的内置验证功能即可
~~~ ~~~
$this->validate($data,[ $this->validate($data,[
'captcha|验证码'=>'required|captcha' 'captcha|验证码'=>'require|captcha'
]); ]);
~~~ ~~~
或者手动验证 或者手动验证

View File

@@ -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 || dev-master"
},
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"think\\captcha\\": "src/" "think\\captcha\\": "src/"

View File

@@ -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,10 +63,11 @@ class Captcha
// 背景颜色 // 背景颜色
'reset' => true, 'reset' => true,
// 验证成功后是否重置 // 验证成功后是否重置
'useArithmetic' => false //是否使用算术验证码
]; ];
private $_image = null; // 验证码图片实例 private $im = null; // 验证码图片实例
private $_color = null; // 验证码字体颜色 private $color = null; // 验证码字体颜色
/** /**
* 架构方法 设置参数 * 架构方法 设置参数
@@ -140,15 +158,43 @@ class Captcha
// 图片高(px) // 图片高(px)
$this->imageH || $this->imageH = $this->fontSize * 2.5; $this->imageH || $this->imageH = $this->fontSize * 2.5;
// 建立一幅 $this->imageW x $this->imageH 的图像 // 建立一幅 $this->imageW x $this->imageH 的图像
$this->_image = imagecreate($this->imageW, $this->imageH); $this->im = imagecreate($this->imageW, $this->imageH);
// 设置背景 // 设置背景
imagecolorallocate($this->_image, $this->bg[0], $this->bg[1], $this->bg[2]); imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]);
// 验证码字体随机颜色 // 验证码字体随机颜色
$this->_color = imagecolorallocate($this->_image, 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));
// 验证码使用随机字体
if ($this->useImgBg) {
$this->background();
}
if ($this->useNoise) {
// 绘杂点
$this->writeNoise();
}
if ($this->useCurve) {
// 绘干扰线
$this->writeCurve();
}
$ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/'; $ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';
// 绘验证码
$code = []; // 验证码
$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, (int)$codeNX, (int)($this->fontSize * 1.6), $this->color, $this->fontttf, $char);
}
$code = $this->authcode($code[0] + $code[2]);
} else {
// 验证码使用随机字体
if (empty($this->fontttf)) { if (empty($this->fontttf)) {
$dir = dir($ttfPath); $dir = dir($ttfPath);
$ttfs = []; $ttfs = [];
@@ -161,40 +207,25 @@ class Captcha
$this->fontttf = $ttfs[array_rand($ttfs)]; $this->fontttf = $ttfs[array_rand($ttfs)];
} }
$this->fontttf = $ttfPath . $this->fontttf; $this->fontttf = $ttfPath . $this->fontttf;
if ($this->useImgBg) {
$this->_background();
}
if ($this->useNoise) {
// 绘杂点
$this->_writeNoise();
}
if ($this->useCurve) {
// 绘干扰线
$this->_writeCurve();
}
// 绘验证码
$code = []; // 验证码
$codeNX = 0; // 验证码第N个字符的左边距
if ($this->useZh) { if ($this->useZh) {
// 中文验证码 // 中文验证码
for ($i = 0; $i < $this->length; $i++) { 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'); $code[$i] = iconv_substr($this->zhSet, floor(mt_rand(0, mb_strlen($this->zhSet, 'utf-8') - 1)), 1, 'utf-8');
imagettftext($this->_image, $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 { } else {
for ($i = 0; $i < $this->length; $i++) { for ($i = 0; $i < $this->length; $i++) {
$code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet) - 1)]; $code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet) - 1)];
$codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.6); $codeNX += mt_rand((int)($this->fontSize * 1.2), (int)($this->fontSize * 1.6));
imagettftext($this->_image, $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), (int)$codeNX, (int)($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(); // 验证码创建时间
@@ -202,9 +233,9 @@ class Captcha
ob_start(); ob_start();
// 输出图像 // 输出图像
imagepng($this->_image); imagepng($this->im);
$content = ob_get_clean(); $content = ob_get_clean();
imagedestroy($this->_image); imagedestroy($this->im);
return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png'); return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
} }
@@ -221,35 +252,35 @@ class Captcha
* ω决定周期最小正周期T=2π/∣ω∣) * ω决定周期最小正周期T=2π/∣ω∣)
* *
*/ */
private function _writeCurve() private function writeCurve()
{ {
$px = $py = 0; $px = $py = 0;
// 曲线前部分 // 曲线前部分
$A = mt_rand(1, $this->imageH / 2); // 振幅 $A = mt_rand(1, (int)($this->imageH / 2)); // 振幅
$b = mt_rand(-$this->imageH / 4, $this->imageH / 4); // Y轴方向偏移量 $b = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // Y轴方向偏移量
$f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量 $f = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand($this->imageH, $this->imageW * 2); // 周期 $T = mt_rand((int)($this->imageH), (int)($this->imageW * 2)); // 周期
$w = (2 * M_PI) / $T; $w = (2 * M_PI) / $T;
$px1 = 0; // 曲线横坐标起始位置 $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) { for ($px = $px1; $px <= $px2; $px = $px + 1) {
if (0 != $w) { if (0 != $w) {
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int)($this->fontSize / 5); $i = (int)($this->fontSize / 5);
while ($i > 0) { while ($i > 0) {
imagesetpixel($this->_image, $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--; $i--;
} }
} }
} }
// 曲线后部分 // 曲线后部分
$A = mt_rand(1, $this->imageH / 2); // 振幅 $A = mt_rand(1, (int)($this->imageH / 2)); // 振幅
$f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量 $f = mt_rand(-(int)($this->imageH / 4), (int)($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand($this->imageH, $this->imageW * 2); // 周期 $T = mt_rand((int)($this->imageH), (int)($this->imageW * 2)); // 周期
$w = (2 * M_PI) / $T; $w = (2 * M_PI) / $T;
$b = $py - $A * sin($w * $px + $f) - $this->imageH / 2; $b = $py - $A * sin($w * $px + $f) - $this->imageH / 2;
$px1 = $px2; $px1 = $px2;
@@ -260,7 +291,7 @@ class Captcha
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int)($this->fontSize / 5); $i = (int)($this->fontSize / 5);
while ($i > 0) { while ($i > 0) {
imagesetpixel($this->_image, $px + $i, $py + $i, $this->_color); imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color);
$i--; $i--;
} }
} }
@@ -271,15 +302,15 @@ class Captcha
* 画杂点 * 画杂点
* 往图片上写不同颜色的字母或数字 * 往图片上写不同颜色的字母或数字
*/ */
private function _writeNoise() private function writeNoise()
{ {
$codeSet = '2345678abcdefhijkmnpqrstuvwxyz'; $codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
for ($i = 0; $i < 10; $i++) { for ($i = 0; $i < 10; $i++) {
//杂点颜色 //杂点颜色
$noiseColor = imagecolorallocate($this->_image, 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->_image, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $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);
} }
} }
} }
@@ -288,9 +319,9 @@ class Captcha
* 绘制背景图片 * 绘制背景图片
* 注:如果验证码输出图片比较大,将占用比较多的系统资源 * 注:如果验证码输出图片比较大,将占用比较多的系统资源
*/ */
private function _background() private function background()
{ {
$path = dirname(__FILE__) . '/verify/bgs/'; $path = __DIR__ . '/../assets/bgs/';
$dir = dir($path); $dir = dir($path);
$bgs = []; $bgs = [];
@@ -306,7 +337,7 @@ class Captcha
list($width, $height) = @getimagesize($gb); list($width, $height) = @getimagesize($gb);
// Resample // Resample
$bgImage = @imagecreatefromjpeg($gb); $bgImage = @imagecreatefromjpeg($gb);
@imagecopyresampled($this->_image, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height); @imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height);
@imagedestroy($bgImage); @imagedestroy($bgImage);
} }

View File

@@ -11,11 +11,11 @@
\think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index"); \think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
\think\Validate::extend('captcha', function ($value, $id = "") { \think\Validate::extend('captcha', function ($value, $id = '') {
return captcha_check($value, $id, (array)\think\Config::get('captcha')); return captcha_check($value, $id);
}); });
\think\Validate::setTypeMsg('captcha', '验证码错误!'); \think\Validate::setTypeMsg('captcha', ':attribute错误!');
/** /**
@@ -23,7 +23,7 @@
* @param array $config * @param array $config
* @return \think\Response * @return \think\Response
*/ */
function captcha($id = "", $config = []) function captcha($id = '', $config = [])
{ {
$captcha = new \think\captcha\Captcha($config); $captcha = new \think\captcha\Captcha($config);
return $captcha->entry($id); return $captcha->entry($id);
@@ -34,7 +34,7 @@ function captcha($id = "", $config = [])
* @param $id * @param $id
* @return string * @return string
*/ */
function captcha_src($id = "") function captcha_src($id = '')
{ {
return \think\Url::build('/captcha' . ($id ? "/{$id}" : '')); return \think\Url::build('/captcha' . ($id ? "/{$id}" : ''));
} }
@@ -44,21 +44,32 @@ function captcha_src($id = "")
* @param $id * @param $id
* @return mixed * @return mixed
*/ */
function captcha_img($id = "") function captcha_img($id = '')
{ {
return '<img src="' . captcha_src($id) . '" alt="captcha" />'; return '<img src="' . captcha_src($id) . '" alt="captcha" />';
} }
/**
* @param string $id
* @param string $element 验证码HTML元素ID
* @return string
*/
function captcha_img_with_replacement($id = '', $element = 'think-captcha')
{
return '<img src="' . captcha_src($id) . '" alt="captcha" id="' . $element . '" onclick="document.getElementById("'
. $element . '").src="' . captcha_src($id) . '"+Math.random()' . '/>';
}
/** /**
* @param $value * @param $value
* @param string $id * @param string $id
* @param array $config * @param array $config
* @return bool * @return bool
*/ */
function captcha_check($value, $id = "", $config = []) function captcha_check($value, $id = '')
{ {
$captcha = new \think\captcha\Captcha($config); $captcha = new \think\captcha\Captcha((array)\think\Config::get('captcha'));
return $captcha->check($value, $id); return $captcha->check($value, $id);
} }