mirror of
https://gitee.com/fastadminnet/think-captcha.git
synced 2026-07-09 01:02:47 +08:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9be9dd7e61 | ||
|
|
66f47c8966 | ||
|
|
42e17addf4 | ||
|
|
1d64363c81 | ||
|
|
d1ca0cae74 | ||
|
|
246728b345 | ||
|
|
951cdb0a39 | ||
|
|
9af43612cc | ||
|
|
deb6c8677b | ||
|
|
d7dcc6a615 | ||
|
|
2a10155772 | ||
|
|
9572406f5e | ||
|
|
dd0e3cdbf2 | ||
|
|
c639de37e2 | ||
|
|
58a9437e94 | ||
|
|
a0cb6687a6 | ||
|
|
0c55455df2 |
@@ -22,7 +22,7 @@ thinkphp5 验证码类库
|
||||
使用TP5的内置验证功能即可
|
||||
~~~
|
||||
$this->validate($data,[
|
||||
'captcha|验证码'=>'required|captcha'
|
||||
'captcha|验证码'=>'require|captcha'
|
||||
]);
|
||||
~~~
|
||||
或者手动验证
|
||||
|
||||
@@ -8,7 +8,10 @@
|
||||
}
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"require": {},
|
||||
"require": {
|
||||
"topthink/think-installer": ">=1.0.10",
|
||||
"topthink/framework": "~5.0.0 || dev-master"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"think\\captcha\\": "src/"
|
||||
|
||||
131
src/Captcha.php
131
src/Captcha.php
@@ -13,6 +13,23 @@ 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
|
||||
*/
|
||||
class Captcha
|
||||
{
|
||||
protected $config = [
|
||||
@@ -46,10 +63,11 @@ class Captcha
|
||||
// 背景颜色
|
||||
'reset' => true,
|
||||
// 验证成功后是否重置
|
||||
'useArithmetic' => false //是否使用算术验证码
|
||||
];
|
||||
|
||||
private $_image = null; // 验证码图片实例
|
||||
private $_color = null; // 验证码字体颜色
|
||||
private $im = null; // 验证码图片实例
|
||||
private $color = null; // 验证码字体颜色
|
||||
|
||||
/**
|
||||
* 架构方法 设置参数
|
||||
@@ -108,18 +126,18 @@ class Captcha
|
||||
{
|
||||
$key = $this->authcode($this->seKey) . $id;
|
||||
// 验证码不能为空
|
||||
$secode = Session::get($key);
|
||||
$secode = Session::get($key, '');
|
||||
if (empty($code) || empty($secode)) {
|
||||
return false;
|
||||
}
|
||||
// session 过期
|
||||
if (time() - $secode['verify_time'] > $this->expire) {
|
||||
Session::delete($key);
|
||||
Session::delete($key, '');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->authcode(strtoupper($code)) == $secode['verify_code']) {
|
||||
$this->reset && Session::delete($key);
|
||||
$this->reset && Session::delete($key, '');
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -140,15 +158,43 @@ class Captcha
|
||||
// 图片高(px)
|
||||
$this->imageH || $this->imageH = $this->fontSize * 2.5;
|
||||
// 建立一幅 $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') . '/';
|
||||
|
||||
// 绘验证码
|
||||
$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)) {
|
||||
$dir = dir($ttfPath);
|
||||
$ttfs = [];
|
||||
@@ -161,50 +207,35 @@ class Captcha
|
||||
$this->fontttf = $ttfs[array_rand($ttfs)];
|
||||
}
|
||||
$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) {
|
||||
// 中文验证码
|
||||
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->_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 {
|
||||
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->_image, $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)));
|
||||
}
|
||||
|
||||
// 保存验证码
|
||||
$key = $this->authcode($this->seKey);
|
||||
$code = $this->authcode(strtoupper(implode('', $code)));
|
||||
|
||||
$secode = [];
|
||||
$secode['verify_code'] = $code; // 把校验码保存到session
|
||||
$secode['verify_time'] = time(); // 验证码创建时间
|
||||
Session::set($key . $id, $secode);
|
||||
Session::set($key . $id, $secode, '');
|
||||
|
||||
ob_start();
|
||||
// 输出图像
|
||||
imagepng($this->_image);
|
||||
imagepng($this->im);
|
||||
$content = ob_get_clean();
|
||||
imagedestroy($this->_image);
|
||||
imagedestroy($this->im);
|
||||
|
||||
return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
|
||||
}
|
||||
@@ -221,35 +252,35 @@ class Captcha
|
||||
* ω:决定周期(最小正周期T=2π/∣ω∣)
|
||||
*
|
||||
*/
|
||||
private function _writeCurve()
|
||||
private function writeCurve()
|
||||
{
|
||||
$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->_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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 曲线后部分
|
||||
$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;
|
||||
@@ -260,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->_image, $px + $i, $py + $i, $this->_color);
|
||||
imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color);
|
||||
$i--;
|
||||
}
|
||||
}
|
||||
@@ -271,15 +302,15 @@ class Captcha
|
||||
* 画杂点
|
||||
* 往图片上写不同颜色的字母或数字
|
||||
*/
|
||||
private function _writeNoise()
|
||||
private function writeNoise()
|
||||
{
|
||||
$codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
|
||||
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++) {
|
||||
// 绘杂点
|
||||
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);
|
||||
|
||||
$bgs = [];
|
||||
@@ -306,7 +337,7 @@ class Captcha
|
||||
list($width, $height) = @getimagesize($gb);
|
||||
// Resample
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
|
||||
\think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
|
||||
|
||||
\think\Validate::extend('captcha', function ($value, $id = "") {
|
||||
return captcha_check($value, $id, (array)\think\Config::get('captcha'));
|
||||
\think\Validate::extend('captcha', function ($value, $id = '') {
|
||||
return captcha_check($value, $id);
|
||||
});
|
||||
|
||||
\think\Validate::setTypeMsg('captcha', '验证码错误!');
|
||||
\think\Validate::setTypeMsg('captcha', ':attribute错误!');
|
||||
|
||||
|
||||
/**
|
||||
@@ -23,7 +23,7 @@
|
||||
* @param array $config
|
||||
* @return \think\Response
|
||||
*/
|
||||
function captcha($id = "", $config = [])
|
||||
function captcha($id = '', $config = [])
|
||||
{
|
||||
$captcha = new \think\captcha\Captcha($config);
|
||||
return $captcha->entry($id);
|
||||
@@ -34,7 +34,7 @@ function captcha($id = "", $config = [])
|
||||
* @param $id
|
||||
* @return string
|
||||
*/
|
||||
function captcha_src($id = "")
|
||||
function captcha_src($id = '')
|
||||
{
|
||||
return \think\Url::build('/captcha' . ($id ? "/{$id}" : ''));
|
||||
}
|
||||
@@ -44,21 +44,32 @@ function captcha_src($id = "")
|
||||
* @param $id
|
||||
* @return mixed
|
||||
*/
|
||||
function captcha_img($id = "")
|
||||
function captcha_img($id = '')
|
||||
{
|
||||
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 string $id
|
||||
* @param array $config
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user