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

Php 如何使用Gmagick展平静态图像中的动画GIF,php,animated-gif,gmagick,Php,Animated Gif,Gmagick,我想做的是在静态图像(非动画GIF)中只保存动画GIF的第一帧 使用Gmagick 1.1.2RC1和GraphicMagick 3.1.18 我读了很多关于它的帖子。有人说它在Gmagick的前一个版本中起作用,但在新版本中不起作用 以下是我当前的测试代码: public function testAnimatedGifFrame() { $testAnimGif = $this->assets.'/animated.gif'; $im = new \Gmagick($

我想做的是在静态图像(非动画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循环(它永远不会消亡)


不确定我错过了什么。

对于那些希望像我一样使用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=$im->flattimages()
$im->setImageFormat('JPG')之前


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

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