2016-03-28 83 views
1

使用php代码合并PNG背景上的用户图像。以下是我使用的代码。如何在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

我想在圆圈用户图像回来。

+0

http://stackoverflow.com/questions/999251/crop-or-mask-an-image-into-a-circle/999563#999563 – C2486

回答

1

您正在复制背景图像上的JPEG图像。

JPEG不支持透明度。

,你可以用GD库做的是:

  • 创建所需大小的新结果图像,然后
  • 复制JPEG(用户图片)其中心,然后
  • 将部分透明的PNG背景(实际上是前景)复制到结果图像上。 PNG背景必须在中间有一个“透明窗口”,以便用户图片不会隐藏在背景后面(换句话说,背景的白色圆圈部分必须透明)。
+0

谢谢@ alex-shesterov它的工作! –

+0

不客气,乐意效劳。 –