php生成gif动画的方法

这篇文章主要介绍了php合成或者创建gif动画,并提供了两个实例,感兴趣的小伙伴们可以参考一下。

首先需要确认GD库是否正常,如果是合成图片,请确保把分解的图片放在frames的文件夹里面。

GIFEncoder.class.php 类

  1. <?
  2. Class GIFEncoder {
  3. var $GIF = "GIF89a"; /* GIF header 6 bytes */
  4. var $VER = "GIFEncoder V2.06"; /* Encoder version */
  5. var $BUF = Array ( );
  6. var $LOP = 0;
  7. var $DIS = 2;
  8. var $COL = -1;
  9. var $IMG = -1;
  10. var $ERR = Array (
  11. 'ERR00' =>"Does not supported function for only one image!",
  12. 'ERR01' =>"Source is not a GIF image!",
  13. 'ERR02' =>"Unintelligible flag ",
  14. 'ERR03' =>"Could not make animation from animated GIF source",
  15. );
  16. /*
  17. :::::::::::::::::::::::::::::::::::::::::::::::::::
  18. ::
  19. :: GIFEncoder...
  20. ::
  21. */
  22. function GIFEncoder (
  23. $GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,
  24. $GIF_red, $GIF_grn, $GIF_blu, $GIF_mod
  25. ) {
  26. if ( ! is_array ( $GIF_src ) && ! is_array ( $GIF_tim ) ) {
  27. printf ( "%s: %s", $this->VER, $this->ERR [ 'ERR00' ] );
  28. exit ( 0 );
  29. }
  30. $this->LOP = ( $GIF_lop > -1 ) ? $GIF_lop : 0;
  31. $this->DIS = ( $GIF_dis > -1 ) ? ( ( $GIF_dis < 3 ) ? $GIF_dis : 3 ) : 2;
  32. $this->COL = ( $GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1 ) ?
  33. ( $GIF_red | ( $GIF_grn << 8 ) | ( $GIF_blu << 16 ) ) : -1;
  34. for ( $i = 0; $i < count ( $GIF_src ); $i++ ) {
  35. if ( strToLower ( $GIF_mod ) == "url" ) {
  36. $this->BUF [ ] = fread ( fopen ( $GIF_src [ $i ], "rb" ), filesize ( $GIF_src [ $i ] ) );
  37. }
  38. else if ( strToLower ( $GIF_mod ) == "bin" ) {
  39. $this->BUF [ ] = $GIF_src [ $i ];
  40. }
  41. else {
  42. printf ( "%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod );
  43. exit ( 0 );
  44. }
  45. if ( substr ( $this->BUF [ $i ], 0, 6 ) != "GIF87a" && substr ( $this->BUF [ $i ], 0, 6 ) != "GIF89a" ) {
  46. printf ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] );
  47. exit ( 0 );
  48. }
  49. for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = TRUE; $k; $j++ ) {
  50. switch ( $this->BUF [ $i ] { $j } ) {
  51. case "!":
  52. if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) {
  53. printf ( "%s: %s ( %s source )!", $this->VER, $this->ERR [ 'ERR03' ], ( $i + 1 ) );
  54. exit ( 0 );
  55. }
  56. break;
  57. case ";":
  58. $k = FALSE;
  59. break;
  60. }
  61. }
  62. }
  63. GIFEncoder::GIFAddHeader ( );
  64. for ( $i = 0; $i < count ( $this->BUF ); $i++ ) {
  65. GIFEncoder::GIFAddFrames ( $i, $GIF_dly [ $i ] );
  66. }
  67. GIFEncoder::GIFAddFooter ( );
  68. }
  69. /*
  70. :::::::::::::::::::::::::::::::::::::::::::::::::::
  71. ::
  72. :: GIFAddHeader...
  73. ::
  74. */
  75. function GIFAddHeader ( ) {
  76. $cmap = 0;
  77. if ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x80 ) {
  78. $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) );
  79. $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7 );
  80. $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap );
  81. $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";
  82. }
  83. }
  84. /*
  85. :::::::::::::::::::::::::::::::::::::::::::::::::::
  86. ::
  87. :: GIFAddFrames...
  88. ::
  89. */
  90. function GIFAddFrames ( $i, $d ) {
  91. $Locals_str = 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) );
  92. $Locals_end = strlen ( $this->BUF [ $i ] ) - $Locals_str - 1;
  93. $Locals_tmp = substr ( $this->BUF [ $i ], $Locals_str, $Locals_end );
  94. $Global_len = 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
  95. $Locals_len = 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  96. $Global_rgb = substr ( $this->BUF [ 0 ], 13,
  97. 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) ) );
  98. $Locals_rgb = substr ( $this->BUF [ $i ], 13,
  99. 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) );
  100. $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
  101. chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "\x0\x0";
  102. if ( $this->COL > -1 && ord ( $this->BUF [ $i ] { 10 } ) & 0x80 ) {
  103. for ( $j = 0; $j < ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $j++ ) {
  104. if (
  105. ord ( $Locals_rgb { 3 * $j + 0 } ) == ( $this->COL >> 0 ) & 0xFF &&
  106. ord ( $Locals_rgb { 3 * $j + 1 } ) == ( $this->COL >> 8 ) & 0xFF &&
  107. ord ( $Locals_rgb { 3 * $j + 2 } ) == ( $this->COL >> 16 ) & 0xFF
  108. ) {
  109. $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 1 ) .
  110. chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . chr ( $j ) . "\x0";
  111. break;
  112. }
  113. }
  114. }
  115. switch ( $Locals_tmp { 0 } ) {
  116. case "!":
  117. $Locals_img = substr ( $Locals_tmp, 8, 10 );
  118. $Locals_tmp = substr ( $Locals_tmp, 18, strlen ( $Locals_tmp ) - 18 );
  119. break;
  120. case ",":
  121. $Locals_img = substr ( $Locals_tmp, 0, 10 );
  122. $Locals_tmp = substr ( $Locals_tmp, 10, strlen ( $Locals_tmp ) - 10 );
  123. break;
  124. }
  125. if ( ord ( $this->BUF [ $i ] { 10 } ) & 0x80 && $this->IMG > -1 ) {
  126. if ( $Global_len == $Locals_len ) {
  127. if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) ) {
  128. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  129. }
  130. else {
  131. $byte = ord ( $Locals_img { 9 } );
  132. $byte |= 0x80;
  133. $byte &= 0xF8;
  134. $byte |= ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
  135. $Locals_img { 9 } = chr ( $byte );
  136. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  137. }
  138. }
  139. else {
  140. $byte = ord ( $Locals_img { 9 } );
  141. $byte |= 0x80;
  142. $byte &= 0xF8;
  143. $byte |= ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  144. $Locals_img { 9 } = chr ( $byte );
  145. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  146. }
  147. }
  148. else {
  149. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  150. }
  151. $this->IMG = 1;
  152. }
  153. /*
  154. :::::::::::::::::::::::::::::::::::::::::::::::::::
  155. ::
  156. :: GIFAddFooter...
  157. ::
  158. */
  159. function GIFAddFooter ( ) {
  160. $this->GIF .= ";";
  161. }
  162. /*
  163. :::::::::::::::::::::::::::::::::::::::::::::::::::
  164. ::
  165. :: GIFBlockCompare...
  166. ::
  167. */
  168. function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len ) {
  169. for ( $i = 0; $i < $Len; $i++ ) {
  170. if (
  171. $GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } ||
  172. $GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } ||
  173. $GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 }
  174. ) {
  175. return ( 0 );
  176. }
  177. }
  178. return ( 1 );
  179. }
  180. /*
  181. :::::::::::::::::::::::::::::::::::::::::::::::::::
  182. ::
  183. :: GIFWord...
  184. ::
  185. */
  186. function GIFWord ( $int ) {
  187. return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) );
  188. }
  189. /*
  190. :::::::::::::::::::::::::::::::::::::::::::::::::::
  191. ::
  192. :: GetAnimation...
  193. ::
  194. */
  195. function GetAnimation ( ) {
  196. return ( $this->GIF );
  197. }
  198. }
  199. ?>

