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 关于CMOS电路图的提问
  • ¥15 Verilog hdl密码锁设计
  • ¥15 CreateFile无法获取特殊文件内容
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥30 vue 页面窗口放大或者缩小元素会变化