【问题标题】:How to flatten an animated GIF in a static image using Gmagick如何使用 Gmagick 将静态图像中的动画 GIF 展平
【发布时间】:2013-05-27 05:59:32
【问题描述】:

我想要做的是仅将动画 GIF 的第一帧保存在静态图像(非动画 gif)中。

使用 Gmagick 1.1.2RC1 和 GraphicMagick 3.1.18

我读了很多关于它的帖子。有人说它在以前版本的 Gmagick 中有效,但在新版本中无效。

这是我当前的测试代码:

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

动画 GIF 由 47 帧组成,但在使用 coalesceImages() 时,脚本永远不会进入 foreach 循环(它永远不会死)。

不知道我在这里错过了什么。

【问题讨论】:

  • 使用 GD 库创建 gif 的副本会将 gif 保存为静态图像,因为您需要一个库来调整带框架的大小。
  • 你确定 $img 是一个数组吗?如果不是,foreach 不会进入“内部”。
  • 另外,你确定它通过了这个断言吗?
  • @Class 我不想使用GD。使用GD 肯定会工作,因为它不支持动画。 @Uberfuzzy 是的,它通过了第一个断言,是的,根据文档,它显然应该是一个数组。

标签: php animated-gif gmagick


【解决方案1】:

对于那些希望像我一样使用 Imagick 或 Gmagick 的人。

只需将其保存为 JPG 和 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();
}

如果要保存动画的最后一张图片而不是第一张,只需要在$im->setImageFormat('JPG');之前添加$im = $im->flattenImages();

我希望这会对你们中的一些人有所帮助;)

【讨论】:

    猜你喜欢
    • 2012-06-25
    • 2011-02-09
    • 2011-08-10
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    相关资源
    最近更新 更多