473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Animating gifs through PHP GD - treat as Image not Object?

118 100+
Me again :D You're going to be sick of me soon. =P

I found the GifEncoder class on phpclasses, which creates an animated gif image from frames and echos it to the browser to show; can also save it as an image with .gif extension.

The only problem is that I need to add the animated gif onto another image (with imagecopymerge), however the gif image that is returned by this code in an object.

Here is the code:

GifEncoder.Class.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. Class GIFAnimator {
  5.     var $GIF = "GIF89a";       
  6.     var $VER = "GIFEncoder V2.05";    
  7.  
  8.     var $BUF = Array ( );
  9.     var $LOP =  0;
  10.     var $DIS =  2;
  11.     var $COL = -1;
  12.     var $IMG = -1;
  13.  
  14.     var $ERR = Array (
  15.         ERR00=>"Does not supported function for only one image!",
  16.         ERR01=>"Source is not a GIF image!",
  17.         ERR02=>"Unintelligible flag ",
  18.         ERR03=>"Does not make animation from animated GIF source",
  19.     );
  20.  
  21.  
  22.     function GIFAnimator    (
  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.  
  35.         for ( $i = 0; $i < count ( $GIF_src ); $i++ ) {
  36.             if ( strToLower ( $GIF_mod ) == "url" ) {
  37.                 $this->BUF [ ] = fread ( fopen ( $GIF_src [ $i ], "rb" ), filesize ( $GIF_src [ $i ] ) );
  38.             }
  39.             else if ( strToLower ( $GIF_mod ) == "bin" ) {
  40.                 $this->BUF [ ] = $GIF_src [ $i ];
  41.             }
  42.             else {
  43.                 printf    ( "%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod );
  44.                 exit    ( 0 );
  45.             }
  46.             if ( substr ( $this->BUF [ $i ], 0, 6 ) != "GIF87a" && substr ( $this->BUF [ $i ], 0, 6 ) != "GIF89a" ) {
  47.                 printf    ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] );
  48.                 exit    ( 0 );
  49.             }
  50.             for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = TRUE; $k; $j++ ) {
  51.                 switch ( $this->BUF [ $i ] { $j } ) {
  52.                     case "!":
  53.                         if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) {
  54.                             printf    ( "%s: %s ( %s source )!", $this->VER, $this->ERR [ 'ERR03' ], ( $i + 1 ) );
  55.                             exit    ( 0 );
  56.                         }
  57.                         break;
  58.                     case ";":
  59.                         $k = FALSE;
  60.                         break;
  61.                 }
  62.             }
  63.         }
  64.         GIFAnimator::GIFHead ( );
  65.         for ( $i = 0; $i < count ( $this->BUF ); $i++ ) {
  66.             GIFAnimator::GIFGetFrames ( $i, $GIF_dly [ $i ] );
  67.         }
  68.         GIFAnimator::GIFFoot ( );
  69.     }
  70.  
  71.     function GIFHead ( ) {
  72.         $cmap = 0;
  73.  
  74.         if ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x80 ) {
  75.             $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) );
  76.  
  77.             $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7        );
  78.             $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap    );
  79.             $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFAnimator::GIFWord ( $this->LOP ) . "\0";
  80.         }
  81.     }
  82.  
  83.     function GIFGetFrames ( $i, $d ) {
  84.  
  85.         $Locals_str = 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) );
  86.  
  87.         $Locals_end = strlen ( $this->BUF [ $i ] ) - $Locals_str - 1;
  88.         $Locals_tmp = substr ( $this->BUF [ $i ], $Locals_str, $Locals_end );
  89.  
  90.         $Global_len = 2 << ( ord ( $this->BUF [ 0  ] { 10 } ) & 0x07 );
  91.         $Locals_len = 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  92.  
  93.         $Global_rgb = substr ( $this->BUF [ 0  ], 13,
  94.                             3 * ( 2 << ( ord ( $this->BUF [ 0  ] { 10 } ) & 0x07 ) ) );
  95.         $Locals_rgb = substr ( $this->BUF [ $i ], 13,
  96.                             3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) );
  97.  
  98.         $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
  99.                         chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "\x0\x0";
  100.  
  101.         if ( $this->COL > -1 && ord ( $this->BUF [ $i ] { 10 } ) & 0x80 ) {
  102.             for ( $j = 0; $j < ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $j++ ) {
  103.                 if    (
  104.                         ord ( $Locals_rgb { 3 * $j + 0 } ) == ( ( $this->COL >> 16 ) & 0xFF ) &&
  105.                         ord ( $Locals_rgb { 3 * $j + 1 } ) == ( ( $this->COL >>  8 ) & 0xFF ) &&
  106.                         ord ( $Locals_rgb { 3 * $j + 2 } ) == ( ( $this->COL >>  0 ) & 0xFF )
  107.                     ) {
  108.                     $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 1 ) .
  109.                                     chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . chr ( $j ) . "\x0";
  110.                     break;
  111.                 }
  112.             }
  113.         }
  114.         switch ( $Locals_tmp { 0 } ) {
  115.             case "!":
  116.                 $Locals_img = substr ( $Locals_tmp, 8, 10 );
  117.                 $Locals_tmp = substr ( $Locals_tmp, 18, strlen ( $Locals_tmp ) - 18 );
  118.                 break;
  119.             case ",":
  120.                 $Locals_img = substr ( $Locals_tmp, 0, 10 );
  121.                 $Locals_tmp = substr ( $Locals_tmp, 10, strlen ( $Locals_tmp ) - 10 );
  122.                 break;
  123.         }
  124.         if ( ord ( $this->BUF [ $i ] { 10 } ) & 0x80 && $this->IMG > -1 ) {
  125.             if ( $Global_len == $Locals_len ) {
  126.                 if ( GIFAnimator::GIFBlock ( $Global_rgb, $Locals_rgb, $Global_len ) ) {
  127.                     $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  128.                 }
  129.                 else {
  130.                     $byte  = ord ( $Locals_img { 9 } );
  131.                     $byte |= 0x80;
  132.                     $byte &= 0xF8;
  133.                     $byte |= ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
  134.                     $Locals_img { 9 } = chr ( $byte );
  135.                     $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  136.                 }
  137.             }
  138.             else {
  139.                 $byte  = ord ( $Locals_img { 9 } );
  140.                 $byte |= 0x80;
  141.                 $byte &= 0xF8;
  142.                 $byte |= ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  143.                 $Locals_img { 9 } = chr ( $byte );
  144.                 $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  145.             }
  146.         }
  147.         else {
  148.             $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  149.         }
  150.         $this->IMG  = 1;
  151.     }
  152.  
  153.     function GIFFoot ( ) {
  154.         $this->GIF .= ";";
  155.     }
  156.  
  157.     function GIFBlock ( $GlobalBlock, $LocalBlock, $Len ) {
  158.  
  159.         for ( $i = 0; $i < $Len; $i++ ) {
  160.             if    (
  161.                     $GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } ||
  162.                     $GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } ||
  163.                     $GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 }
  164.                 ) {
  165.                     return ( 0 );
  166.             }
  167.         }
  168.  
  169.         return ( 1 );
  170.     }
  171.  
  172.     function GIFWord ( $int ) {
  173.  
  174.         return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) );
  175.     }
  176.  
  177.     function GetAnimation ( ) {
  178.         return ( $this->GIF );
  179.     }
  180.  
  181. ?>
  182.  
