Skip to content

Commit

Permalink
代码规范
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jul 25, 2016
1 parent 58a9437 commit c639de3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
42 changes: 21 additions & 21 deletions src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Captcha
// 验证成功后是否重置
];

private $_image = null; // 验证码图片实例
private $_color = null; // 验证码字体颜色
private $im = null; // 验证码图片实例
private $color = null; // 验证码字体颜色

/**
* 架构方法 设置参数
Expand Down Expand Up @@ -140,12 +140,12 @@ public function entry($id = '')
// 图片高(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') . '/';

Expand All @@ -168,11 +168,11 @@ public function entry($id = '')

if ($this->useNoise) {
// 绘杂点
$this->_writeNoise();
$this->writeNoise();
}
if ($this->useCurve) {
// 绘干扰线
$this->_writeCurve();
$this->writeCurve();
}

// 绘验证码
Expand All @@ -182,13 +182,13 @@ public function entry($id = '')
// 中文验证码
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]);
}
}

Expand All @@ -202,9 +202,9 @@ public function entry($id = '')

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');
}
Expand All @@ -221,7 +221,7 @@ public function entry($id = '')
* ω:决定周期(最小正周期T=2π/∣ω∣)
*
*/
private function _writeCurve()
private function writeCurve()
{
$px = $py = 0;

Expand All @@ -238,9 +238,9 @@ private function _writeCurve()
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--;
}
}
Expand All @@ -258,9 +258,9 @@ private function _writeCurve()
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--;
}
}
Expand All @@ -271,15 +271,15 @@ private function _writeCurve()
* 画杂点
* 往图片上写不同颜色的字母或数字
*/
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);
}
}
}
Expand All @@ -306,7 +306,7 @@ private function _background()
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);
}

Expand All @@ -317,4 +317,4 @@ private function authcode($str)
$str = substr(md5($str), 8, 10);
return md5($key . $str);
}
}
}
7 changes: 1 addition & 6 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +28,6 @@ function captcha($id = "", $config = [])
return $captcha->entry($id);
}


/**
* @param $id
* @return string
Expand All @@ -39,7 +37,6 @@ function captcha_src($id = "")
return \think\Url::build('/captcha' . ($id ? "/{$id}" : ''));
}


/**
* @param $id
* @return mixed
Expand All @@ -49,7 +46,6 @@ function captcha_img($id = "")
return '<img src="' . captcha_src($id) . '" alt="captcha" />';
}


/**
* @param $value
* @param string $id
Expand All @@ -61,4 +57,3 @@ function captcha_check($value, $id = "", $config = [])
$captcha = new \think\captcha\Captcha($config);
return $captcha->check($value, $id);
}

0 comments on commit c639de3

Please sign in to comment.