我正在尝试使用php合并两个透明图像但图像有一些黑色边框和斑点我无法弄清楚下面的问题是我的附加代码
$image1=$image2='imagepath.png';
imagealphablending($image2, true);
imagesavealpha($image2, true);
$w=imagesx($image1);
$h=imagesy($image1);
$final = imagecreatetruecolor($w, $h);
$black = imagecolorallocate($final, 0, 0, 0);
$backgroundColor = imagecolortransparent($final, $black);
$percent = 0.583;
$new_width = $w * $percent;
$new_height = $h * $percent;
$wshift = $w/8.5;
$hshift = $h/2.5;
imagecopy($final, $image1, 0,0,0,0,$w,$h);
imagecopyresized($final, $image2,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
header('Content-Type: image/png');
imagepng($final);
imagedestroy($image2);
imagedestroy($image1);
imagedestroy($final);
样本测试:
查看实际操作:http://goo.gl/qMWNB4
图片网址:http://goo.gl/pR59MT
答案 0 :(得分:0)
好的,所以我已经去了,并设法:
a)弄乱你的代码:) b)摆脱足够的黑色,以确保你完成这项工作!
$image1=$image2=imagecreatefrompng('test1.png');
imagealphablending($image2, false);
imagesavealpha($image2, true);
$w=imagesx($image1);
$h=imagesy($image1);
$final = imagecreatetruecolor($w, $h);
$percent = 0.583;
$new_width = $w * $percent;
$new_height = $h * $percent;
$wshift = $w/8.5;
$hshift = $h/2.5;
$temp = imagecreatetruecolor($new_width, $new_height);
imagecopymerge ($final, $image1, 0,0,0,0, $w , $h , 100);
imagecopymerge ($temp, $image2, 0,0,0,0, $w, $h, 100);
$black2 = imagecolorallocate($temp, 0, 0, 0);
$backgroundColor = imagecolortransparent($temp, $black2);
imagecopyresized($final, $temp,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
$black = imagecolorallocate($final, 0, 0, 0);
$backgroundColor = imagecolortransparent($final, $black);
imagepng($final, 'output8.png');
希望你能很快到达那里 - 回到我的日常工作:)
答案 1 :(得分:0)
黑色边框是使用imagecolortransparent
引起的。这适用于基于调色板的图像,而不是真正的颜色,并且在整个文件中使用单一颜色来表示透明度,而不是每个像素的alpha值。
让它发挥作用的技巧是在正确的时间设置正确的混合模式:
$img = imagecreatefrompng('35477413.png');
$w = imagesx($img);
$h = imagesy($img);
$final = imagecreatetruecolor($w, $h);
imagesavealpha($final, true);
$percent = 0.583;
$new_width = $w * $percent;
$new_height = $h * $percent;
$wshift = $w / 8.5;
$hshift = $h / 2.5;
// disable alpha blending so that transparent pixels replace target pixels.
imagealphablending($final, false);
imagecopy($final, $img, 0, 0, 0, 0, $w, $h);
// enable alpha blending so that transparent pixels blend with target pixels.
imagealphablending($final, true);
imagecopyresized($final, $img, $wshift, $hshift, 0, 0, $new_width, $new_height, $w, $h);
header('Content-Type: image/png');
imagepng($final);
imagedestroy($img);
imagedestroy($final);
这给出了以下输出: