Imagick: remove frames from animated GIF?

I am trying to figure out how to remove frames from an animated GIF.

I'm currently trying to do this (as a test):

$count = 1; foreach ($_im AS $frame) { if ($count > 1) { $frame->removeImage(); } $count++; } 

However, this is similar to the toast of everything in the facility.

The suggestions from teammates were simply to create another IM object and extract a fame, etc. However, this seems extremely messy.

+4
source share
2 answers

I Imagick through the Imagick documentation for a while and tried a couple of things ... But I'm not "i.e. we are at least two who cannot find the clear path ^^

In any case, the only way to remove the frame for the animated GIF image is to create a new one containing only those frames that I did not want to delete : - (


Given that I uploaded the image this way:

 // Load the existing image $image = new Imagick(dirname(__FILE__) . '/animated-gif-source.gif'); 

(This is an animated gif with 3 frames, I want to β€œdelete” the second).


As I said, only one way that I found β€œdelete frame” is as follows:

 $new_image = new Imagick(); $i = 1; foreach ($image as $frame) { if ($i===1 || $i===3) { // 3 frames ; we keep the first and third one // ie, remove the second one $new_image->addImage($frame->getImage()); } $i++; } 

So:

  • create new image
  • frame iteration orignal
  • If the current frame is the one I want to save, copy it to a new image


And finally, to output the image to the browser:

 // To directly output to the browser header('Content-Type: image/gif'); echo $new_image->getImagesBlob(); 

Or, to write it to a file:

 // To write the new image to a file // Must use writeImages, and not writeImage (multi-frames ! ) $new_image->writeImages(dirname(__FILE__) . '/animated-gif-output.gif', true); 

Each of these outputs contains only the first and third frames; therefore, it works ...
But, as you said, he is not feeling well : - (


I think this will probably work just fine for most images; you may run into problems with large images, but animated GIFs are usually not that big ... are they?


Another way could be to use the conversion from the command line ... But ... not so great, and I did not find a way to just delete the frame using these : - (

+5
source

I used only command line utilities for IM.

convert srcImage.gif [0] dstImage.gif

Gotta do the trick if I don't forget the option.

[0] refers to the first frame of the animated gif.

+2
source

All Articles