我正在尝试理解如何从动态GIF中删除帧。
目前,我正在尝试以下方法(作为测试):
$count = 1;
foreach ($_im AS $frame) {
if ($count > 1) { $frame->removeImage(); }
$count++;
}
然而,这似乎会将对象中的所有内容都烤焦。
同事们提出的建议是创建另一个IM对象,并从中提取一个 famee,等等。但这似乎非常混乱。
我正在尝试理解如何从动态GIF中删除帧。
目前,我正在尝试以下方法(作为测试):
$count = 1;
foreach ($_im AS $frame) {
if ($count > 1) { $frame->removeImage(); }
$count++;
}
然而,这似乎会将对象中的所有内容都烤焦。
同事们提出的建议是创建另一个IM对象,并从中提取一个 famee,等等。但这似乎非常混乱。
Imagick
文档,并尝试了一些方法...但是我也没能做到你想要的,--所以,至少我们有两个人找不到一个干净的方式^^
无论如何,我唯一成功删除动态GIF图像帧的方法是创建一个新的图像,只包含我不想删除的帧 :-(
考虑到我是这样加载图片的:
// Load the existing image
$image = new Imagick(dirname(__FILE__) . '/animated-gif-source.gif');
(这是一个动态gif,有3帧;我想要“删除”第二帧)。
正如我所说的,我找到的唯一删除帧的方法是这个:
$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++;
}
所以:
最后,将图像输出到浏览器:
// 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);
我只用过IM的命令行工具。
convert srcImage.gif[0] dstImage.gif
这应该就可以了,除非我忘记了某个选项。
[0]指代动画gif的第一帧。