本文实例讲述了PHP code 验证码生成类定义和简单使用。分享给大家供大家参考,具体如下:
code.php
?
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
<?php
namespace code;
/**
* Class Code
*/
class Code
{
protected $number ; //验证码内字符个数
protected $codeType ; //验证码样式
protected $width ; //图像宽
protected $height ; //图像高
protected $code ; //验证码
protected $image ; //图像资源
/**
* Code constructor.
* @param int $number
* @param int $codeType
* @param int $width
* @param int $height
*/
public function __construct( $number =5, $codeType =2, $width =100, $height =40)
{
$this ->number = $number ;
$this ->codeType = $codeType ;
$this ->width = $width ;
$this ->height = $height ;
$this ->code = $this ->createCode();
}
/**
* 销毁资源
*/
public function __destruct()
{
imagedestroy( $this ->image);
}
/**
* 外部调用code时触发
* @param $name
* @return bool
*/
public function __get( $name )
{
if ( 'code' == $name ) {
return $this -> $name ;
} else {
return false;
}
}
/**
* 生成code
*/
protected function createCode()
{
switch ( $this ->codeType) {
case 0:
$code = $this ->getNum();
break ;
case 1:
$code = $this ->getChar();
break ;
case 2:
$code = $this ->getNumChar();
break ;
default :
die ( '样式不对' );
}
return $code ;
}
/**
* 数字验证码
* @return string
*/
protected function getNum()
{
$str = join( '' , range(0,9));
return substr ( str_shuffle ( $str ), 0, $this ->number);
}
/**
* 字符验证码
* @return string
*/
protected function getChar()
{
$str = join( '' , range( 'a' , 'z' ));
$str = $str . strtoupper ( $str );
return substr ( str_shuffle ( $str ), 0, $this ->number);
}
/**
* 字符和数字混合验证码
* @return string
*/
protected function getNumChar()
{
$num = join( '' , range(0, 9));
$str = join( '' , range( 'a' , 'z' ));
$str_big = strtoupper ( $str );
$numChar = $num . $str . $str_big ;
return substr ( str_shuffle ( $numChar ), 0, $this ->number);
}
/**
* 生成图像
*/
protected function createImage()
{
$this ->image = imagecreatetruecolor( $this ->width, $this ->height);
}
/**
* 填充背景色
*/
protected function fillColor()
{
imagefill( $this ->image, 0, 0, $this ->lightColor());
}
/**
* 浅颜色
* @return int
*/
protected function lightColor()
{
return imagecolorallocate( $this ->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255));
}
/**
* 深颜色
* @return int
*/
protected function darkColor()
{
return imagecolorallocate( $this ->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
}
/**
* 添加验证码字符
*/
protected function drawChar()
{
$width = ceil ( $this ->width/ $this ->number);
for ( $i = 0; $i < $this ->number; $i ++) {
$x = mt_rand( $i * ( $width - 5), ( $i + 1) * ( $width - 5));
$y = mt_rand(0, $this ->height - 15);
imagechar( $this ->image, 5, $x , $y , $this ->code[ $i ], $this ->darkColor());
}
}
/**
* 添加干扰点
*/
protected function drawDisturb()
{
for ( $i = 0; $i < 100; $i ++) {
imagesetpixel( $this ->image, mt_rand(0, $this ->width), mt_rand(0, $this ->height), $this ->darkColor());
}
}
/**
* 添加干扰线
*/
protected function drawArc()
{
for ( $i = 0; $i < $this ->number - 3; $i ++) {
imagearc( $this ->image, mt_rand(5, $this ->width), mt_rand(5, $this ->height), mt_rand(5, $this ->width), mt_rand(5, $this ->height),mt_rand(0, 70), mt_rand(300, 360), $this ->darkColor());
}
}
/**
* 输出显示
*/
protected function show()
{
header( 'Content-Type:image/png' );
imagepng( $this ->image);
}
/**
* 外部image
*/
public function outImage()
{
$this ->createImage(); //创建画布
$this ->fillColor(); //填充背景色
$this ->drawChar(); //添加验证字符
$this ->drawDisturb(); //添加干扰点
$this ->drawArc(); //添加干扰线
$this ->show(); //输出
}
}
|
?
1
2
3
4
5
6
7
8
9
10
|
<?php
include './code/Code.php' ;
$code = new code\\Code();
$code ->outImage();
session_start();
$_SESSION [ 'code' ] = [
'code' => $code ->code,
'exp_time' => time() + (60 * 60 * 10),
];
|
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_42176520/article/details/80426549
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 53
-
2025-05-27 70
-
2025-05-29 59
-
2025-05-25 92
-
2025-05-29 37
热门评论