如何在 php 中合并背景 png 图像上的图像
How merge image on background png image in php
我正在使用 php 代码在 png 背景上合并用户图像。下面是我正在使用的代码。
$width = 140;
$height = 140;
$bottom_image = imagecreatefrompng("bg.png");
$top_image = imagecreatefromjpeg("default.jpg");
imagesavealpha($top_image, true);
imagealphablending($top_image, false);
imagecopyresampled($bottom_image, $top_image, 290, 125, 0, 0, $width, $height, $width, $height);
//imagecopy($bottom_image, $top_image, 290, 125, 0, 0, $width, $height);
header('Content-type: image/png');
imagepng($bottom_image);
but i got this result when i save image
我想要圆圈后面的用户图片。
您正在将 JPEG 图像复制到背景图像上。
JPEG 不支持透明度。
你可以用 gd 库做的是:
- 创建所需大小的新结果图像,然后
- 将 JPEG(用户图片)复制到其中心,然后
- 将部分透明的 PNG 背景(实际上是前景)复制到结果图像上。 PNG背景必须在中间有一个"transparent window",这样用户图片就不会隐藏在背景后面(换句话说,背景的白色圆圈部分必须是透明的)。
我正在使用 php 代码在 png 背景上合并用户图像。下面是我正在使用的代码。
$width = 140;
$height = 140;
$bottom_image = imagecreatefrompng("bg.png");
$top_image = imagecreatefromjpeg("default.jpg");
imagesavealpha($top_image, true);
imagealphablending($top_image, false);
imagecopyresampled($bottom_image, $top_image, 290, 125, 0, 0, $width, $height, $width, $height);
//imagecopy($bottom_image, $top_image, 290, 125, 0, 0, $width, $height);
header('Content-type: image/png');
imagepng($bottom_image);
but i got this result when i save image
我想要圆圈后面的用户图片。
您正在将 JPEG 图像复制到背景图像上。
JPEG 不支持透明度。
你可以用 gd 库做的是:
- 创建所需大小的新结果图像,然后
- 将 JPEG(用户图片)复制到其中心,然后
- 将部分透明的 PNG 背景(实际上是前景)复制到结果图像上。 PNG背景必须在中间有一个"transparent window",这样用户图片就不会隐藏在背景后面(换句话说,背景的白色圆圈部分必须是透明的)。