代码规范

This commit is contained in:
thinkphp
2016-07-25 10:05:57 +08:00
parent 58a9437e94
commit c639de37e2
2 changed files with 22 additions and 27 deletions

View File

@@ -48,8 +48,8 @@ class Captcha
// 验证成功后是否重置
];
private $_image = null; // 验证码图片实例
private $_color = null; // 验证码字体颜色
private $im = null; // 验证码图片实例
private $color = null; // 验证码字体颜色
/**
* 架构方法 设置参数
@@ -140,12 +140,12 @@ 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));
// 验证码使用随机字体
$ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';
@@ -168,11 +168,11 @@ class Captcha
if ($this->useNoise) {
// 绘杂点
$this->_writeNoise();
$this->writeNoise();
}
if ($this->useCurve) {
// 绘干扰线
$this->_writeCurve();
$this->writeCurve();
}
// 绘验证码
@@ -182,13 +182,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->_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), $this->fontSize * ($i + 1) * 1.5, $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]);
imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize * 1.6, $this->color, $this->fontttf, $code[$i]);
}
}
@@ -202,9 +202,9 @@ class Captcha
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,7 +221,7 @@ class Captcha
* ω决定周期最小正周期T=2π/∣ω∣)
*
*/
private function _writeCurve()
private function writeCurve()
{
$px = $py = 0;
@@ -238,9 +238,9 @@ class Captcha
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);
$i = (int) ($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->_image, $px + $i, $py + $i, $this->_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出不用这while循环性能要好很多
imagesetpixel($this->im, $px + $i, $py + $i, $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出不用这while循环性能要好很多
$i--;
}
}
@@ -258,9 +258,9 @@ class Captcha
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);
$i = (int) ($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->_image, $px + $i, $py + $i, $this->_color);
imagesetpixel($this->im, $px + $i, $py + $i, $this->color);
$i--;
}
}
@@ -271,15 +271,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, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
}
}
}
@@ -306,7 +306,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);
}
@@ -317,4 +317,4 @@ class Captcha
$str = substr(md5($str), 8, 10);
return md5($key . $str);
}
}
}

View File

@@ -12,12 +12,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'));
return captcha_check($value, $id, (array) \think\Config::get('captcha'));
});
\think\Validate::setTypeMsg('captcha', '验证码错误!');
/**
* @param string $id
* @param array $config
@@ -29,7 +28,6 @@ function captcha($id = "", $config = [])
return $captcha->entry($id);
}
/**
* @param $id
* @return string
@@ -39,7 +37,6 @@ function captcha_src($id = "")
return \think\Url::build('/captcha' . ($id ? "/{$id}" : ''));
}
/**
* @param $id
* @return mixed
@@ -49,7 +46,6 @@ function captcha_img($id = "")
return '<img src="' . captcha_src($id) . '" alt="captcha" />';
}
/**
* @param $value
* @param string $id
@@ -61,4 +57,3 @@ function captcha_check($value, $id = "", $config = [])
$captcha = new \think\captcha\Captcha($config);
return $captcha->check($value, $id);
}