Repository navigation
Expand file tree
/
Copy pathCode.php
More file actions
149 lines (146 loc) · 3.24 KB
/
Copy pathCode.php
File metadata and controls
149 lines (146 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
class Code
{
//验证码的个数
protected $number;
//验证码的类型
protected $codeType;
//宽度
protected $width;
//高度
protected $height;
//图片的类型
protected $imageType;
//验证码
protected $code;
//图片资源
protected $image;
//初始化成员属性
function __construct($number = 4, $codeType = 2,$width = 100, $height = 30, $imageType = 'png')
{
$this->number = $number;
$this->codeType = $codeType;
$this->width = $width;
$this->height = $height;
$this->imageType = $imageType;
//调用生成验证码的函数
$this->code = $this->getCode();
}
//生成验证码字符串的函数
protected function getCode()
{
switch ($this->codeType) {
//纯数字
case 0:
$code = $this->getNumberCode();
break;
case 1:
$code = $this->getCharCode();
break;
case 2:
$code = $this->getNumCharCode();
break;
default :
exit('不支持的类型');
}
return $code;
}
//数字的验证码
protected function getNumberCode()
{
$str = join('', range(0, 9));
return substr(str_shuffle($str), 0, $this->number);
}
//字母的验证码
protected function getCharCode()
{
$arr = range('a', 'z');
$str = join('', $arr);
$str .= strtoupper($str);
return substr(str_shuffle($str), 0, $this->number);
}
//结合
protected function getNumCharCode()
{
$str = '';
for ($i=0; $i < $this->number; $i++) {
$t = mt_rand(0,2);
switch ($t) {
case 0:
$str .= chr(mt_rand(48, 57));
break;
case 1:
$str .= chr(mt_rand(65, 90));
break;
case 2:
$str .= chr(mt_rand(97, 122));
break;
}
}
return $str;
}
//当外部读取code字符串的额,允许读取
function __get($name)
{
if ($name == 'code') {
return $this->code;
}
return false;
}
public function outImage()
{
//生成画布
$this->image = $this->createImage();
//填充背景颜色
$this->fillBackground();
//写验证码
$this->drawCode();
//画干扰元素
$this->drawDisturb();
//输出显示到浏览器
$this->show();
}
protected function createImage()
{
return imagecreatetruecolor($this->width, $this->height);
}
protected function fillBackground()
{
imagefill($this->image,0, 0, $this->lightColor());
}
protected function lightColor()
{
return imagecolorallocate($this->image, mt_rand(130,255), mt_rand(130,255), mt_rand(130,255));
}
protected function darkColor()
{
return imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
}
protected function drawCode()
{
for ($i=0; $i < $this->number; $i++) {
$c = $this->code[$i];
$width = ceil($this->width / $this->number);
$x = mt_rand($i * $width + 10, ($i + 1) * $width - 15);
$y = mt_rand(0, $this->height - 15);
imagechar($this->image , 5, $x, $y, $c, $this->darkColor());
}
}
protected function drawDisturb()
{
for ($i=0; $i < $this->width * $this->height / 8; $i++) {
$x = mt_rand(0, $this->width);
$y = mt_rand(0, $this->height);
imagesetpixel($this->image, $x, $y, $this->darkColor());
}
}
protected function show()
{
header('Content-Type:image/'. $this->imageType);
//拼接一下函数名字
$func = 'image' . $this->imageType;
$func($this->image);
}
}
$code = new Code();
$code->outImage();