views:

74

answers:

4

I need to convert animated GIF to static in PHP. I mean e.g. use its first frame. Any idea how to do that ?

+2  A: 

The best way I could think (not very cute one) is to convert the gif to png/jpeg and then turn it to gif again, :P

try this for converting ;) http://gallery.menalto.com/node/13206

hope this helps you

Saikios
Clearly, converting to PNG is the better alternative if this is what you choose to do. However, since PHP/GD doesn't seem to support animated GIFs, "converting" it to GIF (e.g. `imagegif(imagecreatefromgif($file));`) might work.
You
+3  A: 

Take a look at http://php.net/manual/en/function.imagecreatefromgif.php

Check the code snippet from max lloyd.

Dan
+1  A: 

yes, you can try the gd library for this

http://php.net/manual/en/book.image.php

take a loog into imagejpeg() function

ErVeY
+2  A: 

"Stripping" the GIF of animation can be done by converting it to another format and then back again. PNG is a good candidate for this "other format", since it is non-lossy, unlike JPEG. Using PHPs GD functions, and outputting a PNG instead of a GIF:

header('Content-type: image/png');
imagepng(imagecreatefromgif($file));

This might work (haven't tested) if PHP/GD doesn't support animated GIFs (I don't think it does); and it will output the image in GIF format, unlike the above snippet:

header('Content-type: image/gif');
imagegif(imagecreatefromgif($file));

If that won't work, and output in GIF format is essential, this will:

$img1 = imagecreatefromgif($file);
$size = getimagesize($img1);
$img2 = imagecreatetruecolor($size[0], $size[1]);
imagecopy($img2, $img1, 0, 0, 0, 0, $size[0], $size[1]);
header('Content-type: image/gif');
imagegif($img2);
You