Alex Rivera | Logout

How to extract frames of an animated GIF with PHP

Asked 2012-09-23T10:42:11.543
10

I search for a long time how to extract frames from an animated GIF with PHP... unfortunaly I just found how to get their duration...

I really need to extract the GIF frames and their duration to apply some resize, rotate, etc, on each and then to regenerate the GIF with edited frames !

I don't want to use any software, external library (like ImageMagick), just PHP: in fact I need to allow my class http://phpimageworkshop.com/ to work with animated GIF.

If you have any idea, I'm listening you ^^ !

Edit
Report

1 Answer

5

Well, I don't really recommend doing it this way, but here's an option. Animated gifs are actually just a number of gifs concatenated together by a separator, "\x00\x21\xF9\x04". Knowing that, you simply pull the image into PHP as a string, run an explode, and loop through the array running your transformation. The code could look something like this.

$image_string = file_get_contents($image_path);

$images = explode("\x00\x21\xF9\x04", $image_string);

foreach( $images as $image ) {
  // apply transformation
}

$new_gif = implode("\x00\x21\xF9\x04", $images);

I'm not 100% sure of the specifics of re-concatenating, the image, but here's the wikipedia page regarding the file format of animated GIFs.

answered 2012-09-23T14:34:04.147

Your Answer