Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with imagecopymerge 1

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
CA
Hi,

I am having trouble laying a PNG with transparency over a jpg using imagecopymerge. I believe I am having one of the following problems:

1. the PNG file is not being loaded with its alpha channel
2. I have an error in the way I'm using imagecopymerge

The result I am getting is a jpg of the PNG with a black background instead of the underlying jpg that I want showing through.

If I comment out the imagecopymerge line, the thumbnail image is built properly. But of course, I am missing the image overlay I need.

I hope it is something that is painfully obvious to someone but just unfamiliar to me. I haven't done much with PHP and images.

Thanks in advance.

Code:
$thumb_src = $tmp;
$thumb = imagecreatetruecolor(197,158);
imagecopyresampled($thumb,$thumb_src,0,0,0,0,197,158,$newwidth,$newheight);
//do the image overlay
//load Image and get its size
$overlay_size = getimagesize("../gallery/_images/template.png");
$overlay_tmp = imagecreatefrompng("../gallery/_images/template.png");

//convert the image to PNG-24
$overlay = imagecreatetruecolor($overlay_size[0],$overlay_size[1]);
imagecopy($overlay,$overlay_tmp,0,0,0,0,$overlay_size[0],$overlay_size[1]);
imagedestroy($overlay_tmp);

//create the image file
imagecopymerge($thumb,$overlay,0,0,0,0,197,158,100);
imagejpeg($thumb,"../gallery/_images/th_".$filename,70);
imagedestroy($thumb);

frozenpeas
 
I tried adding these lines but get the same result.

Code:
$thumb = imagecreatetruecolor(197,158);
[b]imagealphablending($thumb,false);
imagesavealpha($thumb,true);[/b]

Thanks again.

frozenpeas
 
I've been trying some things... with the same results. I'm still getting black showing where the underlying graphic should be seen.

Here is where my code is at this point.

Thanks again.

Code:
$thumb = imagecreatetruecolor(197,158);
imagealphablending($thumb,false);
imagesavealpha($thumb,true);
$transparent = imagecolorallocatealpha($thumb,0,0,0,127);
imagefill($thumb,0,0,$transparent); //fill the image with transparency 

imagecopyresampled($thumb,$thumb_src,0,0,0,0,197,158,$newwidth,$newheight);
//do the image overlay
//load image and get its size
$overlay_size = getimagesize("../gallery/_images/template.png");
$overlay_tmp = imagecreatefrompng("../gallery/_images/template.png");

//convert the Image to PNG-24
$overlay = imagecreatetruecolor($overlay_size[0],$overlay_size[1]);
imagecopy($overlay,$overlay_tmp,0,0,0,0,$overlay_size[0],$overlay_size[1]);
imagedestroy($overlay_tmp);

//create the image file
imagecopymerge($thumb,$overlay,0,0,0,0,197,158,100);
imagejpeg($thumb,"../gallery/_images/th_".$filename,70);
imagedestroy($thumb);

frozenpeas
 
Can you try converting your base .jpg image to .png and try it again. As I know, JPEG doesn't support transparency and transparent pixels will be converted to the default background color, which is usually either black or white.

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
This is a PNG being placed over top of a JPG. The JPG has no areas intended for transparency -- only the PNG does.

frozenpeas
 
Try using imagePng() instead imagejpeg() and see if the resulting image is as you expect it to be.

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
i've had a play and got some meaningful results.

one thing I have noticed is that the overlay degrades materially on being opened with imagecreatefrompng().

this code works for me
Code:
<?php
$image = imagecreatefromjpeg('sample.jpg');
$overlay = imagecreatefrompng('template.png');

//get the size of the main image
$size = getimagesize('sample.jpg');
imagealphablending($overlay, false);

//resize the overlay to match the main image
imagecopyresampled($overlay, $overlay,0,0,0,0,$size[0], $size[1], $size[0], $size[1]);

//merge overlay
imagecopy( $image, $overlay, 0, 0, 0, 0, $size[0], $size[1]);
//create the image file
header("Content-Type: image/png");
imagepng($image);
exit;
?>
 
Interesting. This is working now. I added this line:
Code:
imagealphablending($overlay,false);

and I replaced:

Code:
imagecopymerge($thumb,$overlay,0,0,0,0,197,158,100);

with:

Code:
imagecopy($thumb,$overlay,0,0,0,0,197,158);

and that did the trick.

So it's all working for me now... I'm taking a user-uploaded jpg, making it thumbnail size, putting my own frame over it and saving it as a jpg.

Thanks for your help!

Is imagecopymerge() only for streaming the output image? I thought it was like imagecopy() but with alpha channel support. I didn't expect imagecopy() to support transparency at all. I'm glad I was wrong though.

frozenpeas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top