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

今天在公司,经理让做一个滚动字幕。但是,必须生成gif图片。所以上网找了GIFEncoder这个类库。确实很好用,但是,应用过程中也出现了一些问题,现在写在这里,以供后来人参考,少走弯路。

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

 

文字滚动分为两种情况。第一种为水平滚动

  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.  
  24. ob_start();
  25. Header ('Content-type:image/gif');
  26.  
  27. echo $gif->GetAnimation();
  28. ?>

然后是垂直的

  1. <?php
  2. /*
  3.  
  4. 从url获得"str=第一行;第二行;第三行"的数据
  5.  
  6. */
  7. require_once("GIFEncoder.class.php");
  8. $array_str=array();
  9. $str=$_GET['str'];
  10. /*
  11.  
  12. 将$str转化成数组
  13.  
  14. */
  15. if ($str!=''){
  16.     $array_str=explode(";",$str);
  17. }else{
  18.     $array_str=array("欢迎您光临本店!");
  19. }
  20. /*
  21.  
  22. 如果数组元素没有值,清除最后一个“;”
  23.  
  24. */
  25. foreach( $array_str as $k=>$v){
  26.     if( !$v )
  27.     unset( $array_str[$k] );
  28. }
  29.  
  30.  
  31. for ($i=0,$length=count($array_str);$i<$length;$i++){
  32.     for ($k=0;$k<20;$k++){
  33.         $im=imagecreatefromgif("hudongbg.gif");     //根据图片创建图像
  34.         $white = ImageColorAllocate($im,255,255,255);       //设置一个白色
  35.         $test = iconv("GB2312","UTF-8",$array_str[$i]);     //特别注意的是转换编码,因为之后用到的imagettftext只能用utf-8编码,注意,一定要用字符串接收,不能用数组,否则会乱码
  36.         $heigth=40-$k;        //当前文字高度
  37.         imagettftext($im,13,0,0,$heigth,$white,"ziti.ttf",$test);        //将文字写入图片
  38.         imagegif($im);
  39.         $imagedata[] = ob_get_contents();
  40.         ob_clean();
  41.     }
  42. }
  43. /*
  44. 这里是设置每一张图片的延迟时间,到第20帧的时候,延迟2秒。这样才能出现文字走到顶,停顿一会的效果
  45. */
  46.  
  47. $delay=array();
  48. for ($i=1,$length=count($array_str)*20;$i<=$length;$i++){
  49.     if ($i%20==0){
  50.         $delay[$i-1]="200";
  51.     }else{
  52.         $delay[$i-1]="1";
  53.     }
  54. }
  55. $transparent=array(0);
  56. $gif = new GIFEncoder($imagedata,$delay,0,2,0,0,$transparent,"bin");
  57. ob_start();
  58. Header ('Content-type:image/gif');
  59. echo $gif->GetAnimation();
  60. ?>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值