PHP 生成动态GIF验证码

生成一个有意思的动态gif验证码, 来自阿鸡的博客PHP动态gif验证码

效果

GIF

源码

主函数

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
<?php

function ImageCode(
$string = '',
$frames = 60,
$time = 1,
$width = 75,
$height = 25,
$font = 6,
$loops = 0
) {
$auth_str = $string ?: (time() % 2 == 0)
? mt_rand(1000, 9999)
: mt_rand(10000, 99999);

$board_width = $width;
$board_height = $height;

$delay = $time * 1000 / $frames;

// 生成一个N帧的GIF动画
for ($i = 0; $i < $frames; $i++) {
ob_start();

$image = imagecreate($board_width, $board_height);
imagecolorallocate($image, 0, 0, 0);

// 设定文字颜色数组
$colorList[] = imagecolorallocate($image,15,73,210);
$colorList[] = imagecolorallocate($image,0,64,0);
$colorList[] = imagecolorallocate($image,0,0,64);
$colorList[] = imagecolorallocate($image,0,128,128);
$colorList[] = imagecolorallocate($image,27,52,47);
$colorList[] = imagecolorallocate($image,51,0,102);
$colorList[] = imagecolorallocate($image,0,0,145);
$colorList[] = imagecolorallocate($image,0,0,113);
$colorList[] = imagecolorallocate($image,0,51,51);
$colorList[] = imagecolorallocate($image,158,180,35);
$colorList[] = imagecolorallocate($image,59,59,59);
$colorList[] = imagecolorallocate($image,0,0,0);
$colorList[] = imagecolorallocate($image,1,128,180);
$colorList[] = imagecolorallocate($image,0,153,51);
$colorList[] = imagecolorallocate($image,60,131,1);
$colorList[] = imagecolorallocate($image,0,0,0);

$gray = imagecolorallocate($image, 245, 245, 245);
imagefill($image, 0, 0, $gray);
$space = $font * 0.65; // 字符间距

$len = strlen($auth_str);
for ($k = 0; $k < $len; $k++) {
$colorRandom = mt_rand(0, sizeof($colorList) - 1);

$float_top = rand(0, 4);
$float_left = rand(1, 4);

imagettftext(
$image, $font, 0,
$space*$k + $float_left, $font + $float_top,
$colorList[$colorRandom],
'./fonts/comic.ttf',
substr($auth_str, $k, 1)
);
}

for ($k = 0; $k < 20; $k++) {
$colorRandom = mt_rand(0, sizeof($colorList) - 1);
imagesetpixel($image, rand() % 70, rand() % 15, $colorList[$colorRandom]);
}

// 添加干扰线
for ($k = 0; $k < 3; $k++) {
$colorRandom = mt_rand(0, sizeof($colorList) - 1);
$to_draw_line = 1;
if ($to_draw_line) {
imageline($image, mt_rand(0, $board_width), mt_rand(0, $board_height), mt_rand(0, $board_width), mt_rand(0, $board_height), $colorList[$colorRandom]);
} else {
$w = mt_rand(0, $board_width);
$h = mt_rand(0, $board_height);

imagearc($image, $board_width - floor($w / 2), floor($h / 2), $w, $h, rand(90, 180), rand(180, 270), $colorList[$colorRandom]);
}
}

imagegif($image);
imagedestroy($image);
$imageData[] = ob_get_contents();
$delays[] = $delay;
ob_clean();
}

return new AnimatedGif($imageData, $delays, $loops);
}

AnimatedGif类

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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php

