View Full Version : combining images - png24 (GD)

07-25-2007, 04:08 PM
I'm using imagecopy to combine 2 images. One is just a solid colour created by php, the other is a png-24. Basically I want to put the png-24 on top of the solid colour so that it shows through the transparent and translucent parts.

However what happens is the transparency completely overwrites the underlying pixels, so that any page background colour is visible but not the base image.

Am I just dreaming? Is this going to be possible? If it can be done with GD can it be done with imagemagick?

//Send a generated image to the browser

function create_image()

//Set the image width and height
$width = 220;
$height = 220;

//Create the image resource
$image = ImageCreate($width, $height);

$fill = ImageColorAllocate($image, rand(0,255), rand(0,255), rand(0,255));

//Make the background black
ImageFill($image, 0, 0, $fill);
ImagePNG($image, 'testcolor.png');

$overlay = imagecreatefrompng('ponyshade.png');
imageAlphaBlending($overlay, true);
imageSaveAlpha($overlay, true);

//Output the newly created image in jpeg format
ImagePNG($image, 'test.png');
ImagePNG($overlay, 'testover.png');
//Free up resources
<div style = "background-color:#FF00FF">
<image src = "testcolor.png"/>
<image src = "test.png"/><image src = "testover.png"/>

07-25-2007, 04:46 PM
I'm not sure about this but I found this (http://www.sitepoint.com/article/watermark-images-php) which uses imagecopymerge (http://uk.php.net/manual/en/function.imagecopymerge.php) instead of imagecopy.

07-25-2007, 04:52 PM
imagecopymerge doesnt respect alphatransparency.

...looks like I've found the problem. The background image has to be imagecreatetruecolor - not just imagecreate. All works fine now.