使用 PHP 的 ImageMagick API 制作动画 GIF

发布于 2025-01-08 17:32:05 字数 160 浏览 4 评论 0原文

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

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

,但我找不到如何在 PHP API 中执行此操作。不需要调整大小或任何需要的东西,我只有一组需要动画的框架。

I can do it easily in my OS

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2025-01-15 17:32:05

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

假设您正在处理从基本 Web 表单上传的图像,我编写了一个基本示例,该示例循环使用名称为“image0”上传的图像,其中“0”表示包含的文件数量。您自然可以通过对固定文件名或其他名称使用相同的方法来添加图像。

$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。这是基本的想法,它会产生你想要的东西(我希望)。

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

希望这在某种程度上符合您的需求。

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.

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();

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文