0

What I want to do is to save only the first frame of an animated GIF in static image (non animated gif).

Using Gmagick 1.1.2RC1 and GraphicMagick 3.1.18

I read heaps of posts talking about it. Some says that it was working in the previous version of Gmagick but not in the new one.

Here is my current test code:

public function testAnimatedGifFrame()
{
    $testAnimGif = $this->assets.'/animated.gif';

    $im = new \Gmagick($testAnimGif);
    $frameNumber = $im->getNumberImages();

    $this->assertEquals(47, $frameNumber, 'The number of frame for the animated GIF should be 47');

    // Select the first frame and save it
    $frameIndex = 0;
    $img = $im->coalesceImages();
    foreach ($img as $frame) {
        die('here');
        $frameName = ++$frameIndex . '.gif';
        $frame->writeImage( $frameName );
    }
}

The animated GIF is composed of 47 frames, but when using coalesceImages(), the script is never getting inside the foreach loop (it's never dying).

Not sure what I've missed here.

maxwell2022
  • 2,818
  • 5
  • 41
  • 60
  • creating a copy of the gif using `GD` library will save the gif as a static image since you need a library to resize w/frames. – Class May 27 '13 at 06:01
  • Are you sure the $img is an array? If it isnt, foreach wont go "inside". – Uberfuzzy May 27 '13 at 06:05
  • Also, are you sure its making it past that assert? – Uberfuzzy May 27 '13 at 06:06
  • @Class I don't want to use `GD`. With `GD` it would work for sure because it does not support animation. @Uberfuzzy yes it's passing the first assert and yes apparently it is supposed to be an array according the documentation. – maxwell2022 May 27 '13 at 06:28

1 Answers1

0

For those looking to do it using Imagick or Gmagick like I was.

Just save it as a JPG and BAM!

public function testAnimatedGifFrame()
{
    $testAnimGif = $this->assets.'/animated.gif';
    $singleFrame = $this->assets.'/test_single_frame.jpg';

    $im = new \Gmagick($testAnimGif);
    $frameNumber = $im->getNumberImages();

    $this->assertEquals(47, $frameNumber, 'The number of frame for the animated GIF should be 47');

    // Save the image as JPG to disable the animation
    $im->setImageFormat('JPG');
    $im->writeImage($singleFrame);
    $im->destroy();
}

If you want to save the last image of the animation instead of the first one, you just need to add $im = $im->flattenImages(); before $im->setImageFormat('JPG');

I hope this will help some of you ;)

maxwell2022
  • 2,818
  • 5
  • 41
  • 60