How PHP synthesizes or creates GIF animations

Source: Internet
Author: User
This article mainly introduces PHP synthesis or the method of creating GIF animation, the interested friend's reference, I hope to be helpful to everyone.

First, you need to confirm whether the GD library is normal, if it is a composite picture, make sure to put the exploded image 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 ' = ' supported function for only one image! ', ' ERR01 ' = ' and ' Source is ' Not a GIF image! ", ' ERR02 ' =" unintelligible flag ", ' ERR03 ' + ' Could not make animation from Anima      Ted GIF Source ","); * *::--:::::::::::::: Gifencoder,.::::: ":::"-""::: ":".::: "..:::. Coder ($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 = (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::::-:::---:::::--:::::--:::::.::::.          On 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] {ten}) &amp ;          0x07));         $Locals _end = strlen ($this->buf [$i])-$Locals _str-1; $Locals _tmp = substr ($thisBUF [$i], $Locals _str, $Locals _end);         $Global _len = 2 << (ord ($this->buf [0] {ten}) & 0x07);          $Locals _len = 2 << (ord ($this->buf [$i] {ten}) & 0x07);  $Global _rgb = substr ($this->buf [0], 3 * (2 << (ord ($this->buf [0] {         ) & 0x07));  $Locals _rgb = substr ($this->buf [$i], 3 * (2 << (ord. $this->buf [$i]          {ten}) & 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] {ten}) & 0x80) {for ($j = 0; $j < (2 << (ord ($this->buf [$i] {ten}) & 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-&G                                     T;dis << 2) + 1). Chr (($d >> 0) & 0xFF). Chr (($d >> 8) & 0xFF). Chr ($j).                     "\x0";                 Break }}} switch ($Locals _tmp {0}) {case "!": $Locals _img = subst                 R ($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, ten, strlen ($Locals _tmp)-10);              Break } if (Ord ($this->buf [$i] {ten}) & 0x80 && $this->img >-1) {if ($Globa                     L_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] {ten}) & 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::::-:::---:::::--:::::--:::::.::::.     On Gifaddfooter () {$this->gif. = ";"; }/*::::::::::::::::::.:::: Gifblockcompare:::: ":"::: "* * fun".::::..::::    Ction 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::::::.:::---::::::: "     Fword ($int) {return (Chr ($int & 0xFF). Chr (($int >> 8) & 0xFF)); }/*::::::::::::::::: Getanimation::::-:::---:::::--:::::--:::::.::::.     On Getanimation () {return ($this->gif); }}?>

For everyone to share two examples for your reference:

Example 1 synthetic GIF animations:

<?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 Bi         nary 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/gi     F '); 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 ();?>

Example 2 Creating 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); $color 2 = imagecolorallocate ($image, 255,0,0); if ($pt [$i][1] + $pad _height < $board _width) {imagefilledrectangle ($image, 0, $pt [$i][1], $pad _width, $pt [$i][1]+ $p     Ad_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 ();?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.