PHP生成GIF动态图片验证码

开发 后端
相信很多人都想过如何用PHP生成GIF动画来实现动态图片验证码,以下是实现过程。ImageCode函数通过GIFEncoder类实现的GIF动画的PHP源代码,有兴趣的朋友可以研究一下。

相信很多人都想过如何用PHP生成GIF动画来实现动态图片验证码,以下是实现过程。

ImageCode函数通过GIFEncoder类实现的GIF动画的PHP源代码,有兴趣的朋友可以研究一下。

效果如图: 点击查看原图

  1. /**  
  2.  * ImageCode 生成GIF图片验证  
  3.  * @param $string 字符串  
  4.  * @param $width 宽度  
  5.  * @param $height 高度  
  6.  * */ 
  7. function ImageCode($string = ''$width = 75, $height = 25)  
  8. {  
  9.     $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_topsubstr($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();    

有关GIFEncoder类,请下载本文附件:animation.ra

附件下载:animation.rar 2.96KB

原文链接:http://www.phper.org.cn/?post=116

责任编辑:张伟 来源: ITEYE
相关推荐

2024-01-29 08:32:10

Python验证码识别

2009-11-23 16:34:22

PHP GD库

2015-09-21 15:31:05

php实现验证码

2009-12-11 15:17:52

PHP验证码调用

2009-11-23 16:59:23

PHP图形验证码

2009-11-26 10:48:59

PHP验证码

2009-08-06 16:30:58

C#代码和验证码图片

2023-10-27 08:53:13

Python验证码图片识别

2021-08-11 09:51:24

人工智能机器学习技术

2019-08-23 09:52:59

Axure 产品经理

2013-06-19 10:19:59

2015-03-18 10:41:34

图片验证验证码12306

2009-12-08 11:16:07

PHP动态图像创建

2022-02-11 07:10:15

验证码

2021-01-19 10:29:34

短信验证码密码

2015-03-23 17:58:04

验证码倒计时并行

2017-12-21 07:38:19

2010-01-11 14:16:14

VB.NET生成验证码

2020-11-16 07:28:53

验证码
点赞
收藏

51CTO技术栈公众号