On Thu, 2010-05-13 at 00:12 +0100, Alex Davies wrote:
> Hi,
>
> I am trying to import a PNG image from disk, place it on top of a
> transparant image created in GD and output it to the browser. In the case of
> a low opacity setting, I would expect to see the background colour from the
> HTML page.
>
> If I set the opacity to 0, everything works - I end up with a transparant
> image.
>
> However, if I set it for any value >0 (even 1) instead of a
> very-faint-image the whole thing goes black. As the opacity level goes up
> from 0, the amount of black reduces and the amount of imported image
> increases - but this is not what I want.
>
> I am using this code:
>
> <?php
> $src = imagecreatefrompng('test.png');
> $img_width = imagesx($src);
> $img_height = imagesy($src);
>
> // Create trans image
> $dest = imagecreatetruecolor($img_width, $img_height);
> //imagesavealpha($dest, true); // This has no effect it appears
> $trans_colour = imagecolorallocatealpha($dest, 0, 255, 0, 128);
>
> // Make the background transparent
> imagecolortransparent($dest, $trans_colour);
> //imagefill($dest, 0, 0, $trans_colour); // This does not work
>
> // Merge src on top of dest, with opacity of 1 in this case
> imagecopymerge($dest, $src, 0, 0, 0, 0, $img_width, $img_height, 1);
>
> // Output and free from memory
> header('Content-Type: image/png');
> imagepng($dest);
> ?>
>
> The images that this outputs, at opacity levels 0,1 and 80 on a red and
> green background (screenshots of a HTML page) can be downloaded from
> http://www.box.net/shared/h9zn4tjgro
>
> Any help appreciated!
>
> Cheers,
>
> Alex
How exactly are you setting the opacity for the image? The traditional
way is to use imagecolorallocatealpha() on the source.
Thanks,
Ash
http://www.ashleysheridan.co.uk