2016-03-28 85 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

不客氣,樂意效勞。 –