3

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
  • can you use imagemagick, or only gd?
    – JAL
    Jul 31, 2010 at 23:49
  • And what have you tried so far?
    – Charles
    Jul 31, 2010 at 23:51

4 Answers 4

4

"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);
3

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

Check the code snippet from max lloyd.

2

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

1
  • 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
    Aug 1, 2010 at 0:00
1

yes, you can try the gd library for this

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

take a look into imagejpeg() function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.