想象一下:从动画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++;
}
所以:
- 创建新图片
- 迭代orignal的图片
- 如果当前框架是我想要的框架保持,将其复制到新图像
最后,将图像输出到浏览器:
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通常不是那么大...... 是吗?
其他方式可能是从命令行使用转换...但是......不是很好,我没有找到只需删除: - (
这篇关于想象一下:从动画GIF中删除帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!