How to merge two PNGs (one with transparency)?


I want to be able to create create an image out of the a profile picture with a another image being overlaid on it.

The Banned Image I want to use

(Image I want to be overlaid)

The profile pictures have different widths and heights and I want to let the PHP script "stretch" the banned.png over the profile picture, depending on the size of the profile picture.

This is the script I currently use to create the new image to make put the profile picture in grayscale:

function ImageToGreyscale($imagepath, $username){
$image_1_file = $imagepath.$username.'.png';    
$image_2_file = $imagepath.'/scripting/banned.png';
$image_1_size = getimagesize($image_1_file);
$image_1_w = $image_1_size[0];
$image_1_h = $image_1_size[1];
$image_1 = imagecreatefrompng($image_1_file);
$image_2 = imagecreatefrompng($image_2_file);
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagefilter($image_1, IMG_FILTER_GRAYSCALE);
imagecopy($image_1, $image_2, 0, 0, 0, 0, $image_1_w, $image_1_h);
imagepng($image_1, $imagepath.$username.'_banned.png');
imagedestroy($image_1);

}

I want to put the red "Banned" over it.

This is what I want to have as Result:

The original Image

(original)

The edited Image

(edited)

Could you help me with that?


Answers:


Yes this works but you check and adjust the coordinate based on your need.

$image_1 = imagecreatefrompng('girl.png');
$image_2 = imagecreatefrompng('banned.png');
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 100, 100, 100, 100, 100, 100);
imagepng($image_1, 'image_3.png');
imagedestroy($image_1);

OUT 1 enter image description here

Answer 2 As expected output, May try with other images

$image_1_file = 'girl.png';    
$image_2_file = 'banned.png';
$image_1_size = getimagesize($image_1_file);
$image_2_size = getimagesize($image_2_file);
$image_1_w = $image_1_size[0];
$image_1_h = $image_1_size[1];
$image_1 = imagecreatefrompng($image_1_file);
$image_2 = imagecreatefrompng($image_2_file);
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagefilter($image_1, IMG_FILTER_GRAYSCALE);
imagecopyresampled($image_1, $image_2, 0, 0, 0, 0, $image_1_w, $image_1_h, $image_2_size[0], $image_2_size[1]);
imagepng($image_1,'girl_banned.png');

OUT 2 enter image description here