class AnimatedGif
{
/**
* The built gif image
* @var resource
*/
private $image = '';
/**
* The array of images to stack
* @var array
*/
private $buffer = Array();
/**
* How many times to loop? 0 = infinite
* @var int
*/
private $number_of_loops = 0;
/**
*
* @var int
*/
private $DIS = 2;
/**
* Which colour is transparent
* @var int
*/
private $transparent_colour = -1;
/**
* Is this the first frame
* @var int
*/
private $first_frame = TRUE;

/**
* Encode an animated gif
* AnimatedGif constructor.
* @param array $source_images
* @param array $image_delays
* @param $number_of_loops
*/
function __construct(
array $source_images,
array $image_delays,
$number_of_loops
) {
/**
* I have no idea what these even do, they appear to do nothing to the image so far
*/
$transparent_colour_red = 0;
$transparent_colour_green = 0;
$transparent_colour_blue = 0;
$this->number_of_loops = ( $number_of_loops > -1 ) ? $number_of_loops : 0;
$this->set_transparent_colour($transparent_colour_red, $transparent_colour_green, $transparent_colour_blue);
$this->buffer_images($source_images);
$this->addHeader();
for ($i = 0; $i < count($this->buffer); $i++) {
$this->addFrame($i, $image_delays [$i]);
}
}

/**
* Set the transparent colour
* @param int $red
* @param int $green
* @param int $blue
*/
private function set_transparent_colour($red, $green, $blue){
$this->transparent_colour = ( $red > -1 && $green > -1 && $blue > -1 ) ?
( $red | ( $green << 8 ) | ( $blue << 16 ) ) : -1;
}

/**
* Buffer the images and check to make sure they are vaild
* @param $source_images
*/
private function buffer_images($source_images)
{
for ($i = 0; $i < count($source_images); $i++) {
$this->buffer [] = $source_images [$i];
if (substr($this->buffer [$i], 0, 6) != "GIF87a" && substr($this->buffer [$i], 0, 6) != "GIF89a") {
throw new Exception('Image at position ' . $i. ' is not a gif');
}
for ($j = ( 13 + 3 * ( 2 << ( ord($this->buffer [$i] { 10 }) & 0x07 ) ) ), $k = TRUE; $k; $j++) {
switch ($this->buffer [$i] { $j }) {
case "!":
if (( substr($this->buffer [$i], ( $j + 3), 8) ) == "NETSCAPE") {
throw new Exception('You cannot make an animation from an animated gif.');
}
break;
case ";":
$k = FALSE;
break;
}
}
}
}
/**
* Add the gif header to the image
*/
private function addHeader()
{
$cmap = 0;
$this->image = 'GIF89a';
if (ord($this->buffer [0] { 10 }) & 0x80) {
$cmap = 3 * ( 2 << ( ord($this->buffer [0] { 10 }) & 0x07 ) );
$this->image .= substr($this->buffer [0], 6, 7);
$this->image .= substr($this->buffer [0], 13, $cmap);
$this->image .= "!\377\13NETSCAPE2.0\3\1" . $this->word($this->number_of_loops) . "\0";
}
}
/**
* Add a frame to the animation
* @param int $frame The frame to be added
* @param int $delay The delay associated with the frame
*/
private function addFrame($frame, $delay)
{
$Locals_str = 13 + 3 * ( 2 << ( ord($this->buffer [$frame] { 10 }) & 0x07 ) );
$Locals_end = strlen($this->buffer [$frame]) - $Locals_str - 1;
$Locals_tmp = substr($this->buffer [$frame], $Locals_str, $Locals_end);
$Global_len = 2 << ( ord($this->buffer [0] { 10 }) & 0x07 );
$Locals_len = 2 << ( ord($this->buffer [$frame] { 10 }) & 0x07 );
$Global_rgb = substr($this->buffer [0], 13, 3 * ( 2 << ( ord($this->buffer [0] { 10 }) & 0x07 ) ));
$Locals_rgb = substr($this->buffer [$frame], 13, 3 * ( 2 << ( ord($this->buffer [$frame] { 10 }) & 0x07 ) ));
$Locals_ext = "!\xF9\x04" . chr(( $this->DIS << 2 ) + 0) .
chr(( $delay >> 0 ) & 0xFF) . chr(( $delay >> 8 ) & 0xFF) . "\x0\x0";
if ($this->transparent_colour > -1 && ord($this->buffer [$frame] { 10 }) & 0x80) {
for ($j = 0; $j < ( 2 << ( ord($this->buffer [$frame] { 10 }) & 0x07 ) ); $j++) {
if (
ord($Locals_rgb { 3 * $j + 0 }) == ( ( $this->transparent_colour >> 16 ) & 0xFF ) &&
ord($Locals_rgb { 3 * $j + 1 }) == ( ( $this->transparent_colour >> 8 ) & 0xFF ) &&
ord($Locals_rgb { 3 * $j + 2 }) == ( ( $this->transparent_colour >> 0 ) & 0xFF )
) {
$Locals_ext = "!\xF9\x04" . chr(( $this->DIS << 2 ) + 1) .
chr(( $delay >> 0 ) & 0xFF) . chr(( $delay >> 8 ) & 0xFF) . chr($j) . "\x0";
break;
}
}
}
switch ($Locals_tmp { 0 }) {
case "!":
$Locals_img = substr($Locals_tmp, 8, 10);
$Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
break;
case ",":
$Locals_img = substr($Locals_tmp, 0, 10);
$Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
break;
}
if (ord($this->buffer [$frame] { 10 }) & 0x80 && $this->first_frame === FALSE) {
if ($Global_len == $Locals_len) {
if ($this->blockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
$this->image .= ( $Locals_ext . $Locals_img . $Locals_tmp );
} else {
$byte = ord($Locals_img { 9 });
$byte |= 0x80;
$byte &= 0xF8;
$byte |= ( ord($this->buffer [0] { 10 }) & 0x07 );
$Locals_img { 9 } = chr($byte);
$this->image .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
}
} else {
$byte = ord($Locals_img { 9 });
$byte |= 0x80;
$byte &= 0xF8;
$byte |= ( ord($this->buffer [$frame] { 10 }) & 0x07 );
$Locals_img { 9 } = chr($byte);
$this->image .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
}
} else {
$this->image .= ( $Locals_ext . $Locals_img . $Locals_tmp );
}
$this->first_frame = FALSE;
}
/**
* Add the gif footer
*/
private function addFooter()
{
$this->image .= ";";
}

/**
* Compare gif blocks? What is a block?
* @param $GlobalBlock
* @param $LocalBlock
* @param $Len
* @return int
*/
private function blockCompare($GlobalBlock, $LocalBlock, $Len)
{
for ($i = 0; $i < $Len; $i++) {
if (
$GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } ||
$GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } ||
$GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 }
) {
return ( 0 );
}
}
return ( 1 );
}
/**
* No clue
* @param int $int
* @return string the char you meant?
*/
private function word($int)
{
return ( chr($int & 0xFF) . chr(( $int >> 8 ) & 0xFF) );
}

/**
* Return the animated gif
* @return resource
*/
function getAnimation()
{
return $this->image;
}
/**
* Return the animated gif
* @echo image
*/
function display()
{
//late footer add
$this->addFooter();
header('Content-type:image/gif');
echo $this->image;
}
}

使用示例代码:

1
2
3
4
5
6
7
8
9
10
11
$str  = 'abcdefghijkmnpqrstuvwxy3456789';
$len = strlen($str) - 1;
$text = '';

for ($i = 0; $i < 6; $i++) {
$text .= $str[rand(0, $len)];
}

$gif = ImageCode($text, 8, 1, 100, 35, 23);

$gif->display();

参考

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2022 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :