当前位置:Gxlcms > PHP教程 > 在php中如何输出动态gif

在php中如何输出动态gif

时间:2021-07-01 10:21:17 帮助过:3人阅读

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

相关推荐:《PHP入门教程》

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 ) !=
  46. "GIF89a" ) {
  47. printf ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] );
  48. exit ( 0 );
  49. }
  50. for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = TRUE; $k; $j++ )
  51. {
  52. switch ( $this->BUF [ $i ] { $j } ) {
  53. case "!":
  54. if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) {
  55. printf ( "%s: %s ( %s source )!", $this->VER, $this->ERR [ 'ERR03' ],
  56. ( $i + 1 ) );
  57. exit ( 0 );
  58. }
  59. break;
  60. case ";":
  61. $k = FALSE;
  62. break;
  63. }
  64. }
  65. }
  66. GIFEncoder::GIFAddHeader ( );
  67. for ( $i = 0; $i < count ( $this->BUF ); $i++ ) {
  68. GIFEncoder::GIFAddFrames ( $i, $GIF_dly [ $i ] );
  69. }
  70. GIFEncoder::GIFAddFooter ( );
  71. }
  72. /*
  73. :::::::::::::::::::::::::::::::::::::::::::::::::::
  74. ::
  75. :: GIFAddHeader...
  76. ::
  77. */
  78. function GIFAddHeader ( ) {
  79. $cmap = 0;
  80. if ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x80 ) {
  81. $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) );
  82. $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7 );
  83. $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap );
  84. $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";
  85. }
  86. }
  87. /*
  88. :::::::::::::::::::::::::::::::::::::::::::::::::::
  89. ::
  90. :: GIFAddFrames...
  91. ::
  92. */
  93. function GIFAddFrames ( $i, $d ) {
  94. $Locals_str = 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) );
  95. $Locals_end = strlen ( $this->BUF [ $i ] ) - $Locals_str - 1;
  96. $Locals_tmp = substr ( $this->BUF [ $i ], $Locals_str, $Locals_end );
  97. $Global_len = 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
  98. $Locals_len = 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  99. $Global_rgb = substr ( $this->BUF [ 0 ], 13,
  100. 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) ) );
  101. $Locals_rgb = substr ( $this->BUF [ $i ], 13,
  102. 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) );
  103. $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
  104. chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "\x0\x0";
  105. if ( $this->COL > -1 && ord ( $this->BUF [ $i ] { 10 } ) & 0x80 ) {
  106. for ( $j = 0; $j < ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $j++ ) {
  107. if (
  108. ord ( $Locals_rgb { 3 * $j + 0 } ) == ( $this->COL >> 0 ) & 0xFF &&
  109. ord ( $Locals_rgb { 3 * $j + 1 } ) == ( $this->COL >> 8 ) & 0xFF &&
  110. ord ( $Locals_rgb { 3 * $j + 2 } ) == ( $this->COL >> 16 ) & 0xFF
  111. ) {
  112. $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 1 ) .
  113. chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) .
  114. chr ( $j ) . "\x0";
  115. break;
  116. }
  117. }
  118. }
  119. switch ( $Locals_tmp { 0 } ) {
  120. case "!":
  121. $Locals_img = substr ( $Locals_tmp, 8, 10 );
  122. $Locals_tmp = substr ( $Locals_tmp, 18, strlen ( $Locals_tmp ) - 18 );
  123. break;
  124. case ",":
  125. $Locals_img = substr ( $Locals_tmp, 0, 10 );
  126. $Locals_tmp = substr ( $Locals_tmp, 10, strlen ( $Locals_tmp ) - 10 );
  127. break;
  128. }
  129. if ( ord ( $this->BUF [ $i ] { 10 } ) & 0x80 && $this->IMG > -1 ) {
  130. if ( $Global_len == $Locals_len ) {
  131. if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) ) {
  132. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  133. }
  134. else {
  135. $byte = ord ( $Locals_img { 9 } );
  136. $byte |= 0x80;
  137. $byte &= 0xF8;
  138. $byte |= ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
  139. $Locals_img { 9 } = chr ( $byte );
  140. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  141. }
  142. }
  143. else {
  144. $byte = ord ( $Locals_img { 9 } );
  145. $byte |= 0x80;
  146. $byte &= 0xF8;
  147. $byte |= ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  148. $Locals_img { 9 } = chr ( $byte );
  149. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  150. }
  151. }
  152. else {
  153. $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  154. }
  155. $this->IMG = 1;
  156. }
  157. /*
  158. :::::::::::::::::::::::::::::::::::::::::::::::::::
  159. ::
  160. :: GIFAddFooter...
  161. ::
  162. */
  163. function GIFAddFooter ( ) {
  164. $this->GIF .= ";";
  165. }
  166. /*
  167. :::::::::::::::::::::::::::::::::::::::::::::::::::
  168. ::
  169. :: GIFBlockCompare...
  170. ::
  171. */
  172. function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len ) {
  173. for ( $i = 0; $i < $Len; $i++ ) {
  174. if (
  175. $GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } ||
  176. $GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } ||
  177. $GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 }
  178. ) {
  179. return ( 0 );
  180. }
  181. }
  182. return ( 1 );
  183. }
  184. /*
  185. :::::::::::::::::::::::::::::::::::::::::::::::::::
  186. ::
  187. :: GIFWord...
  188. ::
  189. */
  190. function GIFWord ( $int ) {
  191. return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) );
  192. }
  193. /*
  194. :::::::::::::::::::::::::::::::::::::::::::::::::::
  195. ::
  196. :: GetAnimation...
  197. ::
  198. */
  199. function GetAnimation ( ) {
  200. return ( $this->GIF );
  201. }
  202. }
  203. ?>

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

实例 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]+
  48. $ball_size, $ball_size+$pt[$i][1],$color);
  49. //imagesetpixel($image,$pt[$i][0],$pt[$i][1],$color);
  50. imagegif($image);
  51. imagedestroy($image);
  52. $imagedata[] = ob_get_contents();
  53. ob_clean();
  54. ++$i;
  55. }
  56. $gif = new GIFEncoder(
  57. $imagedata,
  58. 100,
  59. 0,
  60. 2,
  61. 0, 0, 1,
  62. "bin"
  63. );
  64. Header ('Content-type:image/gif');
  65. echo $gif->GetAnimation();
  66. ?>

以上就是在php中如何输出动态gif的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行