Harpreet Singh
Harpreet Singh

Reputation: 13

How merge image on background png image in php

Im using php code to merge user image on png background. Here below is code i am use.

$width = 140; 
$height = 140; 
$bottom_image = imagecreatefrompng("bg.png"); 
$top_image = imagecreatefromjpeg("default.jpg"); 
imagesavealpha($top_image, true); 
imagealphablending($top_image, false); 
imagecopyresampled($bottom_image, $top_image, 290, 125, 0, 0, $width,     $height, $width, $height);
//imagecopy($bottom_image, $top_image, 290, 125, 0, 0, $width, $height); 
header('Content-type: image/png');
imagepng($bottom_image);

but i got this result when i save image

i want user image in round circle back.

Upvotes: 1

Views: 1191

Answers (1)

Alex Shesterov
Alex Shesterov

Reputation: 27585

You are copying a JPEG image over the background image.

JPEG doesn't support transparency.

What you could do with the gd library is:

  • Create a new result image of the desired size, then
  • Copy the JPEG (user picture) to its center, then
  • Copy the partially-transparent PNG background (actually foreground) over result image. The PNG background must have a "transparent window" in the middle so that the user picture doesn't get hidden behind the background (in other words, the white circle part of the background must be transparent).

Upvotes: 1

Related Questions