为大家分享两个实例供大家参考:

实例 1 合成gif动画:

  1. <?php
  2. include "GIFEncoder.class.php";
  3. /*
  4. Build a frames array from sources...
  5. */
  6. if ( $dh = opendir ( "frames/" ) ) {
  7. while ( false !== ( $dat = readdir ( $dh ) ) ) {
  8. if ( $dat != "." && $dat != ".." ) {
  9. $frames [ ] = "frames/$dat";
  10. $framed [ ] = 5;
  11. }
  12. }
  13. closedir ( $dh );
  14. }
  15. /*
  16. GIFEncoder constructor:
  17. =======================
  18. image_stream = new GIFEncoder (
  19. URL or Binary data 'Sources'
  20. int 'Delay times'
  21. int 'Animation loops'
  22. int 'Disposal'
  23. int 'Transparent red, green, blue colors'
  24. int 'Source type'
  25. );
  26. */
  27. $gif = new GIFEncoder (
  28. $frames,
  29. $framed,
  30. 0,
  31. 2,
  32. 0, 0, 0,
  33. "url"
  34. );
  35. /*
  36. Possibles outputs:
  37. ==================
  38. Output as GIF for browsers :
  39. - Header ( 'Content-type:image/gif' );
  40. Output as GIF for browsers with filename:
  41. - Header ( 'Content-disposition:Attachment;filename=myanimation.gif');
  42. Output as file to store into a specified file:
  43. - FWrite ( FOpen ( "myanimation.gif", "wb" ), $gif->GetAnimation ( ) );
  44. */
  45. Header ( 'Content-type:image/gif' );
  46. echo $gif->GetAnimation ( );
  47. ?>

