Php merge of two images looks broken

I am trying to combine two transparent images using php, but the image has some black border and spots that I couldn’t understand the problem below is my attached code

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

test sample:

see in action: http://goo.gl/qMWNB4

image url: http://goo.gl/pR59MT

+4
source share
2 answers

OK, so I did it:

a) make a mess of your code :) b) get rid of enough black that I'm sure you have finished cna!

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

Hope you arrive soon - get back to day work for me :)

0
source

imagecolortransparent. , , - .

, , - :

$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

0

Source: https://habr.com/ru/post/1629336/


All Articles