A-A+

PHP使用GIFEncoder生成动态gif滚动字幕

2009年03月11日 PHP 暂无评论 阅读 1 次

一个滚动字幕。要求用php生成gif图片。所以上网找了GIFEncoder这个类库。确实很好用,但是,应用过程中也出现了一些小问题,现在放在这里,以供参考,少走弯路。

  1. <?php
  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...作者:邢海龙 tdweb0718
  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 .= "!37713NETSCAPE2.031" . GIFEncoder::GIFWord ( $this->LOP ) . "";
  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 = "!xF9x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
  101. chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "x0x0";
  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 = "!xF9x04" . 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. <?php
  2. require_once("GIFEncoder.class.php");
  3. $count=0;   设置默认计数器
  4. while(true){
  5. $str = $_REQUEST['str'] ? $_REQUEST['str']:"暂无输入";
  6. $length=strlen($str)*9;     计算行长度,这个计算的比较简单,就是每个字数*9个像素
  7. $im=imagecreatefromgif("hudongbg.gif");     根据图片创建图像
  8. $white = ImageColorAllocate($im,255,255,255);       设置一个白色
  9. $str = iconv("GB2312","UTF-8",$str);        特别注意的是转换编码,因为之后用到的imagettftext只能用utf-8编码
  10. $now=220-$count*5;      当前运行的水平位置
  11.     imagettftext($im,13,0,$now,20,$white,"ziti.ttf",$str);      根据字体在图片上写文字,参数意思(图像源,文字大小,倾斜角度,水平位置,垂直位置,颜色,使用的字体文件,要写的内容
  12.     imagegif($im);      
  13.     imagedestroy($im);
  14. $imagedata[] = ob_get_contents();       创建这一帧的图像数据
  15.     ob_clean();
  16. $count++;
  17. if ($now+$length<0){    如果最后一个文字移动到头,那么结束
  18. break;
  19.     }
  20. }
  21. $diy[]=0;开始延迟时间
  22. $gif = new GIFEncoder($imagedata,$diy,0,2,0,0,0,"bin");
  23. ob_start();
  24. Header ('Content-type:image/gif');
  25. echo $gif->GetAnimation();
  26. ?>

然后是垂直的

  1. <?php
  2. /*
  3. 从url获得"str=第一行;第二行;第三行"的数据
  4. */
  5. require_once("GIFEncoder.class.php");
  6. $array_str=array();
  7. $str=$_GET['str'];
  8. /*
  9. 将$str转化成数组
  10. */
  11. if ($str!=''){
  12. $array_str=explode(";",$str);
  13. }else{
  14. $array_str=array("欢迎您光临本店!");
  15. }
  16. /*
  17. 如果数组元素没有值,清除最后一个“;”
  18. */
  19. foreach( $array_str as $k=>$v){
  20. if( !$v )
  21.     unset( $array_str[$k] );
  22. }
  23. for ($i=0,$length=count($array_str);$i<$length;$i++){
  24. for ($k=0;$k<20;$k++){
  25. $im=imagecreatefromgif("hudongbg.gif");     根据图片创建图像
  26. $white = ImageColorAllocate($im,255,255,255);       设置一个白色
  27. $test = iconv("GB2312","UTF-8",$array_str[$i]);     特别注意的是转换编码,因为之后用到的imagettftext只能用utf-8编码,注意,一定要用字符串接收,不能用数组,否则会乱码
  28. $heigth=40-$k;        当前文字高度
  29.         imagettftext($im,13,0,0,$heigth,$white,"ziti.ttf",$test);        将文字写入图片

  30.         imagegif($im);
  31. $imagedata[] = ob_get_contents();
  32.         ob_clean();
  33.     }
  34. }
  35. /*
  36. 这里是设置每一张图片的延迟时间,到第20帧的时候,延迟2秒。这样才能出现文字走到顶,停顿一会的效果
  37. */
  38. $delay=array();
  39. for ($i=1,$length=count($array_str)*20;$i<=$length;$i++){
  40. if ($i%20==0){
  41. $delay[$i-1]="200";
  42.     }else{
  43. $delay[$i-1]="1";
  44.     }
  45. }
  46. $transparent=array(0);
  47. $gif = new GIFEncoder($imagedata,$delay,0,2,0,0,$transparent,"bin");
  48. ob_start();
  49. Header ('Content-type:image/gif');
  50. echo $gif->GetAnimation();
  51. ?>

给我留言

Copyright © 浩然东方 保留所有权利.   Theme  Ality 07032740

用户登录