网页GIF动态图片验证码制作方法

海外服务器 (353) 2015-11-03 13:42:31

相信很多人都想过如何用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_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();    
THE END