Imagick:从动画 GIF 中删除帧? [英] Imagick: Remove frames from an animated GIF?
问题描述
我想了解如何从动画 GIF 中删除帧.
I am trying to understand how to remove frames from an animated GIF.
目前我正在尝试这个(作为测试):
Currently I am trying this (as a test):
$count = 1;
foreach ($_im AS $frame) {
if ($count > 1) { $frame->removeImage(); }
$count++;
}
然而,这似乎烤了对象中的所有东西.
However this seems to toast everything in the object.
同事的建议是创建另一个 IM 对象,然后将一个名人提取到其中,等等.然而,这看起来非常混乱.
Suggestions from workmates have been to just create another IM object, and extract a famee into it, etc. That seems extremely messy however.
推荐答案
我一直在浏览 Imagick
文档一段时间,并尝试了一些东西......但我没有设法做你想要的--所以,我们是至少有两个找不到干净的方法^^
无论如何,我设法删除动画 GIF 图像的帧的唯一方法是创建一个新的,只包含我不想删除的帧:-(
Anyway, the only way I managed to remove a frame for an animated GIF image was by creating a new one, containing only the frames I didn't want to remove :-(
考虑到我以这种方式加载了图像:
Considering I loaded an image this way :
// Load the existing image
$image = new Imagick(dirname(__FILE__) . '/animated-gif-source.gif');
(这是一个动画 gif,有 3 帧;我想删除"第二帧).
正如我所说,我发现删除框架"的唯一方法是这个:
As I said, only way I found to "remove a frame" is this one :
$new_image = new Imagick();
$i = 1;
foreach ($image as $frame) {
if ($i===1 || $i===3) {
// 3 frames ; we keep the first and third one
// ie, remove the second one
$new_image->addImage($frame->getImage());
}
$i++;
}
所以:
- 创建新图像
- 遍历原始帧
- 如果当前帧是我想保留的帧,请将其复制到新图像
最后,将图像输出到浏览器:
And, in the end, to output the image to the browser :
// To directly output to the browser
header('Content-Type: image/gif');
echo $new_image->getImagesBlob();
或者,将其写入文件:
// To write the new image to a file
// Must use writeImages, and not writeImage (multi-frames ! )
$new_image->writeImages(dirname(__FILE__) . '/animated-gif-output.gif', true);
这些输出中的每一个只包含第一帧和第三帧;所以,它的工作...
但是,正如你所说,感觉不太好:-(
Each one of these outputs only contain the first and third frames ; so, it's working...
But, as you said, it doesn't feel good :-(
我认为,它可能适用于大多数图像;您可能会遇到大图像的问题,但动画 GIF 通常没有那么大...是吗?
其他方法可能是从命令行使用转换......但是......不是那么好,我没有找到一种方法来删除带有 :-(
这篇关于Imagick:从动画 GIF 中删除帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!