实例 2 创建gif动画:

  1. <?php
  2. include "GIFEncoder.class.php";
  3. ob_start();
  4. $board_width = 60;
  5. $board_height = 60;
  6. $pad_width = 5;
  7. $pad_height = 15;
  8. $ball_size = 5;
  9. $game_width = $board_width - $pad_width*2 - $ball_size;
  10. $game_height = $board_height-$ball_size;
  11. $x = 0;
  12. $y = rand(0,$game_height);
  13. $xv = rand(1,10);
  14. $yv = rand(1,10);
  15. $pt[] = array($x,$y);
  16. do{
  17. $x += $xv;
  18. $y += $yv;
  19. if($x > $game_width){
  20. $xv = -1*$xv;
  21. $x = $game_width - ($x-$game_width);
  22. }elseif($x < 0){
  23. $xv = -1*$xv;
  24. $x = abs($x);
  25. }
  26. if($y>$game_height){
  27. $yv = -1*$yv;
  28. $y = $game_height - ($y - $game_height);
  29. }elseif($y<0){
  30. $yv = -1*$yv;
  31. $y = abs($y);
  32. }
  33. $pt[] = array($x,$y);
  34. }while($x!=$pt[0][0]||$y!=$pt[0][1]);
  35. $i = 0;
  36. while(isset($pt[$i])){
  37. $image = imagecreate($board_width,$board_height);
  38. imagecolorallocate($image, 0,0,0);
  39. $color = imagecolorallocate($image, 255,255,255);
  40. $color2 = imagecolorallocate($image, 255,0,0);
  41. if($pt[$i][1] + $pad_height < $board_width){
  42. imagefilledrectangle($image,0,$pt[$i][1],$pad_width, $pt[$i][1]+$pad_height,$color);
  43. }else{
  44. imagefilledrectangle($image,0,$board_width-$pad_height,$pad_width, $board_width,$color);
  45. }
  46. imagefilledrectangle($image,$board_width-$pad_width,0,$board_width, $board_height,$color2);
  47. imagefilledrectangle($image,$pad_width+$pt[$i][0], $ball_size+$pt[$i][1]-$ball_size, $pad_width+$pt[$i][0]+$ball_size, $ball_size+$pt[$i][1],$color);
  48. //imagesetpixel($image,$pt[$i][0],$pt[$i][1],$color);
  49. imagegif($image);
  50. imagedestroy($image);
  51. $imagedata[] = ob_get_contents();
  52. ob_clean();
  53. ++$i;
  54. }
  55. $gif = new GIFEncoder(
  56. $imagedata,
  57. 100,
  58. 0,
  59. 2,
  60. 0, 0, 1,
  61. "bin"
  62. );
  63. Header ('Content-type:image/gif');
  64. echo $gif->GetAnimation();
  65. ?>

以上就是教大家如何利用php合成或是创建gif动画,希望大家仔细研究分享的两个实例,有所收获。