【代码】PHP 生成GIF动画实现动态图片验证码
相信大多数的人都想过如何用PHP生成GIF动画来实现动态图片验证码,下面就为大家分享一下实现的过程。ImageCode函数通过GIFEncoder类实现的GIF动画的PHP源代码,感兴趣的朋友可以去研究一下,O(∩_∩)O谢谢~
/**
* 调用示例
* */
session_start();
$authstr = mt_rand(1000, 9999);// 生成随机字符串
$_SESSION['verify'] = $authstr;// 记录session
ImageCode($authstr, 60);// 显示GIF动画
效果如图:http://www.phper.org.cn/content/uploadfile/201106/4d630c956a58bd4b88a4a6e0cddbb84520110622145343.gif
view sourceprint?
01.
/** (PS:T不错的php Q扣裙:276167802,验证:csl)
02.
* ImageCode 生成GIF图片验证
03.
* @param $string 字符串
04.
* @param $width 宽度
05.
* @param $height 高度
06.
* */
07.
function ImageCode($string = '', $width = 75, $height = 25)
08.
{
09.
$authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 99999));
10.
11.
$board_width = $width;
12.
$board_height = $height;
13.
// 生成一个32帧的GIF动画
14.
for($i = 0; $i < 32; $i++)
15.
{
16.
ob_start();
17.
$image = imagecreate($board_width, $board_height);
18.
imagecolorallocate($image, 0,0,0);
19.
// 设定文字颜色数组
20.
$colorList[] = ImageColorAllocate($image, 15,73,210);
21.
$colorList[] = ImageColorAllocate($image, 0,64,0);
22.
$colorList[] = ImageColorAllocate($image, 0,0,64);
23.
$colorList[] = ImageColorAllocate($image, 0,128,128);
24.
$colorList[] = ImageColorAllocate($image, 27,52,47);
25.
$colorList[] = ImageColorAllocate($image, 51,0,102);
26.
$colorList[] = ImageColorAllocate($image, 0,0,145);
27.
$colorList[] = ImageColorAllocate($image, 0,0,113);
28.
$colorList[] = ImageColorAllocate($image, 0,51,51);
29.
$colorList[] = ImageColorAllocate($image, 158,180,35);
30.
$colorList[] = ImageColorAllocate($image, 59,59,59);
31.
$colorList[] = ImageColorAllocate($image, 0,0,0);
32.
$colorList[] = ImageColorAllocate($image, 1,128,180);
33.
$colorList[] = ImageColorAllocate($image, 0,153,51);
34.
$colorList[] = ImageColorAllocate($image, 60,131,1);
35.
$colorList[] = ImageColorAllocate($image, 0,0,0);
36.
$fontcolor = ImageColorAllocate($image, 0,0,0);
37.
$gray = ImageColorAllocate($image, 245,245,245);
38.
39.
$color = imagecolorallocate($image, 255,255,255);
40.
$color2 = imagecolorallocate($image, 255,0,0);
41.
42.
imagefill($image, 0, 0, $gray);
43.
44.
$space = 15; // 字符间距
45.
if($i > 0) // 屏蔽第一帧
46.
{
47.
for ($k = 0; $k < strlen($authstr); $k++)
48.
{
49.
$colorRandom = mt_rand(0,sizeof($colorList)-1);
50.
$float_top = rand(0,4);
51.
$float_left = rand(0,3);
52.
imagestring($image, 6, $space * $k, $top + $float_top, substr($authstr, $k, 1), $colorList[$colorRandom]);
53.
}
54.
}
55.
56.
for ($k = 0; $k < 20; $k++)
57.
{
58.
$colorRandom = mt_rand(0,sizeof($colorList)-1);
59.
imagesetpixel($image, rand()%70 , rand()%15 , $colorList[$colorRandom]);
60.
61.
}
62.
// 添加干扰线
63.
for($k = 0; $k < 3; $k++)
64.
{
65.
$colorRandom = mt_rand(0, sizeof($colorList)-1);
66.
// $todrawline = rand(0,1);
67.
$todrawline = 1;
68.
if($todrawline)
69.
{
70.
imageline($image, mt_rand(0, $board_width), mt_rand(0,$board_height), mt_rand(0,$board_width), mt_rand(0,$board_height), $colorList[$colorRandom]);
71.
}
72.
else
73.
{
74.
$w = mt_rand(0,$board_width);
75.
$h = mt_rand(0,$board_width);
76.
imagearc($image, $board_width - floor($w / 2) , floor($h / 2), $w, $h,rand(90,180), rand(180,270), $colorList[$colorRandom]);
77.
}
78.
}
79.
imagegif($image);
80.
imagedestroy($image);
81.
$imagedata[] = ob_get_contents();
82.
ob_clean();
83.
++$i;
84.
}
85.
86.
$gif = new GIFEncoder($imagedata);
87.
Header ('Content-type:image/gif');
88.
echo $gif->GetAnimation();
89.
}
以上是本文关于PHP 生成GIF动画实现动态图片验证码的详细叙述,希望本文对广大php开发者有所帮助,感谢阅读本文。
页:
[1]