PHP实现GIF图片验证码

这篇文章主要介绍了PHP如何生成GIF动态图片验证码,在注册界面时大家经常用到验证码,需要了解的朋友可以参考下。

这是一个用PHP生成GIF动画来实现动态图片验证码的程序,非常实用,是php用户注册登录界面经常需要用到的验证码程序,有需要GIF动态图片验证码功能的朋友可以收藏一下。

第一步:创建ImageCode.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. {
  10. $authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 99999));
  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. $color = imagecolorallocate($image, 255,255,255);
  39. $color2 = imagecolorallocate($image, 255,0,0);
  40. imagefill($image, 0, 0, $gray);
  41. $space = 15; // 字符间距
  42. if($i > 0) // 屏蔽第一帧
  43. {
  44. for ($k = 0; $k < strlen($authstr); $k++)
  45. {
  46. $colorRandom = mt_rand(0,sizeof($colorList)-1);
  47. $float_top = rand(0,4);
  48. $float_left = rand(0,3);
  49. imagestring($image, 6, $space * $k, $top + $float_top, substr($authstr, $k, 1), $colorList[$colorRandom]);
  50. }
  51. }
  52. for ($k = 0; $k < 20; $k++)
  53. {
  54. $colorRandom = mt_rand(0,sizeof($colorList)-1);
  55. imagesetpixel($image, rand()%70 , rand()%15 , $colorList[$colorRandom]);
  56. }
  57. // 添加干扰线
  58. for($k = 0; $k < 3; $k++)
  59. {
  60. $colorRandom = mt_rand(0, sizeof($colorList)-1);
  61. // $todrawline = rand(0,1);
  62. $todrawline = 1;
  63. if($todrawline)
  64. {
  65. imageline($image, mt_rand(0, $board_width), mt_rand(0,$board_height), mt_rand(0,$board_width), mt_rand(0,$board_height), $colorList[$colorRandom]);
  66. }
  67. else
  68. {
  69. $w = mt_rand(0,$board_width);
  70. $h = mt_rand(0,$board_width);
  71. imagearc($image, $board_width - floor($w / 2) , floor($h / 2), $w, $h, rand(90,180), rand(180,270), $colorList[$colorRandom]);
  72. }
  73. }
  74. imagegif($image);
  75. imagedestroy($image);
  76. $imagedata[] = ob_get_contents();
  77. ob_clean();
  78. ++$i;
  79. }
  80. $gif = new GIFEncoder($imagedata);
  81. Header ('Content-type:image/gif');
  82. echo $gif->GetAnimation();
  83. }
  84. /**
  85. * GIFEncoder类
  86. * */
  87. Class GIFEncoder
  88. {
  89. var $GIF = "GIF89a"; /* GIF header 6 bytes */
  90. var $VER = "GIFEncoder V2.06"; /* Encoder version */
  91. var $BUF = Array ( );
  92. var $LOP = 0;
  93. var $DIS = 2;
  94. var $COL = -1;
  95. var $IMG = -1;
  96. var $ERR = Array (
  97. 'ERR00' =>"Does not supported function for only one image!",
  98. 'ERR01' =>"Source is not a GIF image!",
  99. 'ERR02' =>"Unintelligible flag ",
  100. 'ERR03' =>"Could not make animation from animated GIF source",
  101. );
  102. 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' )
  103. {
  104. if (!is_array($GIF_src) && !is_array($GIF_tim))
  105. {
  106. printf ( "%s: %s", $this->VER, $this->ERR['ERR00']);
  107. exit( 0 );
  108. }
  109. $this->LOP = ($GIF_lop > -1) ? $GIF_lop : 0;
  110. $this->DIS = ($GIF_dis > -1) ? (( $GIF_dis < 3 ) ? $GIF_dis : 3) : 2;
  111. $this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
  112. for ($i = 0, $src_count = count($GIF_src); $i < $src_count; $i++ )
  113. {
  114. if (strToLower( $GIF_mod ) == "url")
  115. {
  116. $this->BUF[] = fread (fopen($GIF_src [$i], "rb"), filesize ($GIF_src [$i]));
  117. }
  118. elseif(strToLower($GIF_mod) == "bin")
  119. {
  120. $this->BUF [ ] = $GIF_src [ $i ];
  121. }
  122. else
  123. {
  124. printf("%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod);
  125. exit(0);
  126. }
  127. if (substr($this->BUF[$i], 0, 6) != "GIF87a" && substr($this->BUF [$i], 0, 6) != "GIF89a")
  128. {
  129. printf( "%s: %d %s", $this->VER, $i, $this->ERR ['ERR01']);
  130. exit(0);
  131. }
  132. for ($j = (13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07 ))), $k = TRUE; $k; $j++)
  133. {
  134. switch ($this->BUF [$i]{$j})
  135. {
  136. case "!":
  137. if ((substr($this->BUF[$i], ($j + 3), 8)) == "NETSCAPE")
  138. {
  139. printf( "%s: %s ( %s source )!", $this->VER, $this->ERR ['ERR03'], ($i + 1));
  140. exit( 0 );
  141. }
  142. break;
  143. case ";":
  144. $k = FALSE;
  145. break;
  146. }
  147. }
  148. }
  149. GIFEncoder::GIFAddHeader();
  150. for($i = 0, $count_buf = count($this->BUF); $i < $count_buf; $i++)
  151. {
  152. GIFEncoder::GIFAddFrames($i, $GIF_dly[$i]);
  153. }
  154. GIFEncoder::GIFAddFooter();
  155. }
  156. function GIFAddHeader ( )
  157. {
  158. $cmap = 0;
  159. if (ord($this->BUF[0]{10}) & 0x80 )
  160. {
  161. $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ]{10} ) & 0x07 ));
  162. $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7);
  163. $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap);
  164. $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";
  165. }
  166. }
  167. function GIFAddFrames ( $i, $d )
  168. {
  169. $Locals_str = 13 + 3 * (2 <<(ord($this->BUF[$i]{10}) & 0x07));
  170. $Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;
  171. $Locals_tmp = substr ($this->BUF[$i], $Locals_str, $Locals_end);
  172. $Global_len = 2 << (ord( $this->BUF [0]{10} ) & 0x07 );
  173. $Locals_len = 2 << (ord( $this->BUF[$i]{10}) & 0x07);
  174. $Global_rgb = substr($this->BUF[0], 13, 3 * (2 << ( ord ( $this->BUF[0]{10} ) & 0x07)));
  175. $Locals_rgb = substr ( $this->BUF[$i], 13, 3 * (2 << ( ord ( $this->BUF[$i]{10} ) & 0x07)));
  176. $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 0) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
  177. if ( $this->COL > -1 && ord($this->BUF[$i]{10}) & 0x80)
  178. {
  179. for($j = 0; $j < (2 << (ord( $this->BUF[$i]{10}) & 0x07)); $j++ )
  180. {
  181. 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 )
  182. {
  183. $Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 1) . chr (( $d >> 0) & 0xFF) . chr (( $d >> 8) & 0xFF) . chr ($j) . "\x0";
  184. break;
  185. }
  186. }
  187. }
  188. switch ( $Locals_tmp { 0 } )
  189. {
  190. case "!":
  191. $Locals_img = substr($Locals_tmp, 8, 10);
  192. $Locals_tmp = substr($Locals_tmp, 18, strlen ($Locals_tmp) - 18);
  193. break;
  194. case ",":
  195. $Locals_img = substr($Locals_tmp, 0, 10);
  196. $Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
  197. break;
  198. }
  199. if ( ord ( $this->BUF[$i]{10} ) & 0x80 && $this->IMG > -1 )
  200. {
  201. if ( $Global_len == $Locals_len )
  202. {
  203. if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) )
  204. {
  205. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  206. }
  207. else
  208. {
  209. $byte = ord ( $Locals_img{9});
  210. $byte |= 0x80;
  211. $byte &= 0xF8;
  212. $byte |= ( ord ( $this->BUF [ 0 ]{10}) & 0x07);
  213. $Locals_img{9} = chr($byte);
  214. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);
  215. }
  216. }
  217. else
  218. {
  219. $byte = ord($Locals_img{9});
  220. $byte |= 0x80;
  221. $byte &= 0xF8;
  222. $byte |= (ord($this->BUF[$i]{10}) & 0x07);
  223. $Locals_img {9} = chr($byte);
  224. $this->GIF .= ($Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp);
  225. }
  226. }
  227. else
  228. {
  229. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  230. }
  231. $this->IMG = 1;
  232. }
  233. function GIFAddFooter ( ) {
  234. $this->GIF .= ";";
  235. }
  236. function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len )
  237. {
  238. for ( $i = 0; $i < $Len; $i++ )
  239. {
  240. 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})
  241. {
  242. return ( 0 );
  243. }
  244. }
  245. return ( 1 );
  246. }
  247. function GIFWord ( $int )
  248. {
  249. return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) );
  250. }
  251. function GetAnimation ( )
  252. {
  253. return ($this->GIF);
  254. }
  255. }

