php - Show transparent png image over another one

I'm trying to overlap a transparent png image over another png. I followed other answers but I didn't manage to solve my problem. Here it is what I've done so far:
header("Content-type: image/png");
$im = imagecreatefrompng("image/orange.png");
$trans_background = imagecolorallocate($im, 0, 0, 0, 127);
$logo = imagecreatefrompng($data["badgeUrls"]["medium"]);
imagesavealpha($logo, true);
imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, 200, 200);
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
and this is the result: result
EDIT: logo.png it's transparent. If I try this code on html using the same link, it works:
<!DOCTYPE html>
<html>
<body style="background-color:powderblue;">
<img src="https://api-assets.clashofclans.com/badges/200/lTvtX122PSoz5wXzrzp5mFlw0y-72zVviKvuy9cXOFs.png">
</body>
</html>
Result 2: pic
Answer
Solution:
I have modified your code by commenting some lines and adjusting the dimensions to prevent the black background:
<?php
header("Content-type: image/png");
$im = imagecreatefrompng("orange.png");
//$trans_background = imagecolorallocate($im, 0, 0, 0, 127);
$logo = imagecreatefrompng('logo.png'); //logo image dimensions = 64 x 64 px
//imagesavealpha($logo, true);
//imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, 64, 64); // dimensions of the logo image 64 * 64 px
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
The envelope image in the screen shot is transparent png image of dimensions 64 * 64 px.
Update:
If you want to add a custom background to the logo image and you want to determine its dimensions dynamically, you will have to useimagesx()
for getting width andimagesy()
for getting height. Also, you have to useimagecolorallocatealpha()
in your code instead ofimagecolorallocate()
<?php
header("Content-type: image/png");
$im = imagecreatefrompng("orange.png");
$trans_background = imagecolorallocatealpha($im, 0, 0, 100, 50);
// Blue background // to be transparent imagecolorallocatealpha ($im, 0,0,0,127)
$logo = imagecreatefrompng('logo2.png');
imagesavealpha($logo, true);
imagefill($logo, 0, 0, $trans_background);
imagecopy($im, $logo, 100, 100, 0, 0, imagesx($logo), imagesy($logo));
imagepng($im);
imagedestroy($logo);
imagedestroy($im);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[hy000] [1698] access denied for user 'root'@'localhost'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.