Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top