在 PHP 中将一张图像添加到另一张图像的底部

发布于 2024-10-04 13:28:15 字数 263 浏览 3 评论 0原文

我想在 php 中将一张图像添加到另一张图像的底部

我用这个来加载图像:

//load top
$top = @imagecreatefrompng($templateTop);
//load bottom
$bottom = @imagecreatefrompng($templateBottom);

现在我想将它们添加到一张图片中并一起显示顶部和底部。

我可以用什么方法来做到这一点?

谢谢!

I'd like to add one image to the bottom of another in php

I've this to load the images:

//load top
$top = @imagecreatefrompng($templateTop);
//load bottom
$bottom = @imagecreatefrompng($templateBottom);

Now I'd like to add them to one picture and display top and bottom together.

What way can I do this?

Thanks!

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

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

发布评论

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

评论(3

拥抱没勇气 2024-10-11 13:28:15

使用 imagecopy

$top_file = 'image1.png';
$bottom_file = 'image2.png';

$top = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);

// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);

// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;

// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);

// save to file
imagepng($new, 'merged_image.png');

Use imagecopy:

$top_file = 'image1.png';
$bottom_file = 'image2.png';

$top = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);

// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);

// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;

// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);

// save to file
imagepng($new, 'merged_image.png');
注定孤独终老 2024-10-11 13:28:15

为了实现这一目标,你必须
a) 合并图像并将结果存储在文件中
b) 生成一个合适的标签来指向它。
c) 避免再次使用该文件名,直到该人离开。

如果您只想将两个图像组合一次,请使用图像魔法。

如果您经常想要一张一张地显示两个图像,请使用合适的 html 来执行此操作,并让浏览器执行此操作。

例如,将图像放入

<div><div><img.../></div><div><img .../></div></div> 

您以正常方式使用 php 生成的图像中。
(这比让标签出现在这里更容易:)

To achieve this you would have to
a) Combine the image and store the result in a file
b) generate a suitable tag to point to it.
c) Avoid using that filename again, until that person had left.

If you want to combine two images just once, then use image magic.

If you frequently want to display two images one under the other, do so using suitable html, and let the browser do it.

E.g. Put the images in a

<div><div><img.../></div><div><img .../></div></div> 

which you generate with php in the normal way.
(Which is easier than getting tags to appear here :)

话少情深 2024-10-11 13:28:15
$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";

$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);


// Place "photo_to_paste.png" on "white_image.png"
imagecopy($im, $im2, 20, 10, 0, 0, imagesx($im2), imagesy($im2));

// Save output image.
imagepng($im, "output.png", 0);
$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";

$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);


// Place "photo_to_paste.png" on "white_image.png"
imagecopy($im, $im2, 20, 10, 0, 0, imagesx($im2), imagesy($im2));

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