第二步:验证码生成,创建yzimg.php,并引用入文件ImageCode.php,代码如下:

  1. <?php
  2. if(!isset($_SESSION)){
  3. session_start();
  4. }
  5. require_once(dirname(__FILE__)."ImageCode.php");
  6. unset($_SESSION['yzm']);
  7. $randCode = '';
  8. $chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789';
  9. for ( $i = 0; $i < 4; $i++ )
  10. {
  11. $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  12. }
  13. $_SESSION['yzm']=$randCode; // 记录session
  14. ImageCode($randCode, 60);
  15. /*方法二*/
  16. /*$authstr = mt_rand(1000, 9999); // 生成随机字符串
  17. $_SESSION['yzm'] = $authstr; // 记录session
  18. ImageCode($authstr, 60); // 显示GIF动画*/
  19. ?>

第三步:调用和显示验证码,在需要添加验证码的页面加入以下代码,代码如下:

  1. <input name="yzm" type="text" class="input1" size="12" />
  2. <SCRIPT LANGUAGE="JavaScript">
  3. <!--
  4. document.write('<img name="imageField" class="yz_Img" title="刷新验证码" onclick="this.src=this.src+Math.random();" src="yzimg.php?'+Math.random()+'">');
  5. //-->
  6. </SCRIPT>

以上就是为大家分享的如何使用php生成动态验证码的相应代码,希望对大家的学习有所帮助。