1

I am in need of reading the individual frames of an animated GIF in PHP.

I use GD all the time for image manipulation, but unfortunately it lacks the capability to handle multiple frames in a GIF image.

I'm currently in the middle of carving up the GIF file format in an attempt to make my own extractor, but before I go any further I was wondering if anyone had any pointers for code that already does this - I can't be the first to want to handle animations in PHP, surely.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Have you checked out ImageMagick? I haven't ever used it in PHP, but it looks like it might be able to do what you're looking for. Check out [example #5 on this page.](http://php.net/manual/en/imagick.examples-1.php) – Gordon Bailey Feb 20 '12 at 05:34
  • It'd be nice if half of PHP.net weren't down, most importantly `static.php.net` and `pecl.php.net`... – Niet the Dark Absol Feb 20 '12 at 06:06

1 Answers1

2

Yup. I think the ImageMagick extension should work nicely in your case. I've used it in my PHP codes before, and while I've not used GD before, ImageMagick has been sufficient for my minor image manipulation needs. =)

$agif = new Imagick("animated.gif");

// Loop over all individual frames 
foreach ($agif as $frame) {

    // Do whatever you need to do here 

}

// Save the modified gif
$agif->writeImages("new_animated.gif", true);

The full set of functions available through the ImageMagick PHP extension can be found at http://php.net/manual/en/book.imagick.php

jsamraj
  • 58
  • 7
  • I'm running PHP on Windows and I can't work out how to get imagick added. I know SO isn't really for this kind of question, but would you have any idea how to get it working on Windows? – Niet the Dark Absol Feb 20 '12 at 06:47
  • You'll need the windows installer from here to install on your system first, but the usual installation of PHP extensions don't apply to Windows so I'm afraid I can't share much experience there. This question on SO might help though. =) Cheers! – jsamraj Feb 20 '12 at 15:30