使用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.