2017-07-06 203 views
2

我需要合併下面的圖像,但我的解決辦法是行不通的。用PHP合併兩個PNG圖像。輸出圖像的變化和錯誤的不透明度

我的代碼是下面(I通過GET參數傳遞的圖像的URL提供給腳本)

<?php 
$dest = imagecreatefrompng($_GET['img1']); 
$src = imagecreatefrompng($_GET['img2']); 

imagecopymerge($dest, $src, 0, 0, 0, 0, 1500, 1500, 50); 

$white = imagecolorallocate($dest, 255, 255, 255); 
imagecolortransparent($dest, $white); 

header('Content-Type: image/png'); 
imagepng($dest); 
?> 

兩個圖像與透明背景PNG又都是1500x1500。

第一張圖片:

enter image description here

2圖像:

enter image description here

我能得到什麼: enter image description here

爲什麼我不能作出最後的圖像有正確的不透明?我試圖改變的imagecopymerge()爲0或100,但在這種情況下的最後一個值我只得到一個圖像或其他。我需要他們兩個,完全重疊在一起!

另外,如果你周圍的最終圖像在寶石仔細一看,有一些額外的藍色......這怎麼可能?

enter image description here

回答

0

這是通過設置所產生的圖像通過您的呼叫imagecolortransparent它設置任何白色(#ffffff)像素以100%的透明使用圖像透明度,而不是每像素alpha通道透明度造成的。

產生的圖像上的人工產物('額外的藍色'和邊緣像素)是由未保存的原始圖像的alpha通道透明度和像素(100%透明但不是白色)原始的顏色。

你的結果的「洗出」看來自告訴imagecopymerge合併用50%透明度的兩個圖像。

的解決方案是使用正確的Alpha設置:

<?php 

$dest = imagecreatefrompng($_GET['img1']); 
$src = imagecreatefrompng($_GET['img2']); 

imagesavealpha($dest, true); 

imagecopy($dest, $src, 0, 0, 0, 0, 1500, 1500); 

header('Content-Type: image/png'); 
imagepng($dest); 
+0

@wiredmark這是否解決問題了嗎? – timclutton