Skip to content

Commit

Permalink
改进文档/文档注释
Browse files Browse the repository at this point in the history
  • Loading branch information
cmpan committed Jun 26, 2017
1 parent 2ee43f1 commit 8353af7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
1、生成验证码图片
```
$cfg = [
'class' => '\wf\captcha\strategy\GDSimple',
'gradient' => 32, // 文字倾斜度范围
'fontSize' => 30, // 验证码字体大小(px)
'length' => 4, // 验证码位数
'distortLevel' => 0,// 验证码扭曲级别(0-9),0为不扭曲,如果启用,建议为验证码字体大小/6
];
$capt = new \wf\captcha\strategy\GDSimple($cfg);
$capt = \wfCaptcha($cfg); // new \wf\captcha\strategy\GDSimple($cfg);
$secId = 'login';
$capt->render($secId);
```

2、验证码对比校验
```
// 验证码id与生产验证码的id一致
Expand Down Expand Up @@ -48,7 +50,7 @@ if (!\wf\captcha\Code::check(@$_POST['secode'], $secId)) {
// GDSimple(推荐使用)
$cfg = [
'class' => 'GDSimple',
'class' => '\wf\captcha\strategy\GDSimple',
'gradient' => 32, // 文字倾斜度范围
'fontSize' => 30, // 验证码字体大小(px)
'length' => 4, // 验证码位数
Expand All @@ -61,15 +63,15 @@ $cfg = [
// GDSafety
$cfg = [
'class' => 'GDSafety',
'class' => '\wf\captcha\strategy\GDSafety',
'expire' => 3000, // 验证码过期时间(s)
'gradient' => 20, // 文字倾斜度范围
'length' => 4, // 验证码位数
];
// GD
$cfg = [
'class' => 'GD',
'class' => '\wf\captcha\strategy\GD',
'expire' => 3000, // 验证码过期时间(s)
'useBgImg' => false, // 是否使用背景图片
'useCurve' => false, // 是否画混淆曲线
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"captcha",
"anti robot"
],
"version" : "0.5.2",
"version" : "0.6.0",
"time" : "2017-06-26 16:10:00",
"require" : {
"php" : ">=5.5.0",
"ext-gd" : "*"
Expand Down
6 changes: 3 additions & 3 deletions lib/CaptchaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* 验证码接口
*
* useage:
* $capt = app()->getDi()->captcha();
* $capt = wfCaptcha(); //
* $capt->render();
*
* 验证码对比校验
* if (!app()->getDi()->captcha()->check(@$_POST['secode'])) {
* if (!\wf\captcha\Code::check(@$_POST['secode'])) {
* print 'error secode';
* }
*
Expand All @@ -29,7 +29,7 @@
interface CaptchaInterface
{
/**
* 输出验证码并把在服务器端保存验证码
* 生成验证码,输出为图片格式,并保存验证码字符到session
*
* @param string $id = 'sec' 验证码类别,如登录)login;注册)regster
*/
Expand Down
12 changes: 7 additions & 5 deletions lib/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
namespace wf\captcha;

/**
* 验证码存储及检查
* 验证码存储、检查、扭曲
*/
class Code
{
const SESS_KEY = '@captcha_sk';

/**
* 验证验证码是否正确
* 检测验证码是否正确
*
* @param string $code 用户验证码
* @param string $id 下标
* @param string $id = 'sec' 下标
* @return bool 用户验证码是否正确
*/
public static function check($code, $id = 'sec')
Expand Down Expand Up @@ -54,9 +54,10 @@ public static function check($code, $id = 'sec')

/**
* 保存最新验证码信息
*
* @param string $code 验证码字符串
* @param int $expire 验证码过期时间戳
* @param string $id = ''
* @param string $id = 'sec'
*/
public static function save($code, $expire, $id = 'sec')
{
Expand All @@ -70,7 +71,8 @@ public static function save($code, $expire, $id = 'sec')
}

/**
* 水平扭曲图片
* 水平扭曲验证码图片
*
* @param resource $img
* @param int $level 扭曲级别(0-9),0为不扭曲,建议为验证码字体大小/6
*/
Expand Down
22 changes: 16 additions & 6 deletions lib/strategy/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* 可配置的属性都是一些简单直观的变量,我就不用弄一堆的setter/getter了
*
* useage:
* $capt = \wf\Factory::captcha();
* $capt->setCfg($cfg);
* $capt = \wf\captcha\strategy\GD($cfg);
* $capt->entry();
*
* 验证码对比校验
Expand Down Expand Up @@ -58,11 +57,20 @@ public function __construct(array $cfg = [])
* @var string
*/
private $codeSet = '3456789AbcDEFHKLMNPQRSTUVWXY';
private $image = null; // 验证码图片实例
private $color = null; // 验证码字体颜色

/**
* 验证码图片实例
* @var function imagecreate
*/
private $image = null;

/**
* 验证码字体颜色
* @var function imagecolorallocate
*/
private $color = null;

/**
* 生成验证码
* {@inheritDoc}
* @see \wf\captcha\CaptchaInterface::render()
*/
Expand Down Expand Up @@ -103,7 +111,9 @@ public function render($id = 'sec')
$gradient = mt_rand(-$this->cfg['gradient'], $this->cfg['gradient']);

// 写一个验证码字符
imagettftext($this->image, $this->cfg['fontSize'], $gradient, $codeNX, mt_rand($this->cfg['fontSize']*1.25, $this->cfg['fontSize']*1.36), $this->color/*imagecolorallocate($this->image, mt_rand(1,130), mt_rand(1,130), mt_rand(1,130))*/, $ttf, $code[$i]);
imagettftext($this->image, $this->cfg['fontSize'], $gradient, $codeNX,
mt_rand($this->cfg['fontSize']*1.25, $this->cfg['fontSize']*1.36),
$this->color, $ttf, $code[$i]);
}

// 保存验证码
Expand Down
29 changes: 15 additions & 14 deletions lib/strategy/GDSimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
* 可配置的属性都是一些简单直观的变量,我就不用弄一堆的setter/getter了
*
* useage:
* $capt = new \component\captcha\Captcha($cfg);
* $capt = \wf\captcha\strategy\GDSimple($cfg);
* $capt->entry();
*
* 验证码对比校验
* if (!\component\captcha\Captcha::check(@$_POST['secode'], $id)) {
* if (!\wf\captcha\Captcha::check(@$_POST['secode'], $id)) {
* print 'error secode';
* }
*
Expand Down Expand Up @@ -44,19 +44,16 @@ public function __construct(array $cfg = [])

/**
* 验证码图片实例
* @var \component\captcha\Captcha
* @var function imagecreate
*/
private $image = null;
private $image = null;

/**
* 验证码字体颜色
* @var array
*/
private $color = null;

/**
* 生成验证码
* @var function imagecolorallocate
*/
private $color = null;

public function render($id = 'sec')
{
// 图片宽(px)
Expand All @@ -77,11 +74,13 @@ public function render($id = 'sec')
// 建立一幅 $this->cfg['width'] x $this->cfg['height'] 的图像
$this->image = imagecreate($this->cfg['width'], $this->cfg['height']);

imagecolorallocate($this->image, $this->cfg['bgColor'][0], $this->cfg['bgColor'][1], $this->cfg['bgColor'][2]);
imagecolorallocate($this->image, $this->cfg['bgColor'][0],
$this->cfg['bgColor'][1], $this->cfg['bgColor'][2]);
}

// 验证码文字颜色
$this->color = imagecolorallocate($this->image, $this->cfg['color'][0], $this->cfg['color'][1], $this->cfg['color'][2]);
$this->color = imagecolorallocate($this->image, $this->cfg['color'][0],
$this->cfg['color'][1], $this->cfg['color'][2]);

// 验证码使用随机字体
$ttf = dirname(dirname(__DIR__)) . '/assets/gd_simple/code.ttf';
Expand All @@ -98,7 +97,8 @@ public function render($id = 'sec')
} elseif ($i > 0 && in_array($code[$i-1], ['B', 'G', 'H', 'N'])) {
// 较宽易混淆的字符
$codeNX += $this->cfg['fontSize']*0.7;
} elseif ($i > 0 && (is_numeric($code[$i-1]) || in_array($code[$i-1], [6, 7, 9, 'A', 'C', 'F', 'L', 'P', 'T', 'V']))) {
} elseif ($i > 0 && (is_numeric($code[$i-1])
|| in_array($code[$i-1], [6, 7, 9, 'A', 'C', 'F', 'L', 'P', 'T', 'V']))) {
// 不易混淆的字符 679ACFLPTV
$codeNX += $this->cfg['fontSize']*0.5;
} else {
Expand All @@ -109,7 +109,8 @@ public function render($id = 'sec')
$gradient = mt_rand(-$this->cfg['gradient'], $this->cfg['gradient']);

// 写一个验证码字符
imagettftext($this->image, $this->cfg['fontSize'], $gradient, $codeNX, $this->cfg['fontSize']*1.1, $this->color/*imagecolorallocate($this->image, mt_rand(1,130), mt_rand(1,130), mt_rand(1,130))*/, $ttf, $code[$i]);
imagettftext($this->image, $this->cfg['fontSize'], $gradient,
$codeNX, $this->cfg['fontSize']*1.1, $this->color, $ttf, $code[$i]);
}

// 保存验证码
Expand Down

0 comments on commit 8353af7

Please sign in to comment.