我有一个透明背景的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 :(得分: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);
答案 1 :(得分: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);
答案 2 :(得分: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);
现在这两个图像合并并保留了透明度,这两行产生了一个随机的白色背景,希望这有助于其他同样的问题