randNumber.class.php
3.93 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
<?php
class Util_Randnumber_Randnumber
{
private $mWidth;
private $mHeight;
private $mNum;
private $mImageType = '';
private $mRandNumber; //产生的验证码
private $mImage; //验证码图片
private $mDisturbPix; //干扰像素
/**
* 初始化函数
*/
public function __construct($imageType = 'gif',$width = '50', $height = '20', $num = '4')
{
$this->mWidth = $width;
$this->mHeight = $height;
$this->mNum = $num;
$this->mImageType = $imageType;
$this->createCode();
}
/**
* 产生验证码
*/
public function createCode()
{
$this->mRandNumber = strtoupper(substr(md5(rand()), 0, $this->mNum));
}
/**
* 获取验证码
*
* @return string
*/
public function getCode()
{
return $this->mRandNumber;
}
/**
* 产生图片
*/
private function createImage()
{
$this->mImage = @imagecreate($this->mWidth, $this->mHeight);
//底色颜色
$bg = imagecolorallocate($this->mImage, 255, 255, 255);
//边框颜色
$border = imagecolorallocate($this->mImage, 0, 0, 0);
//设置底色
imagefilledrectangle($this->mImage, 0, 0, $this->mWidth - 1, $this->mHeight - 1, $bg);
//设置边框
imagerectangle($this->mImage, 0, 0, $this->mWidth - 1, $this->mHeight - 1, $border);
}
/**
* 设置干扰像素
*/
private function setDisturbPix()
{
for($i = 0; $i <= 100; $i++)
{
$this->mDisturbPix = imagecolorallocate($this->mImage, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->mImage, rand(2, $this->mWidth), rand(2, $this->mHeight), $this->mDisturbPix);
}
}
/**
* 往图片上写验证码
*/
private function writeCkCodeToImage()
{
for($i = 0; $i <= $this->mNum; $i++)
{
$bg_color = imagecolorallocate($this->mImage, rand(0, 255), rand(0, 128), rand(0, 255));
$x = floor($this->mWidth / $this->mNum) * $i + 3;
$y = rand(0, $this->mHeight - 15);
$font = dirname(__FILE__).'/arial.ttf';
imagettftext($this->mImage,15,0, 5, 23,$bg_color, $font,$this->mRandNumber);
//imagechar($this->mImage, rand(5, 8), $x, $y, $this->mRandNumber[$i], $bg_color);
}
//增加图片的掺杂特性
srand((double)microtime()*1000000);
$linenums = mt_rand(20, 30);
for($i=0; $i <= $linenums ; $i++)
{
$linecolor = imagecolorallocate($this->mImage, 0, 0, 0);
$linex = mt_rand(5, $this->mWidth-5);
$liney = mt_rand(5, $this->mHeight/2);
imageline($this->mImage, $linex, $liney, $linex + mt_rand(0, 10) - 2, $liney + mt_rand(0, 10) - 2, $linecolor);
}
header("Pragma:no-cache\r\n");
header("Cache-Control:no-cache\r\n");
header("Expires:0\r\n");
/* 输出图片格式为 gif,jpg ,png*/
header("Content-type: image/".$this->mImageType);
$ImageFun = 'image'.$this->mImageType;
$ImageFun($this->mImage);
ImageDestroy($this->mImage);
}
/**
* 设置验证码
*/
public function setRandNumber($randNumber)
{
$this->mRandNumber = $randNumber;
}
public function getNum()
{
return $this->Num;
}
/**
* 生成验证码图片
*/
public function expImg()
{
if(empty($this->mRandNumber))
{
$this->createCode(); //产生验证码
}
$this->createImage(); //产生图片
$this->setDisturbPix(); //设置干扰像素
$this->writeCkCodeToImage(); //往图片上写验证码
}
/**
* 析构函数
*/
public function __destruct()
{
unset($this->mWidth, $this->mHeight,$this->mImageType,$this->mNum);
}
}
?>