欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php生成gif动画的方法

程序员文章站 2023-11-14 14:38:34
首先需要确认gd库是否正常,如果是合成图片,请确保把分解的图片放在frames的文件夹里面。 gifencoder.class.php 类

首先需要确认gd库是否正常,如果是合成图片,请确保把分解的图片放在frames的文件夹里面。
gifencoder.class.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 ); 
    } 
} 
 
?> 

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

实例 1 合成gif动画:

<?php 
include "gifencoder.class.php"; 
/* 
  build a frames array from sources... 
*/ 
if ( $dh = opendir ( "frames/" ) ) { 
  while ( false !== ( $dat = readdir ( $dh ) ) ) { 
    if ( $dat != "." && $dat != ".." ) { 
      $frames [ ] = "frames/$dat"; 
      $framed [ ] = 5; 
    } 
  } 
  closedir ( $dh ); 
} 
/* 
    gifencoder constructor: 
    ======================= 
 
    image_stream = new gifencoder  ( 
              url or binary data 'sources' 
              int         'delay times' 
              int         'animation loops' 
              int         'disposal' 
              int         'transparent red, green, blue colors' 
              int         'source type' 
            ); 
*/ 
$gif = new gifencoder  ( 
      $frames, 
      $framed, 
      0, 
      2, 
      0, 0, 0, 
      "url" 
    ); 
/* 
    possibles outputs: 
    ================== 
 
    output as gif for browsers : 
      - header ( 'content-type:image/gif' ); 
    output as gif for browsers with filename: 
      - header ( 'content-disposition:attachment;filename=myanimation.gif'); 
    output as file to store into a specified file: 
      - fwrite ( fopen ( "myanimation.gif", "wb" ), $gif->getanimation ( ) ); 
*/ 
header ( 'content-type:image/gif' ); 
echo  $gif->getanimation ( ); 
?> 

实例 2 创建gif动画:

<?php 
include "gifencoder.class.php"; 
 
ob_start(); 
$board_width = 60; 
$board_height = 60; 
$pad_width = 5; 
$pad_height = 15; 
$ball_size = 5; 
$game_width = $board_width - $pad_width*2 - $ball_size; 
$game_height = $board_height-$ball_size; 
 
$x = 0; 
$y = rand(0,$game_height); 
$xv = rand(1,10); 
$yv = rand(1,10); 
$pt[] = array($x,$y); 
do{ 
    $x += $xv; 
    $y += $yv; 
    if($x > $game_width){ 
        $xv = -1*$xv; 
        $x = $game_width - ($x-$game_width); 
    }elseif($x < 0){ 
        $xv = -1*$xv; 
        $x = abs($x); 
    } 
    if($y>$game_height){ 
        $yv = -1*$yv; 
        $y = $game_height - ($y - $game_height); 
    }elseif($y<0){ 
        $yv = -1*$yv; 
        $y = abs($y); 
    } 
    $pt[] = array($x,$y); 
}while($x!=$pt[0][0]||$y!=$pt[0][1]); 
 
$i = 0; 
while(isset($pt[$i])){ 
    $image = imagecreate($board_width,$board_height); 
    imagecolorallocate($image, 0,0,0); 
    $color = imagecolorallocate($image, 255,255,255); 
    $color2 = imagecolorallocate($image, 255,0,0); 
     
    if($pt[$i][1] + $pad_height < $board_width){ 
        imagefilledrectangle($image,0,$pt[$i][1],$pad_width, $pt[$i][1]+$pad_height,$color); 
    }else{ 
        imagefilledrectangle($image,0,$board_width-$pad_height,$pad_width, $board_width,$color); 
    } 
    imagefilledrectangle($image,$board_width-$pad_width,0,$board_width, $board_height,$color2); 
    imagefilledrectangle($image,$pad_width+$pt[$i][0], $ball_size+$pt[$i][1]-$ball_size, $pad_width+$pt[$i][0]+$ball_size, $ball_size+$pt[$i][1],$color); 
    //imagesetpixel($image,$pt[$i][0],$pt[$i][1],$color); 
    imagegif($image); 
    imagedestroy($image); 
    $imagedata[] = ob_get_contents(); 
    ob_clean(); 
    ++$i; 
} 
 
$gif = new gifencoder( 
              $imagedata, 
              100, 
              0, 
              2, 
              0, 0, 1, 
              "bin" 
    ); 
     
header ('content-type:image/gif'); 
echo $gif->getanimation(); 
?> 

以上就是教大家如何利用php合成或是创建gif动画,希望大家仔细研究分享的两个实例,有所收获。