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.     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. <?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. ?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章