2010-11-11 133 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); 

现在这两个图像合并并保留了透明度,这两行代码产生了一个随机的白色背景,希望这可以帮助其他人解决同一问题