then to produce the image from frames:

gif.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. include "GifEncoder.Class.php";
  4.  
  5. if ( $dh = opendir ( "frames/" ) ) {
  6.     while ( false !== ( $dat = readdir ( $dh ) ) ) {
  7.         if ( $dat != "." && $dat != ".." ) {
  8.             $frames [ ] = "frames/$dat";
  9.             $framed [ ] = 20;
  10.         }
  11.     }
  12.     closedir ( $dh );
  13. }
  14.  
  15. $gif = new GIFAnimator    (
  16.                             $frames, //sources - URL or path
  17.                             $framed, //delay times - int
  18.                             0, //Animation loops - int - 0 is infinite
  19.                             2, //Disposal - int
  20.                             -1, -1, -1, //transparency red, green, blue - int 
  21.                             "url" // source type
  22.         );
  23.  
  24.         header("Content-Type: image/gif");
  25. echo $gif->GetAnimation ( );
  26.  
  27.  
  28.  
  29. ?> 
  30.  
Is there any way that I can use or manipulate this code to produce a valid gif image from it could be used in the imagecopymerge function?

I tried adding this $gif->GetAnimation() to the imagecopymerge like this: (just to try something; not that it'd work)

Expand|Select|Wrap|Line Numbers
  1. $image = imagecreate(600,240);
  2. $black = imagecolorallocate($image, 0, 0, 0);
  3. imagefill($image, 0, 0, $black);
  4.  
  5.  
  6. imagecopymerge($image, $gif->GetAnimation(), 300, 100, 0, 0, 15, 15, 100);
  7.  
  8. header("content-type: image/gif");
  9. Imagegif($image);
  10. imagedestroy($image);
  11.  
  12.  
and got this error

Warning: imagecopymerge(): supplied argument is not a valid Image resource in /home/sites/helraizer.co.uk/public_html/ocr/animation.php on line 31
any ideas?

Sam
Jun 3 '08 #1
5 3386
pbmods
5,821 Expert 4TB
Heya, Sam.

I think GIFAnimator::$GIF is a string, rather than an image resource.

Try using imagecreatefromstring().
Jun 4 '08 #2
helraizer1
118 100+
Hey, pbmods

Expand|Select|Wrap|Line Numbers
  1. $im = imagecreatefromstring($gif->GIF);
  2.  
  3. imagecopymerge($image, $im, 300, 100, 0, 0, 15, 15, 100);
  4.  
That works, only once it's merged onto the image, it's no longer animated. xD

Is there any way to actuall merge an animated gif onto a static gif image?

Sam
Jun 4 '08 #3
pbmods
5,821 Expert 4TB
Hm. It sounds like what you'll need to do is to composite each frame individually.

In other words:
Expand|Select|Wrap|Line Numbers
  1. foreach( $frames as $name = $thePfhrame )
  2. {
  3.     $imgPfhrame = imagecreatefromstring($thePfhrame);
  4.     imagecopymerge($image, $imgPfhrame, 300, 100, 0, 0, 15, 15, 100);
  5.     imagegif($image, "frames/{$name}");
  6. }
  7.  
Then reload and import the frames into the GIFAnimator class.
Jun 4 '08 #4
helraizer1
118 100+
Hey pbmods!

I have used

[PHP]<?php

include('gif.php');

$image = imagecreatefrompng("../mychatbox/user/helraizer1.png");


if ( $dh = opendir ( "frames/" ) ) {

while ( false !== ( $dat = readdir ( $dh ) ) ) {

if ( $dat != "." && $dat != ".." ) {

$frames [ ] .= "frames/$dat";

$framed [ ] = 20;

}

}

closedir ( $dh );

}

foreach( $frames as $thePfhrame )

{

$imgPfhrame = imagecreatefromgif($thePfhrame);

imagecopymerge($image, $imgPfhrame, imageSX($image)/2, imageSY($image)/1.5, 0, 0, 15, 15, 100);




}


$gif = new GIFAnimator (

$frames, //sources - URL or path

$framed, //delay times - int

0, //Animation loops - int - 0 is infinite

2, //Disposal - int

-1, -1, -1, //transparency red, green, blue - int

"url" // source type

);






header("Content-Type: image/gif");

echo $gif->GetAnimation();


FWrite ( FOpen ( "img.gif", "wb" ), $gif->GetAnimation ( ) );


?> [/PHP]

and it produces this:


or



As you can see the one that is in middle is animated! So that works perfectly fine, on its own.

However when I put the code in my showimage script (to produce the shoutbox), which is in a different folder, but still has a 'frames' folder with the same frames in it, it gives me the error.

GIFEncoder V2.05: Does not supported function for only one image!
Even though there are 9 frames in the folder.

[PHP]
if(stristr($line, ":roll:")) {
session_register("roll");
$_SESSION['roll'] = 1;
}

if (isset($filter)) {
header("Content-Type: image/png"); // tell the browser what we're gonna give it
imagepng($image);

} elseif ($page == 1) {

if(!isset($_SESSION['roll'])) {
header("Content-Type: image/png"); // tell the browser what we're gonna give it
imagepng($image);
imagepng($image, "./user/" . $user . $box. ".png");

} else {

$pos = strpos($line, ":roll:");

if ($font == "palab") {
$post = $pos * 5.7;
} elseif ($font == "comicsans") {
$post = $pos * 6.725;
} elseif ($font == "calibri") {
$post = $pos * 5;
} elseif ($font == "courbd") {
$post = ($pos * 5.8) + 1;
} elseif ($font == "arial") {
$post = ($pos * 7.3) + 1;
} else {

}



if ( $dh = opendir ( "frames/" ) ) {

while ( false !== ( $dat = readdir ( $dh ) ) ) {

if ( $dat != "." && $dat != ".." ) {

$frames [ ] .= "frames/$dat";

$framed [ ] = 17;

}

}

closedir ( $dh );

}

foreach( $frames as $thePfhrame )

{

$imgPfhrame = imagecreatefromgif($thePfhrame);

imagecopymerge($image, $imgPfhrame, ($cur_line_x + $post) + 6, (($cur_line_y + $lineheight) - 30.5), 0, 0, 15, 15, 100);




}


$gif = new GIFAnimator (

$frames, //sources - URL or path

$framed, //delay times - int

0, //Animation loops - int - 0 is infinite

2, //Disposal - int

-1, -1, -1, //transparency red, green, blue - int

"url" // source type

);






header("Content-Type: image/gif");

echo $gif->GetAnimation();


FWrite ( FOpen ( "helraizer2.gif", "wb" ), $gif->GetAnimation ( ) );

}


else {
header("Content-Type: image/png"); // tell the browser what we're gonna give it
imagepng($image);

}
[/PHP]

Any idea why this happens?

Sam
Jun 15 '08 #5
pbmods
5,821 Expert 4TB
From line 26 in the code listing in your first post:

Expand|Select|Wrap|Line Numbers
  1. if ( ! is_array ( $GIF_src ) && ! is_array ( $GIF_tim ) ) {
  2.             printf    ( "%s: %s", $this->VER, $this->ERR [ 'ERR00' ] );
  3.             exit    ( 0 );
  4.         }
  5.  
I'll spare you some needless vgrepping; ERR00 is 'Does not supported (sic) function for only one image!'.

$GIF_tim doesn't seem to exist, so I'm a little confused by that (it also might be the source of the problem you're hitting).

I'd also confirm that $frames that you're passing to GIFAnimator's constructor on line 69 of your previous post is an array.
Jun 22 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: MrPickwick | last post by:
Hi there. If I place a picturebox on my form and load (at design time) an animated gif into it, it shows and animates allright. If I load my animated gifs in an Imagelist at design time and at...
0
by: GrandpaB | last post by:
I am creating a small simulation in VB and wish to incorporate several animated GIFS. I can load the GIFS from the hard drive into a picture box and they animate, but I have two questions. 1.)...
4
by: Chris Moltisanti | last post by:
Hi, I am executing a piece of javascript that does some intensive processing (sorting large tables). This can take a good few seconds so I have an animated gif as a progress bar to let the user...
4
by: Charleees | last post by:
GIFS not working properly in JavaScript PopUps Hi all, I have a button and when i click tha button it redirects to another page..... I have also added a java script for the button that...
3
by: ZikO | last post by:
Hi. I am making a Multimedia Presentation in VB.NET 2005 and I have some animated GIFs which I need to use. I tried to use PictureBox for it but it doesn't play an animation in GIFs :/. What I...
27
by: Phil | last post by:
I thought it would be nice to display some animated GIFs on some of my forms. I put a PictureBox control on a form, and loaded my GIF file in. It animates, but not properly. It seems very jerky and...
9
by: Peter Webb | last post by:
I want to animate one object moving in front of another. I cannot re-render the background as the object moves, as it would be extremely time consuming. This is what I would like to do. I draw...
0
by: helraizer1 | last post by:
Hey, I have a PHP script that I made for an image based shoutbox with emoticons. The problem is that php GD doesn't support animated gifs, yet perl GD does. How would I make it so that I could...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.