PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How merge image on background png image in php

 October 10, 2022     gd, php     No comments   

Issue

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.


Solution

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


Answered By - Alex Shesterov
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing