ValidateCode.class.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
/**
*/
class ValidateCode{
private $ codenum; '/font/elephant.ttf' ;$this ->charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789' ; コード = $this ->createCheckCode() ;ストレージの添え字は code */
コード"] = strtoupper($ this-> checkcode);
for ( $i =0; $i < $this ->codeNum; $i ++){
$this ->checkCode .= $this ->charset{rand(0, strlen ( $this ->文字セット)-1)};
}
return $this ->checkCode;
}
/* 出验证码 */
private function outImage(){
$this ->getCreateImage();
$this ->createLine();
$this ->outputText();
$this ->outputImage();
}
/* 验证码背景图片 */
private function getCreateImage(){
$this ->image = imagecreatetruecolor( $this ->width, $this ->height);
$color = imagecolorallocate( $this ->image, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle( $this ->image,0, $this ->height, $this ->width,0, $color );
}
/* 向句柄图片中写入文字情報 */
private function OutputText(){
$_x = $this ->width / $this ->codeNum; ($ i = 0; $ i&lt; $ this-&gt; codenum; $ i ++){
$ this-&gt; fontcolor = imageColorAllocate($ this-&gt; image、mt_rand(0,156)、mt_rand (0,156),mt_rand(0,156));
imagettftext( $this ->image, $this ->fontSize,mt_rand(-30,30), $_x * $i +mt_rand(1,5), $this ->height / 1.4, $this ->fontColor, $this ->font, $this ->checkCode[ $i ]);
}
}
/* 创建雪花图案 */
プライベート関数createLine() {
for ( $i =0; $i
$color = imagecolorallocate( $this ->image,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline( $this ->image,mt_rand(0, $this ->幅),mt_rand(0, $this ->高さ),mt_rand(0, $this ->幅),mt_rand(0 、$this ->高さ)、$color );
}
for ( $i =0; $i
$color = imagecolorallocate( $this ->image,mt_rand(20) 0,255)、mt_rand(200,255)、mt_rand( 200,255));
imagestring( $this ->image,mt_rand(1,5),mt_rand(0, $this ->width),mt_rand(0, $this ->height), '*' , $color );
}
}
/* 验证コード图片の生成 */
プライベート関数OutputImage(){
header( 'Content-type:image/png' );
imagepng( $this ->image);
imagedestroy( $this ->image);
}
/* 销毁句柄、释放资源 */
function __destruct() {
imagedestroy( $this ->image);
}
}
imageCode.php
1
2
3
4
session_start(); //开启SESSION
require_once 'ValidateCode.class.php' ;
echo new ValidateCode();
image.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
<フォームメソッド = "POST" アクション = "image.php" >