使用 PHP 的 ImageMagick API 制作动画 GIF [英] Make an animated GIF with PHP's ImageMagick API

查看:60
本文介绍了使用 PHP 的 ImageMagick API 制作动画 GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我的操作系统中轻松完成

I can do it easily in my OS

convert -delay 1/1 -loop 0 *.gif animated.gif

但我无法在 PHP API 中找到如何执行此操作.无需调整大小或任何需要,我只有一组需要动画的帧.

But I can't find how to do this in the PHP API. No resizing or anything needed, I've just got a set of frames that need animating.

推荐答案

虽然我不是 PHP 专家,但我知道这个问题并不难.您想要做的是创建一个 Imagick 对象,您可以将帧附加到该对象.对于每一帧,您都可以更改时间等参数.

While I'm not a PHP expert, I know that this issue isn't a too difficult one. What you want to do is create an Imagick object that you can append your frames to. With each frame you can change parameters like timing etc.

假设您正在处理从基本 Web 表单上传的图像,我编写了一个基本示例,该示例循环遍历以image0"名称上传的图像,其中0"上升到任意多个文件包括在内.您自然可以通过对固定文件名或其他任何内容使用相同的方法来添加图像.

Assuming you're working with images that are uploaded from a basic web form, I've written a basic example that loops through images that were uploaded with a name of "image0", where "0" goes up to however many files are included. You could naturally just add images by using the same methods on fixed file names or whatever.

$GIF = new Imagick();
$GIF->setFormat("gif");

for ($i = 0; $i < sizeof($_FILES); ++$i) {
    $frame = new Imagick();
    $frame->readImage($_FILES["image$i"]["tmp_name"]);
    $frame->setImageDelay(10);
    $GIF->addImage($frame);
}

header("Content-Type: image/gif");
echo $GIF->getImagesBlob();

这个例子创建了一个 Imagick 对象,它将成为我们的 GIF.然后循环上传到服务器的文件,首先读取每个文件(但请记住,此技术依赖于图像的命名如上所述),其次获得延迟值,第三次将其附加到即将成为的 GIF.这是基本的想法,它会产生你所追求的(我希望).

This example creates an Imagick object that is what will become our GIF. The files that were uploaded to the server are then looped through and each one is firstly read (remember however that this technique relies on that the images are named as I described above), secondly it gets a delay value, and thirdly, it's appended to the GIF-to-be. That's the basic idea, and it will produce what you're after (I hope).

但是有很多东西需要篡改,而且您的配置可能看起来有所不同.我总是发现 php.net Imagick API 引用有点烂,但它仍然很好我不时使用它来引用标准 ImageMagick 中的内容.

But there's lot to tamper with, and your configuration may look different. I always found the php.net Imagick API reference to kind of suck, but it's still nice to have and I use it every now and then to reference things from the standard ImageMagick.

希望这有点符合您的要求.

Hope this somewhat matches what you were after.

这篇关于使用 PHP 的 ImageMagick API 制作动画 GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