在 PHP 中将 PNG 放在 JPG 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2269363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:48:58  来源:igfitidea点击:

Put PNG over a JPG in PHP

phpimageimage-processinggd

提问by Chris

I want to do the following in PHP:

我想在 PHP 中执行以下操作:

I have two images, a jpg and a png. I want to resize the jpg to the same size as the png then put the png on top. The PNG has transparency so I would like to preserve that so the jpg shows underneath.

我有两个图像,一个 jpg 和一个 png。我想将 jpg 调整为与 png 相同的大小,然后将 png 放在顶部。PNG 具有透明度,所以我想保留它,以便 jpg 显示在下面。

If anyone could help that would be great!

如果有人可以提供帮助,那就太好了!

Thanks

谢谢

回答by skyman

<?
$png = imagecreatefrompng('./mark.png');
$jpeg = imagecreatefromjpeg('./image.jpg');

list($width, $height) = getimagesize('./image.jpg');
list($newwidth, $newheight) = getimagesize('./mark.png');
$out = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($out, $jpeg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($out, $png, 0, 0, 0, 0, $newwidth, $newheight, $newwidth, $newheight);
imagejpeg($out, 'out.jpg', 100);
?>

回答by webkul

This is the working code which i using

这是我使用的工作代码

$dest = imagecreatefrompng('mapCanvas.png');
$src = imagecreatefromjpeg('si.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
// Copy and merge
imagecopymerge($dest, $src, 17, 13, 0, 0, 60, 100, 100);

// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);

imagedestroy($dest);
imagedestroy($src);

回答by TheJacobTaylor

Here is a link to an example that will overlay a transparent watermark onto an image. Might be your use case, might be related.

这是一个示例的链接,该示例将透明水印叠加到图像上。可能是你的用例,可能是相关的。

http://www.php.net/manual/en/image.examples.merged-watermark.php

http://www.php.net/manual/en/image.examples.merged-watermark.php

There is also a way to load JPG images, resize images, turn on alpha tracking, and export images in GD.

还有一种方法可以加载 JPG 图像、调整图像大小、打开 alpha 跟踪以及在 GD 中导出图像。

Jacob

雅各布