PHP合并两张图片时显示不正常

4
我正在尝试使用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


这实际上既不是虚线边框,也不是边框。这是因为您的图像分辨率较低。 - Matheno
我用300300像素的图像进行了测试,出现了相同的问题。如果使用600600像素的图像仍然出现相同的问题,该怎么办? - saurav
imagecopymerge可能会有一点帮助。http://php.net/manual/zh/function.imagecopymerge.php - Michas
我也尝试过,有没有其他方法来解决这个问题? - saurav
2个回答

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');

希望你能尽快完成 - 对我来说,现在回到日常工作了 :)

我现在又开始尝试了 - 取得了一些进展。这是一个有趣的任务,伙计! - puppyFlo
通常不会,你为什么有一个提议?你已经成功解决了这个问题吗? - puppyFlo
是的,我正在使用你的代码,如果你有兴趣,我还有一些任务。 - saurav
兄弟,但是图片看起来有点问题,我们能不能想办法修复一下呢? - saurav

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);

这将产生以下输出:

result


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接