2010-11-11 256 views
1

我有一個透明背景的PNG水印圖像。但隨機產生一個白色的背景,而不是保持透明。GD:使PNG透明的白色背景

// Watermark 
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png'); 
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png'); 

// Combinde watermark image with image already generated in $dst 
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight); 

解決方案是增加:

imagealphablending($dst, true); 
imagesavealpha($dst, true); 

完整代碼:

// Watermark 
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png'); 
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png'); 

imagealphablending($dst, true); 
imagesavealpha($dst, true); 

// Combinde watermark image with image already generated in $dst 
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, 

回答

0

使用alpha通道保存$dst,而不是$watermark

// Watermark 
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png'); 
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png'); 

imagealphablending($dst, false); 
imagesavealpha($dst, true); 

// Combinde watermark image with image already generated in $dst 
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight); 
+0

只需添加imagealphablending($ dst,true); imagesavealpha($ dst,true);訣竅。感謝您讓我走上正軌。 – Cudos 2010-11-12 10:01:09

0

嘗試imagecopymerge代替imagecopy的

編輯:試試這個代碼:

header('Content-type: image/jpeg'); 
$dst = imagecreatefromjpeg($image_path); 
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png'); 
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png'); 
imagecopymerge($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight, 100); 
imagejpeg($dst,'',90); 
imagedestroy($dst); 
+0

這隻留下一個黑盒子應該在哪裏水印。 – Cudos 2010-11-11 16:45:42

0

我有同樣的問題,但對我來說,得到它的工作我註釋掉從我的兩行代碼:

imagesavealpha($image_1, true); 
imagesavealpha($image_2, true); 

所以我的代碼是這樣的:

$image_1 = imagecreatefrompng("example26_".$acct.".png"); 
$image_2 = imagecreatefrompng('example27.png'); 
imagealphablending($image_1, true); 
imagealphablending($image_2, true); 
//imagesavealpha($image_1, true); 
//imagesavealpha($image_2, true); 
imagecopy($image_1, $image_2, 0, 0, 0, 0, 1350, 250); 
header("Content-Type: image/png"); 
imagepng($image_1); 

現在這兩個圖像合併並保留了透明度,這兩行代碼產生了一個隨機的白色背景,希望這可以幫助其他人解決同一問題