PHP使用GIFEncoder类生成gif动态滚动字幕
程序员文章站
2023-12-09 13:09:15
今天在公司,经理让做一个滚动字幕。但是,不许生成gif图片。所以上网找了gifencoder这个类库。确实很好用,但是,应用过程中也出现了一些问题,现在写在这里,以供后...
今天在公司,经理让做一个滚动字幕。但是,不许生成gif图片。所以上网找了gifencoder这个类库。确实很好用,但是,应用过程中也出现了一些问题,现在写在这里,以供后来人参考,少走弯路。
文字滚动分为两种情况。第一种为水平滚动:
复制代码 代码如下:
<?php
require_once("gifencoder.class.php");
$count=0; //设置默认计数器
while(true){
$str = $_request['str'] ? $_request['str']:"暂无输入";
$length=strlen($str)*9; //计算行长度,这个计算的比较简单,就是每个字数*9个像素
$im=imagecreatefromgif("hudongbg.gif"); //根据图片创建图像
$white = imagecolorallocate($im,255,255,255); //设置一个白色
$str = iconv("gb2312","utf-8",$str); //特别注意的是转换编码,因为之后用到的imagettftext只能用utf-8编码
$now=220-$count*5; //当前运行的水平位置
imagettftext($im,13,0,$now,20,$white,"ziti.ttf",$str); //根据字体在图片上写文字,参数意思(图像源,文字大小,倾斜角度,水平位置,垂直位置,颜色,使用的字体文件,要写的内容
imagegif($im);
imagedestroy($im);
$imagedata[] = ob_get_contents(); //创建这一帧的图像数据
ob_clean();
$count++;
if ($now+$length<0){ //如果最后一个文字移动到头,那么结束
break;
}
}
$diy[]=0;//开始延迟时间
$gif = new gifencoder($imagedata,$diy,0,2,0,0,0,"bin");
ob_start();
header ('content-type:image/gif');
echo $gif->getanimation();
?>
然后是垂直的:
复制代码 代码如下:
<?php
/*
从url获得"str=第一行;第二行;第三行"的数据
*/
require_once("gifencoder.class.php");
$array_str=array();
$str=$_get['str'];
/*
将$str转化成数组
*/
if ($str!=''){
$array_str=explode(";",$str);
}else{
$array_str=array("欢迎您光临本店!");
}
/*
如果数组元素没有值,清除最后一个“;”
*/
foreach( $array_str as $k=>$v){
if( !$v )
unset( $array_str[$k] );
}
for ($i=0,$length=count($array_str);$i<$length;$i++){
for ($k=0;$k<20;$k++){
$im=imagecreatefromgif("hudongbg.gif"); //根据图片创建图像
$white = imagecolorallocate($im,255,255,255); //设置一个白色
$test = iconv("gb2312","utf-8",$array_str[$i]); //特别注意的是转换编码,因为之后用到的imagettftext只能用utf-8编码,注意,一定要用字符串接收,不能用数组,否则会乱码
$heigth=40-$k; //当前文字高度
imagettftext($im,13,0,0,$heigth,$white,"ziti.ttf",$test); //将文字写入图片
imagegif($im);
$imagedata[] = ob_get_contents();
ob_clean();
}
}
/*
这里是设置每一张图片的延迟时间,到第20帧的时候,延迟2秒。这样才能出现文字走到顶,停顿一会的效果
*/
$delay=array();
for ($i=1,$length=count($array_str)*20;$i<=$length;$i++){
if ($i%20==0){
$delay[$i-1]="200";
}else{
$delay[$i-1]="1";
}
}
$transparent=array(0);
$gif = new gifencoder($imagedata,$delay,0,2,0,0,$transparent,"bin");
ob_start();
header ('content-type:image/gif');
echo $gif->getanimation();
?>
gifencoder类源码:
复制代码 代码如下:
<?php
class gifencoder {
var $gif = "gif89a"; /* gif header 6 bytes */
var $ver = "gifencoder v2.06"; /* encoder version */
var $buf = array ( );
var $lop = 0;
var $dis = 2;
var $col = -1;
var $img = -1;
var $err = array (
'err00' =>"does not supported function for only one image!",
'err01' =>"source is not a gif image!",
'err02' =>"unintelligible flag ",
'err03' =>"could not make animation from animated gif source",
);
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: gifencoder...
::
*/
function gifencoder (
$gif_src, $gif_dly, $gif_lop, $gif_dis,
$gif_red, $gif_grn, $gif_blu, $gif_mod
) {
if ( ! is_array ( $gif_src ) && ! is_array ( $gif_tim ) ) {
printf ( "%s: %s", $this->ver, $this->err [ 'err00' ] );
exit ( 0 );
}
$this->lop = ( $gif_lop > -1 ) ? $gif_lop : 0;
$this->dis = ( $gif_dis > -1 ) ? ( ( $gif_dis < 3 ) ? $gif_dis : 3 ) : 2;
$this->col = ( $gif_red > -1 && $gif_grn > -1 && $gif_blu > -1 ) ?
( $gif_red | ( $gif_grn << 8 ) | ( $gif_blu << 16 ) ) : -1;
for ( $i = 0; $i < count ( $gif_src ); $i++ ) {
if ( strtolower ( $gif_mod ) == "url" ) {
$this->buf [ ] = fread ( fopen ( $gif_src [ $i ], "rb" ), filesize ( $gif_src [ $i ] ) );
}
else if ( strtolower ( $gif_mod ) == "bin" ) {
$this->buf [ ] = $gif_src [ $i ];
}
else {
printf ( "%s: %s ( %s )!", $this->ver, $this->err [ 'err02' ], $gif_mod );
exit ( 0 );
}
if ( substr ( $this->buf [ $i ], 0, 6 ) != "gif87a" && substr ( $this->buf [ $i ], 0, 6 ) != "gif89a" ) {
printf ( "%s: %d %s", $this->ver, $i, $this->err [ 'err01' ] );
exit ( 0 );
}
for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->buf [ $i ] { 10 } ) & 0x07 ) ) ), $k = true; $k; $j++ ) {
switch ( $this->buf [ $i ] { $j } ) {
case "!":
if ( ( substr ( $this->buf [ $i ], ( $j + 3 ), 8 ) ) == "netscape" ) {
printf ( "%s: %s ( %s source )!", $this->ver, $this->err [ 'err03' ], ( $i + 1 ) );
exit ( 0 );
}
break;
case ";":
$k = false;
break;
}
}
}
gifencoder::gifaddheader ( );
for ( $i = 0; $i < count ( $this->buf ); $i++ ) {
gifencoder::gifaddframes ( $i, $gif_dly [ $i ] );
}
gifencoder::gifaddfooter ( );
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: gifaddheader...
::
*/
function gifaddheader ( ) {
$cmap = 0;
if ( ord ( $this->buf [ 0 ] { 10 } ) & 0x80 ) {
$cmap = 3 * ( 2 << ( ord ( $this->buf [ 0 ] { 10 } ) & 0x07 ) );
$this->gif .= substr ( $this->buf [ 0 ], 6, 7 );
$this->gif .= substr ( $this->buf [ 0 ], 13, $cmap );
$this->gif .= "!/377/13netscape2.0/3/1" . gifencoder::gifword ( $this->lop ) . "/0";
}
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: gifaddframes...
::
*/
function gifaddframes ( $i, $d ) {
$locals_str = 13 + 3 * ( 2 << ( ord ( $this->buf [ $i ] { 10 } ) & 0x07 ) );
$locals_end = strlen ( $this->buf [ $i ] ) - $locals_str - 1;
$locals_tmp = substr ( $this->buf [ $i ], $locals_str, $locals_end );
$global_len = 2 << ( ord ( $this->buf [ 0 ] { 10 } ) & 0x07 );
$locals_len = 2 << ( ord ( $this->buf [ $i ] { 10 } ) & 0x07 );
$global_rgb = substr ( $this->buf [ 0 ], 13,
3 * ( 2 << ( ord ( $this->buf [ 0 ] { 10 } ) & 0x07 ) ) );
$locals_rgb = substr ( $this->buf [ $i ], 13,
3 * ( 2 << ( ord ( $this->buf [ $i ] { 10 } ) & 0x07 ) ) );
$locals_ext = "!/xf9/x04" . chr ( ( $this->dis << 2 ) + 0 ) .
chr ( ( $d >> 0 ) & 0xff ) . chr ( ( $d >> 8 ) & 0xff ) . "/x0/x0";
if ( $this->col > -1 && ord ( $this->buf [ $i ] { 10 } ) & 0x80 ) {
for ( $j = 0; $j < ( 2 << ( ord ( $this->buf [ $i ] { 10 } ) & 0x07 ) ); $j++ ) {
if (
ord ( $locals_rgb { 3 * $j + 0 } ) == ( $this->col >> 0 ) & 0xff &
ord ( $locals_rgb { 3 * $j + 1 } ) == ( $this->col >> 8 ) & 0xff &
ord ( $locals_rgb { 3 * $j + 2 } ) == ( $this->col >> 16 ) & 0xff
) {
$locals_ext = "!/xf9/x04" . chr ( ( $this->dis << 2 ) + 1 ) .
chr ( ( $d >> 0 ) & 0xff ) . chr ( ( $d >> 8 ) & 0xff ) . chr ( $j ) . "/x0";
break;
}
}
}
switch ( $locals_tmp { 0 } ) {
case "!":
$locals_img = substr ( $locals_tmp, 8, 10 );
$locals_tmp = substr ( $locals_tmp, 18, strlen ( $locals_tmp ) - 18 );
break;
case ",":
$locals_img = substr ( $locals_tmp, 0, 10 );
$locals_tmp = substr ( $locals_tmp, 10, strlen ( $locals_tmp ) - 10 );
break;
}
if ( ord ( $this->buf [ $i ] { 10 } ) & 0x80 && $this->img > -1 ) {
if ( $global_len == $locals_len ) {
if ( gifencoder::gifblockcompare ( $global_rgb, $locals_rgb, $global_len ) ) {
$this->gif .= ( $locals_ext . $locals_img . $locals_tmp );
}
else {
$byte = ord ( $locals_img { 9 } );
$byte |= 0x80;
$byte &= 0xf8;
$byte |= ( ord ( $this->buf [ 0 ] { 10 } ) & 0x07 );
$locals_img { 9 } = chr ( $byte );
$this->gif .= ( $locals_ext . $locals_img . $locals_rgb . $locals_tmp );
}
}
else {
$byte = ord ( $locals_img { 9 } );
$byte |= 0x80;
$byte &= 0xf8;
$byte |= ( ord ( $this->buf [ $i ] { 10 } ) & 0x07 );
$locals_img { 9 } = chr ( $byte );
$this->gif .= ( $locals_ext . $locals_img . $locals_rgb . $locals_tmp );
}
}
else {
$this->gif .= ( $locals_ext . $locals_img . $locals_tmp );
}
$this->img = 1;
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: gifaddfooter...
::
*/
function gifaddfooter ( ) {
$this->gif .= ";";
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: gifblockcompare...
::
*/
function gifblockcompare ( $globalblock, $localblock, $len ) {
for ( $i = 0; $i < $len; $i++ ) {
if (
$globalblock { 3 * $i + 0 } != $localblock { 3 * $i + 0 } ||
$globalblock { 3 * $i + 1 } != $localblock { 3 * $i + 1 } ||
$globalblock { 3 * $i + 2 } != $localblock { 3 * $i + 2 }
) {
return ( 0 );
}
}
return ( 1 );
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: gifword...
::
*/
function gifword ( $int ) {
return ( chr ( $int & 0xff ) . chr ( ( $int >> 8 ) & 0xff ) );
}
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: getanimation...
::
*/
function getanimation ( ) {
return ( $this->gif );
}
}
?>
推荐阅读