使用 PHP 从 html 创建动画 gif

发布于 2024-12-12 13:31:48 字数 422 浏览 4 评论 0原文

我有以下简单的 html,需要使用它来创建和保存新的动画 gif:

<div id="wrap">
  <div id="content">
    <p>Message here</p>
    <img src="image.jpg" />
  </div>
  <img src="image.gif" width="709" height="425" />
</div>

代码末尾的 gif 是一个动画 gif - 然后我希望能够在顶部覆盖文本和另一个 jpeg 图形其中,保留gif的动画。

首先,这是否可能,其次,如果可能的话,有人可以指出我正确的方向吗?

我猜我可能需要以某种方式合并 PHPs imagegif 函数?

I have the following simple html which I need to use to create and save a new animated gif:

<div id="wrap">
  <div id="content">
    <p>Message here</p>
    <img src="image.jpg" />
  </div>
  <img src="image.gif" width="709" height="425" />
</div>

The gif towards the end of the code is an animated gif - I would then like to be able to overlay text and another jpeg graphic over the top of this, retaining the animation of the gif.

Firstly is this possible and secondly could someone please point me in the right direction if it is.

I'm guessing I might need to incorporate PHPs imagegif function somehow??

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

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

发布评论

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

评论(1

不疑不惑不回忆 2024-12-19 13:31:48

据我所知,PHP的GD库函数无法生成GIF动画。

您将不得不依赖其他工具,例如 ImageMagik 的 convert 函数(您可以通过 exec 调用它)。

评论后编辑:

如果您只想创建非动画 gif,那么使用 GD 库可以轻松完成该过程。

假设您的文本位于变量 $txt 中,还有两个要堆叠的图像 image1.jpgimage2.gif

最终结果将如下所示

    TEXT
-------------
|           |
|  IMAGE 1  |
|           |
 -----------
-------------
|           |
|  IMAGE 2  |
|           |
 -----------

您首先打开两个图像:

$i1 = imagecreatefromjpeg("image1.jpg");
$i2 = imagecreatefromgif("image2.gif");

现在找到两个图像的大小。

$i1_w = imagesx($i1);
$i1_h = imagesy($i1);
$i2_w = imagesx($i2);
$i2_h = imagesy($i2);

您的最终图像将具有

// Add 30px for the text, you can calculate this precisely 
// using imagettfbbox but be sure to use imagettftext 
// instead of imagestring later
$height = $i1_h + $i2_h + 30;
$width = max($i1_w, $i2_w);

现在您创建输出图像

$img = imagecreatetruecolor($width, $height);

将文本放在顶部

$black = imagecolorallocate($img, 0, 0, 0);
// Instead of using 1 as 2nd parameter you can use a font created 
// with imageloadfont. Also, you may want to calculate text coordinates
// so that it is centered etc.
imagestring($img, 1, 10, 10, $txt, $black);

现在添加图像

imagecopy($img, $img1, ($width-$img1_w)/2, 30, 0, 0, $img1_w, $img1_h);
imagecopy($img, $img2, ($width-$img2_w)/2, 35+$img1_h, 0, 0, $img2_w, $img2_h);

最后,输出 gif

header('Content-Type: image/gif');
imagegif($img); // Or imagejpeg, imagepng etc.

如果您只想保存图像而不显示它,只需执行以下操作:

imagegif($img, "output.gif");

As far as I am aware, PHP's GD library function are not able to generate animated GIFs.

You will have to rely on other tools, such as ImageMagik's convert function (you can call it through exec).

EDIT after comment:

If you just want to create a non-animated gif, then the process is easily done with GD libraries.

Say you have your text in a variable $txt, and two images image1.jpg and image2.gif that you want to stack.

The end result will look like

    TEXT
-------------
|           |
|  IMAGE 1  |
|           |
 -----------
-------------
|           |
|  IMAGE 2  |
|           |
 -----------

You would first open the two images:

$i1 = imagecreatefromjpeg("image1.jpg");
$i2 = imagecreatefromgif("image2.gif");

Now find the size of the two images.

$i1_w = imagesx($i1);
$i1_h = imagesy($i1);
$i2_w = imagesx($i2);
$i2_h = imagesy($i2);

Your final image will have

// Add 30px for the text, you can calculate this precisely 
// using imagettfbbox but be sure to use imagettftext 
// instead of imagestring later
$height = $i1_h + $i2_h + 30;
$width = max($i1_w, $i2_w);

Now you create your output image

$img = imagecreatetruecolor($width, $height);

Put the text on top

$black = imagecolorallocate($img, 0, 0, 0);
// Instead of using 1 as 2nd parameter you can use a font created 
// with imageloadfont. Also, you may want to calculate text coordinates
// so that it is centered etc.
imagestring($img, 1, 10, 10, $txt, $black);

Now add the images

imagecopy($img, $img1, ($width-$img1_w)/2, 30, 0, 0, $img1_w, $img1_h);
imagecopy($img, $img2, ($width-$img2_w)/2, 35+$img1_h, 0, 0, $img2_w, $img2_h);

Finally, output the gif

header('Content-Type: image/gif');
imagegif($img); // Or imagejpeg, imagepng etc.

If you just want to save the image, without showing it just do:

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