How to generate GIF animation in PHP _php tips

Source: Internet
Author: User
Tags abs ord php class rand

First you need to verify that the GD library is normal, and if it is a composite picture, make sure you put the exploded picture in the Frames folder.
GIFEncoder.class.php class

?        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 ' animation ' a 
 
    nimated GIF Source ",);  *:::::::::::::::::::::.::;::.:::.::::.:::: Gifencoder ...:: */function Gifencoder ($GIF _src, $GIF _dly, $GIF _lop, $GIF _dis, $GIF _re  D, $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 '], $GI 
                F_MOD); 
            Exit (0); } if (substr ($this->buf [$i], 0, 6) != "gif87a" && substr ($this->buf [$i], 0, 6)!= "gif89a") {printf ("%s:%d%s", $t 
                His->ver, $i, $this->err [' ERR01 ']); 
            Exit (0); for ($j = 3 * (2 << (ord ($this->buf [$i] {ten}) & 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] {ten}) & 0x80) {$cmap = 3 * (2 << (ord ($this->buf [0] { 
 
            ) & 0x07)); 
            $this->gif. = substr ($this->buf [0], 6, 7); 
            $this->gif. = substr ($this->buf [0], $cmap); $this->gif. = "!\377\13netscape2.0\3\1". Gifencoder::gifword ($this->lop). 
        "The"; 
    }/*::::::::::::::::, (::): ":;::";: ":": ":";::: Gifaddframes ...:: */function Gifaddframes ($i, $d) {$Locals _STR = + 3 * (2 << ord ($this->buf [$i] {1 
 
        0}) & 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] {ten}) & 0x07); 
 
        $Locals _len = 2 << (ord ($this->buf [$i] {}) & 0x07); $Global _rgb = substr ($this->buf [0], 3 * (2 << (ord ($this->buf [0] 
        {ten}) & 0x07)); $Locals _rgb = substr ($this->buf [$i], 3 * (2 << (ord $this->buf [$i 
 
        ] {}) & 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] {}) & 0x80) {for ($j = 0; $j ; (2 << (ord ($this->buf [ $i] {}) & 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 >>) & 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, strlen ($Locals _tmp)-18); 
            Break Case ",": $locals_img = substr ($Locals _tmp, 0, 10); 
                $Locals _tmp = substr ($Locals _tmp, strlen ($Locals _tmp)-10); 
        Break } if (Ord ($this->buf [$i] {ten}) & 0x80 && $this->img >-1) {if ($Glo  
                    Bal_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] {ten}) & 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] {}) & 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 ...:: * * func 
    tion Gifword ($int) {return (Chr ($int & 0xFF). Chr (($int >> 8) & 0xFF); 
    }/*::::::::::::::::;::.::,:.::.:.::::::;:: Getanimation ...:: */ 
    function Getanimation () {return ($this->gif);  }}?>

For you to share two examples for your reference:

Instance 1 Synthetic GIF animation:

<?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 ' S 
Ource type '); 
* * $gif = new Gifencoder ($frames, $framed, 0, 2, 0, 0, 0, "url"); /* possibles outputs: ================== Output as GIF for browsers:-Header (' Content-type:ima 
    Ge/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 (); 
 ?>

Instance 2 creates an animated 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, $color 2); 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 ();  ?>

The above is to teach you how to use PHP to synthesize or create GIF animation, I hope you carefully study the share of the two examples, some gains.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.