Skip to content

Commit a83e433

Browse files
committed
optimize
1 parent 8c89934 commit a83e433

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
安全可靠的验证码,拥有很强的反机器识别能力,同时又不失人眼阅读体验。
44

55
## require
6-
- php 5.5+
6+
- php 7.0+
77
- GD2
88

99
## 安装
1010
该组件已包含在Windwork框架中,如果你已安装Windwork框架则可以直接使用。
1111

12-
- 安装方式一:通过composer安装(推荐)
12+
- 安装
1313
```
1414
composer require windwork/captcha
1515
```
@@ -24,12 +24,12 @@ use wf\captcha\Captcha;
2424
require_once 'vendor/autoload.php';
2525
2626
$capt = new Captcha();
27-
$capt->setLevel(Captcha::LEVEL_NORMAL);
27+
//$capt->setLevel(Captcha::LEVEL_HEIGHT);
2828
$capt->create();
2929
3030
// set to session
31-
session_start();
32-
$_SESSION['phrase'] = $capt->getPhrase();
31+
//session_start();
32+
//$_SESSION['phrase'] = $capt->getPhrase();
3333
3434
$capt->output(90);
3535

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"captcha",
66
"anti robot"
77
],
8-
"version" : "1.2.0",
9-
"time" : "2019-12-04 22:00:00",
8+
"version" : "1.2.1",
9+
"time" : "2019-12-05 16:30:00",
1010
"require" : {
1111
"php" : ">=7.0",
1212
"ext-gd" : "*"

lib/Captcha.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class Captcha implements CaptchaInterface
5151
'length' => 4, // 验证码位数
5252
'fontSize' => 36, // 验证码字体大小(px)
5353
'bgColor' => [255,255,255],
54-
'color' => [],
5554
];
5655

5756
/**
@@ -72,7 +71,7 @@ class Captcha implements CaptchaInterface
7271
*
7372
* @var string
7473
*/
75-
protected $phraseSet = '23456789ABCDEFHKLMNPQRSTUVWXY';
74+
protected $phraseSet = '2345679ABCDEFHKLMNPQRSTUVWXY';
7675

7776
/**
7877
* 验证码图片实例
@@ -190,20 +189,17 @@ public function getPhrase()
190189
public function create()
191190
{
192191
// 图片宽(px)
193-
$this->width = $this->cfg['length'] * $this->cfg['fontSize'] * 0.85;
192+
$this->width = $this->cfg['length'] * $this->cfg['fontSize'] * 0.86;
194193

195194
// 图片高(px)
196195
$this->height = $this->cfg['fontSize'] * 1.36;
197196

198197
// 建立一幅 $this->width x $this->height 的图像
199198
$this->image = imagecreate($this->width, $this->height);
200199

201-
$bgColor = $this->cfg['bgColor'] ? $this->cfg['bgColor'] : [mt_rand(0x70, 0xff), mt_rand(0x70, 0xff), mt_rand(0x70, 0xff)];
200+
$bgColor = $this->cfg['bgColor'];
202201
imagecolorallocate($this->image, $bgColor[0], $bgColor[1], $bgColor[2]);
203202

204-
if ($this->cfg['color']) {
205-
}
206-
207203
$this->phrase();
208204

209205
if ($this->cfg['distort']) {
@@ -231,8 +227,7 @@ protected function phrase()
231227

232228
// 倾斜度
233229
$gradient = mt_rand(-22, 22);
234-
$colorRGB = self::COLORS[mt_rand(0, count(self::COLORS) - 1)];
235-
$color = imagecolorallocate($this->image, $colorRGB[0], $colorRGB[1], $colorRGB[2]);
230+
$color = imagecolorallocate($this->image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
236231

237232
// 绘制一个验证码字符
238233
imagettftext($this->image, $this->cfg['fontSize'], $gradient, $phraseNX, $this->cfg['fontSize']*1.1, $color, $this->getFont(), $phrase[$i]);
@@ -242,7 +237,6 @@ protected function phrase()
242237
$this->phrase = implode('', $phrase);
243238
}
244239

245-
246240
/**
247241
* 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)
248242
*
@@ -268,8 +262,7 @@ protected function curve()
268262

269263
$px1 = mt_rand(0, $this->width/4); // 曲线横坐标起始位置
270264
$px2 = mt_rand($px1 + $this->width/3, $this->width); // 曲线横坐标结束位置
271-
$colorRGB = self::COLORS[mt_rand(0, count(self::COLORS) - 1)];
272-
$color = imagecolorallocate($this->image, $colorRGB[0], $colorRGB[1], $colorRGB[2]);
265+
$color = imagecolorallocate($this->image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
273266

274267
for ($px = $px1; $px <= $px2; $px ++) {
275268
if ($w == 0) {

test/demo.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
$capt = new Captcha();
88
//$capt->setLevel(Captcha::LEVEL_HEIGHT);
9-
$capt->setLevel(Captcha::LEVEL_NORMAL);
109
$capt->create();
1110

1211
// set to session
13-
session_start();
14-
$_SESSION['phrase'] = $capt->getPhrase();
12+
//session_start();
13+
//$_SESSION['phrase'] = $capt->getPhrase();
1514

1615
$capt->output(90);
1716

0 commit comments

Comments
 (0)