Skip to content

Commit

Permalink
Merge pull request #2 from evalor/master
Browse files Browse the repository at this point in the history
验证码类功能更新
  • Loading branch information
kiss291323003 authored Nov 16, 2017
2 parents 75a6de4 + 28cda38 commit 47eda19
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 34 deletions.
95 changes: 70 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,100 @@
验证码组件
------

用于生产验证码,支持自定义验证码字体
用于生产验证码,支持自定义验证码字体,使用Composer安装

配置
```
composer require easyswoole/verifycode
```

配置定义
------

组件本身提供了默认配置,即使不做任何设置也可以直接生成验证码,需要对验证码进行自定义配置可以使用组件提供的`Conf`类进行动态配置

```
use easySwoole\VerifyCode\Conf;
$Conf = new Conf();
```

#### 设置字符集合
可以自定义验证码生成使用的字符集合设置后从集合中随机选取,不设置则从`[0-9A-Za-z]`中随机选取

```
$Conf->setCharset('123456ABCD');
```

#### 设置背景色
设置验证码的背景颜色,不设置默认使用白色,支持使用完整HEX,缩写HEX和RGB值设置

```
$Conf->setBackColor('#3A5FCD');
$Conf->setBackColor('CCC');
$Conf->setBackColor([30, 144, 255]);
```

// 验证码字符长度 默认生成4位
$Conf->length = 4;
#### 设置文字颜色
设置验证码的背景颜色,不设置则随机生成一个颜色,支持使用完整HEX,缩写HEX和RGB值设置

// 指定字体文件 默认随机
$Conf->useFont = '/path/to/file.ttf';
```
$Conf->setFontColor('#3A5FCD');
$Conf->setFontColor('CCC');
$Conf->setFontColor([30, 144, 255]);
```

// 验证码的文字颜色[R,G,B] 默认随机
$Conf->fontColor = [135, 135, 135];
#### 设置混淆
支持两种混淆方式,默认两种混淆都是关闭的,需要手动开启

// 验证码图片宽度 默认动态适应
$Conf->imageL = 400;
```
// 开启或关闭混淆曲线
$Conf->setUseCurve();
// 开启或关闭混淆噪点
$Conf->setUseNoise();
```

// 验证码图片高度 默认动态适应
$Conf->imageH = 200;
#### 设置字体
默认验证码类已经带有6种字体,如果需要增加自己的字体库来提高识别难度,或者指定使用的字体,可以如下设置,注意字体路径需要使用绝对路径,即文件的完整路径

// 验证码字符集 默认数字+大小写字母
$Conf->charset = '1234567890';
```
// 增加单个字体传入路径字符串
$Conf->setFonts('path/to/file.ttf');
// 增加多个字体传入路径的数组
$Conf->setFonts(['path/to/file1.ttf', 'path/to/file2.ttf']);
```

// 开启干扰噪点 默认不开启
$Conf->useNoise = false;
```
// 指定生成使用的字体文件
$Conf->setUseFont('path/to/file.ttf');
```

// 开启混淆曲线 默认不开启
$Conf->useCurve = false;
#### 其他设置
可以指定图片宽高,字体大小,随机生成的验证码位数等

// 添加字体到验证码字体库 生成时随机
$Conf->fonts = ['/path/to/file.ttf', '/path/to/file2.ttf'];
```
// 设置图片的宽度
$Conf->setImageWidth(400);
// 设置图片的高度
$Conf->setImageHeight(200);
// 设置生成字体大小
$Conf->setFontSize(30);
// 设置生成验证码位数
$Conf->setLength(4);
```

// 验证码字体大小
$Conf->fontSize = 25;
#### 链式调用
为了更流畅的进行设置,所有的配置项均支持链式调用

```
$Conf = new Conf();
$Conf->setUseNoise()->setUseCurve()->setFontSize(30);
```

传入配置有两种方法,可以使用上方的动态配置,将设置好的配置类传入给验证码类
------

可以使用上方的动态配置,将设置好的配置类传入给验证码类,
```
$Conf = new Conf();
$Conf->length = 5;
$Conf->setFontSize(30);
$VCode = new VerifyCode($Conf);
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"autoload": {
"psr-4": {
"easySwoole\\":"src"
"easySwoole\\VerifyCode\\":"src"
}
}
}
165 changes: 163 additions & 2 deletions src/Conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Conf extends SplBean
public $useNoise = false; // 随机噪点
public $useFont = null; // 指定字体
public $fontColor = null; // 字体颜色
public $backColor = null; // 背景颜色
public $imageL = null; // 图片宽度
public $imageH = null; // 图片高度
public $fonts = []; // 额外字体
Expand All @@ -35,13 +36,173 @@ protected function initialize()
{
}

public function __set($name, $value)
/**
* 设置字符集
* @param string $charset
* @return Conf
*/
public function setCharset($charset)
{
$this->$name = $value;
is_string($charset) && $this->charset = $charset;
return $this;
}

/**
* 开启混淆曲线
* @param bool $useCurve
* @return Conf
*/
public function setUseCurve($useCurve = true)
{
is_bool($useCurve) && $this->useCurve = $useCurve;
return $this;
}

/**
* 开启噪点生成
* @param bool $useNoise
* @return Conf
*/
public function setUseNoise($useNoise = true)
{
is_bool($useNoise) && $this->useNoise = $useNoise;
return $this;
}

/**
* 使用自定义字体
* @param string $useFont
* @return Conf
*/
public function setUseFont($useFont)
{
is_string($useFont) && $this->useFont = $useFont;
return $this;
}

/**
* 设置文字颜色
* @param array|string $fontColor
* @return Conf
*/
public function setFontColor($fontColor)
{
if (is_string($fontColor)) $this->fontColor = $this->HEXToRGB($fontColor);
if (is_array($fontColor)) $this->fontColor = $fontColor;
return $this;
}

/**
* 设置背景颜色
* @param null $backColor
* @return Conf
*/
public function setBackColor($backColor)
{
if (is_string($backColor)) $this->backColor = $this->HEXToRGB($backColor);
if (is_array($backColor)) $this->backColor = $backColor;
return $this;
}

/**
* 设置图片宽度
* @param int|string $imageL
* @return Conf
*/
public function setImageWidth($imageL)
{
$this->imageL = intval($imageL);
return $this;
}

/**
* 设置图片高度
* @param null $imageH
* @return Conf
*/
public function setImageHeight($imageH)
{
$this->imageH = intval($imageH);
return $this;
}

/**
* 设置字体集
* @param array|string $fonts
* @return Conf
*/
public function setFonts($fonts)
{
if (is_string($fonts)) array_push($this->fonts, $fonts);
if (is_array($fonts) && !empty($fonts)) {
if (empty($this->fonts)) {
$this->fonts = $fonts;
} else {
array_merge($this->fonts, $fonts);
}
}
return $this;
}

/**
* 设置字体尺寸
* @param int $fontSize
* @return Conf
*/
public function setFontSize($fontSize)
{
$this->fontSize = intval($fontSize);
return $this;
}

/**
* 设置验证码长度
* @param int $length
* @return Conf
*/
public function setLength($length)
{
$this->length = intval($length);
return $this;
}

/**
* 获取配置值
* @param $name
* @author : evalor <[email protected]>
* @return mixed
*/
public function __get($name)
{
return $this->$name;
}

/**
* 十六进制转RGB
* @param $hexColor
* @author : evalor <[email protected]>
* @return array
*/
function HEXToRGB($hexColor)
{
$color = str_replace('#', '', $hexColor);
if (strlen($color) > 3) {
$rgb = array(
hexdec(substr($color, 0, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 4, 2))
);
} else {
$color = $hexColor;
$r = substr($color, 0, 1) . substr($color, 0, 1);
$g = substr($color, 1, 1) . substr($color, 1, 1);
$b = substr($color, 2, 1) . substr($color, 2, 1);
$rgb = array(
hexdec($r),
hexdec($g),
hexdec($b)
);
}
return $rgb;
}
}
13 changes: 7 additions & 6 deletions src/VerifyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public function __construct($options = null)

// 初始化配置项
$this->useFont || $this->useFont = $fonts[array_rand($fonts)];
$this->imageL || $this->imageL = $this->length * $this->fontSize * 1.5 + $this->fontSize * 1.5;
$this->imageL || $this->imageL = $this->length * $this->fontSize * 1.5 + $this->fontSize / 2;
$this->imageH || $this->imageH = $this->fontSize * 2;
$this->fontColor || $this->fontColor = [mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)];
$this->backColor || $this->backColor = [255, 255, 255];
}

/**
Expand All @@ -49,16 +50,16 @@ function DrawCode($Code = null)
// 如果传入了验证码则要重置参数
if (!is_null($Code)) {
$this->length = strlen($Code);
$this->imageL = $this->length * $this->fontSize * 1.5 + $this->fontSize * 1.5;
$this->imageH = $this->fontSize * 2;
$this->imageL || $this->imageL = $this->length * $this->fontSize * 1.5 + $this->fontSize / 2;
$this->imageH || $this->imageH = $this->fontSize * 2;
} else {
$Code = substr(str_shuffle($this->charset), 0, $this->length);
}

// 创建空白画布
$this->imInstance = imagecreate($this->imageL, $this->imageH);
// 设置背景颜色
imagecolorallocate($this->imInstance, 243, 251, 254);
$this->backColor = imagecolorallocate($this->imInstance, $this->backColor[0], $this->backColor[1], $this->backColor[2]);
// 设置字体颜色
$this->fontColor = imagecolorallocate($this->imInstance, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]);
// 画干扰噪点
Expand All @@ -69,9 +70,9 @@ function DrawCode($Code = null)
// 绘验证码
$codeNX = 0; // 验证码第N个字符的左边距
for ($i = 0; $i < $this->length; $i++) {
$codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.6);
$codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.4);
// 写一个验证码字符
imagettftext($this->imInstance, $this->fontSize, mt_rand(-40, 70), $codeNX, $this->fontSize * 1.5, $this->fontColor, $this->useFont, $Code[$i]);
imagettftext($this->imInstance, $this->fontSize, mt_rand(-50, 50), $codeNX, $this->fontSize * 1.5, $this->fontColor, $this->useFont, $Code[$i]);
}

// 输出验证码结果集
Expand Down

0 comments on commit 47eda19

Please sign in to comment.