douwengzao5790 2013-05-27 05:59
浏览 31
已采纳

如何使用Gmagick在静态图像中展平动画GIF

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.

  • 写回答

1条回答 默认 最新

  • douyong6585 2013-05-28 06:12
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式