...

View Full Version : combining images - png24 (GD)



NancyJ
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?



<?php
//Send a generated image to the browser
create_image();


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);
imagecopy($image,$overlay,0,0,0,0,220,220);


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

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum