php生成动态验证码图片(gif)

发一个真正能运行的通过php生成的动态验证码图片的示例。网上下载的好多都报错的,于是自己综合各方面的见解,终于完成了这么一个效果。下面先发下效果图:

php生成动态(gif)验证码图片

下面是php生成动态验证码需要用到的相关类和函数。

  1. <?php 
  2. /** 
  3. *ImageCode 生成包含验证码的GIF图片的函数 
  4. *@param $string 字符串 
  5. *@param $width 宽度 
  6. *@param $height 高度 
  7. **/ 
  8. function ImageCode($string='',$width=75,$height=25){ 
  9.     $authstr=$string?$string:((time()%2==0)?mt_rand(1000,9999):mt_rand(10000,99999)); 
  10.     $board_width=$width
  11.     $board_height=$height
  12.     // 生成一个32帧的GIF动画 
  13.     for($i=0;$i<32;$i++){ 
  14.         ob_start(); 
  15.         $image=imagecreate($board_width,$board_height); 
  16.         imagecolorallocate($image,0,0,0); 
  17.         // 设定文字颜色数组  
  18.         $colorList[]=ImageColorAllocate($image,15,73,210); 
  19.         $colorList[]=ImageColorAllocate($image,0,64,0); 
  20.         $colorList[]=ImageColorAllocate($image,0,0,64); 
  21.         $colorList[]=ImageColorAllocate($image,0,128,128); 
  22.         $colorList[]=ImageColorAllocate($image,27,52,47); 
  23.         $colorList[]=ImageColorAllocate($image,51,0,102); 
  24.         $colorList[]=ImageColorAllocate($image,0,0,145); 
  25.         $colorList[]=ImageColorAllocate($image,0,0,113); 
  26.         $colorList[]=ImageColorAllocate($image,0,51,51); 
  27.         $colorList[]=ImageColorAllocate($image,158,180,35); 
  28.         $colorList[]=ImageColorAllocate($image,59,59,59); 
  29.         $colorList[]=ImageColorAllocate($image,0,0,0); 
  30.         $colorList[]=ImageColorAllocate($image,1,128,180); 
  31.         $colorList[]=ImageColorAllocate($image,0,153,51); 
  32.         $colorList[]=ImageColorAllocate($image,60,131,1); 
  33.         $colorList[]=ImageColorAllocate($image,0,0,0); 
  34.         $fontcolor=ImageColorAllocate($image,0,0,0); 
  35.         $gray=ImageColorAllocate($image,245,245,245); 
  36.         $color=imagecolorallocate($image,255,255,255); 
  37.         $color2=imagecolorallocate($image,255,0,0); 
  38.         imagefill($image,0,0,$gray); 
  39.         $space=15;// 字符间距 
  40.         if($i>0){// 屏蔽第一帧 
  41.             $top=0; 
  42.             for($k=0;$k<strlen($authstr);$k++){  
  43.                 $colorRandom=mt_rand(0,sizeof($colorList)-1); 
  44.                 $float_top=rand(0,4); 
  45.                 $float_left=rand(0,3); 
  46.                 imagestring($image,6,$space*$k,$top+$float_top,substr($authstr,$k,1),$colorList[$colorRandom]); 
  47.             } 
  48.        } 
  49.         for($k=0;$k<20;$k++){  
  50.             $colorRandom=mt_rand(0,sizeof($colorList)-1); 
  51.             imagesetpixel($image,rand()%70,rand()%15,$colorList[$colorRandom]); 
  52.      
  53.         } 
  54.         // 添加干扰线 
  55.         for($k=0;$k<3;$k++){ 
  56.             $colorRandom=mt_rand(0,sizeof($colorList)-1); 
  57.             $todrawline=1; 
  58.             if($todrawline){ 
  59.                 imageline($image,mt_rand(0,$board_width),mt_rand(0,$board_height),mt_rand(0,$board_width),mt_rand(0,$board_height),$colorList[$colorRandom]); 
  60.             }else
  61.                 $w=mt_rand(0,$board_width); 
  62.                 $h=mt_rand(0,$board_width); 
  63.                 imagearc($image,$board_width-floor($w / 2),floor($h / 2),$w,$h, rand(90,180),rand(180,270),$colorList[$colorRandom]); 
  64.             } 
  65.         } 
  66.         imagegif($image); 
  67.         imagedestroy($image); 
  68.         $imagedata[]=ob_get_contents(); 
  69.         ob_clean(); 
  70.         ++$i
  71.     } 
  72.     $gif=new GIFEncoder($imagedata); 
  73.     Header('Content-type:image/gif'); 
  74.     echo $gif->GetAnimation(); 
  75.  
  76.  
  77. /** 
  78. *GIFEncoder类 
  79. **/ 
  80. Class GIFEncoder{ 
  81.     var $GIF="GIF89a";              /* GIF header 6 bytes       */   
  82.     var $VER="GIFEncoder V2.06";      /* Encoder version            */   
  83.     var $BUF=Array(); 
  84.     var $LOP=0; 
  85.     var $DIS=2; 
  86.     var $COL=-1; 
  87.     var $IMG=-1; 
  88.     var $ERR=Array( 
  89.         'ERR00'=>"Does not supported function for only one image!",  
  90.         'ERR01'=>"Source is not a GIF image!",  
  91.         'ERR02'=>"Unintelligible flag ",  
  92.         'ERR03'=>"Could not make animation from animated GIF source",  
  93.    ); 
  94.     function GIFEncoder($GIF_src,$GIF_dly=100,$GIF_lop=0,$GIF_dis=0, $GIF_red=0,$GIF_grn=0,$GIF_blu=0,$GIF_mod='bin'){ 
  95.         if(!is_array($GIF_src)&&!is_array($GIF_tim)){ 
  96.             printf("%s: %s",$this->VER,$this->ERR['ERR00']); 
  97.             exit(0); 
  98.         }   
  99.         $this->LOP=($GIF_lop>-1)?$GIF_lop:0; 
  100.         $this->DIS=($GIF_dis>-1)?(($GIF_dis<3)?$GIF_dis:3):2; 
  101.         $this->COL=($GIF_red>-1&&$GIF_grn>-1&&$GIF_blu>-1)?($GIF_red |($GIF_grn<<8)|($GIF_blu<<16)):-1; 
  102.  
  103.         for($i=0,$src_count=count($GIF_src);$i<$src_count;$i++){ 
  104.             if(strToLower($GIF_mod)=="url"){ 
  105.                 $this->BUF[]=fread(fopen($GIF_src [$i],"rb"),filesize($GIF_src [$i])); 
  106.            }elseif(strToLower($GIF_mod)=="bin"){ 
  107.                 $this->BUF [ ]=$GIF_src [ $i ]; 
  108.            }else
  109.                 printf("%s: %s(%s)!",$this->VER,$this->ERR [ 'ERR02' ],$GIF_mod); 
  110.                 exit(0); 
  111.            }   
  112.             if(substr($this->BUF[$i],0,6)!="GIF87a"&&substr($this->BUF [$i],0,6)!="GIF89a"){ 
  113.                 printf("%s: %d %s",$this->VER,$i,$this->ERR ['ERR01']); 
  114.                 exit(0); 
  115.            }   
  116.             for($j=(13+3*(2<<(ord($this->BUF[$i]{10})&0x07))),$k=TRUE;$k;$j++){ 
  117.                 switch($this->BUF [$i]{$j}){ 
  118.                     case "!":   
  119.                         if((substr($this->BUF[$i],($j+3),8))=="NETSCAPE"){ 
  120.                                 printf("%s: %s(%s source)!",$this->VER,$this->ERR ['ERR03'],($i+1)); 
  121.                                 exit(0); 
  122.                        }   
  123.                         break
  124.                     case ";":   
  125.                         $k=FALSE; 
  126.                     break
  127.                }   
  128.            }   
  129.         }   
  130.         GIFEncoder::GIFAddHeader(); 
  131.         for($i=0,$count_buf=count($this->BUF);$i<$count_buf;$i++){ 
  132.             GIFEncoder::GIFAddFrames($i,$GIF_dly[$i]); 
  133.        }   
  134.         GIFEncoder::GIFAddFooter(); 
  135.     }   
  136.     function GIFAddHeader(){ 
  137.         $cmap=0; 
  138.         if(ord($this->BUF[0]{10})&0x80){ 
  139.             $cmap=3*(2<<(ord($this->BUF [0]{10})&0x07)); 
  140.             $this->GIF.=substr($this->BUF [0],6,7); 
  141.             $this->GIF.=substr($this->BUF [0],13,$cmap); 
  142.             $this->GIF.="!\377\13NETSCAPE2.0\3\1".GIFEncoder::GIFWord($this->LOP)."\0"
  143.         }   
  144.     }   
  145.     function GIFAddFrames($i,$d){ 
  146.         $Locals_str=13+3*(2 <<(ord($this->BUF[$i]{10})&0x07)); 
  147.         $Locals_end=strlen($this->BUF[$i])-$Locals_str-1; 
  148.         $Locals_tmp=substr($this->BUF[$i],$Locals_str,$Locals_end); 
  149.         $Global_len=2<<(ord($this->BUF [0]{10})&0x07); 
  150.         $Locals_len=2<<(ord($this->BUF[$i]{10})&0x07); 
  151.         $Global_rgb=substr($this->BUF[0],13,3*(2<<(ord($this->BUF[0]{10})&0x07))); 
  152.         $Locals_rgb=substr($this->BUF[$i],13,3*(2<<(ord($this->BUF[$i]{10})&0x07))); 
  153.         $Locals_ext="!\xF9\x04".chr(($this->DIS<<2)+0).chr(($d>>0)&0xFF).chr(($d>>8)&0xFF)."\x0\x0"
  154.         if($this->COL>-1&&ord($this->BUF[$i]{10})&0x80){ 
  155.             for($j=0;$j<(2<<(ord($this->BUF[$i]{10})&0x07));$j++){ 
  156.                 if(ord($Locals_rgb{3*$j+0})==($this->COL>> 0)&0xFF&&ord($Locals_rgb{3*$j+1})==($this->COL>> 8)&0xFF&&ord($Locals_rgb{3*$j+2})==($this->COL>>16)&0xFF){ 
  157.                     $Locals_ext="!\xF9\x04".chr(($this->DIS<<2)+1).chr(($d>>0)&0xFF).chr(($d>>8)&0xFF).chr($j)."\x0"
  158.                     break
  159.                }   
  160.            }   
  161.         }   
  162.         switch($Locals_tmp{0}){ 
  163.             case "!":   
  164.                 $Locals_img=substr($Locals_tmp,8,10); 
  165.                 $Locals_tmp=substr($Locals_tmp,18,strlen($Locals_tmp)-18); 
  166.                 break
  167.             case ",":   
  168.                 $Locals_img=substr($Locals_tmp,0,10); 
  169.                 $Locals_tmp=substr($Locals_tmp,10,strlen($Locals_tmp)-10); 
  170.                 break
  171.         }   
  172.         if(ord($this->BUF[$i]{10})&0x80&&$this->IMG>-1){ 
  173.             if($Global_len==$Locals_len){ 
  174.                 if(GIFEncoder::GIFBlockCompare($Global_rgb,$Locals_rgb,$Global_len)){ 
  175.                     $this->GIF.=($Locals_ext.$Locals_img.$Locals_tmp); 
  176.                 }else
  177.                     $byte=ord($Locals_img{9}); 
  178.                     $byte|=0x80; 
  179.                     $byte&=0xF8; 
  180.                     $byte|=(ord($this->BUF [0]{10})&0x07); 
  181.                     $Locals_img{9}=chr($byte); 
  182.                     $this->GIF.=($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp); 
  183.                 }   
  184.             }else
  185.                 $byte=ord($Locals_img{9}); 
  186.                 $byte|=0x80; 
  187.                 $byte&=0xF8; 
  188.                 $byte|=(ord($this->BUF[$i]{10})&0x07); 
  189.                 $Locals_img {9}=chr($byte); 
  190.                 $this->GIF.=($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp); 
  191.             }   
  192.         }else
  193.             $this->GIF.=($Locals_ext.$Locals_img.$Locals_tmp); 
  194.         }   
  195.         $this->IMG=1; 
  196.     }   
  197.     function GIFAddFooter(){ 
  198.         $this->GIF.=";"
  199.     }   
  200.     function GIFBlockCompare($GlobalBlock,$LocalBlock,$Len){ 
  201.         for($i=0;$i<$Len;$i++){ 
  202.             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}){ 
  203.                 return(0); 
  204.            }   
  205.        }   
  206.         return(1); 
  207.     }   
  208.     function GIFWord($int){ 
  209.        return(chr($int&0xFF).chr(($int>>8)&0xFF)); 
  210.     }   
  211.     function GetAnimation(){ 
  212.         return($this->GIF); 
  213.     }   

php生成动态验证码用法示例:

  1. /*调用示例*/ 
  2. session_start(); 
  3. $checkCode=''
  4. $chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789'
  5. for($i=0;$i<4;$i++){ 
  6.     $checkCode.=substr($chars,mt_rand(0,strlen($chars)-1),1); 
  7. $_SESSION['code']=strtoupper($checkCode);// 记录session 
  8. ImageCode($checkCode,60);// 显示GIF动画 

转载请注明:代码家园 » php生成动态验证码图片(gif)

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)