PHP生成GIF动态图片验证码

ImageCode函数通过GIFEncoder类实现的GIF动画的PHP源代码,有兴趣的朋友可以研究一下,PHP生成GIF动态图片验证码实例代码如下:

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