Re: Transparent PNGs: Merging

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2009-12-12 at 05:02 +0000, Alex Davies wrote:

> Hi All,
> 
> I apologise if this is a newbie post; i'm new to transparent graphics
> (PNGs in my case).
> 
> I have a transparent PNG on disk, which I want PHP to load into memory
> and add some text to ("watermarking", I guess). This seems to be what
> is achieved @ http://php.ca/manual/en/image.examples.merged-watermark.php
> 
> I have taken the following code almost entirely from the manual, but
> can not understand what I have done wrong:
> 
> <snip>
> //Create the image
> $img = @imagecreatetruecolor($width, $height)
>       or die("Cannot Initialize new GD image stream");
> 
> //Make it transparent
> imagesavealpha($img, true);
> $trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127);
> imagefill($img, 0, 0, $trans_colour);
> 
> //Get the text color
> $text_color = imagecolorallocate($img, 255, 0, 0);
> 
> //Draw the string
> imagestring($img, $font_size, 0, 0,  $string, $text_color);
> 
> // Load background image, find position, print
> $img2 = imagecreatefrompng('xxx.png');
> $yoffset = (imagesx($img2)/2) - (imagesx($img)/2);
> imagecopymerge($img2, $img, $yoffset, 5, 0, 0, imagesx($img),
> imagesy($img), 100);
> 
> // Output the merged images
> header ("Content-type: image/png");
> imagepng($img2);
> </snip>
> 
> This code prints the background image with transparancy removed with a
> light grey, and the text image in red on a black background in the
> correct position.
> If I change it to imagepng($img), the text image is printed correctly
> (transparency and all)
> 
> Suggestions appreciated, i'm lost!
> 
> Many thanks,
> 
> Alex
> 


The only thing I can obviously see is that you've told it to ignore
transparency on $img2 by not asking it to preserve it (the default is to
ignore). PNG doesn't handle transparency in the same way a GIF does,
each pixel can have a varying degree of opacity, which allows whatever
is beneath it to show through. Add the imagesavealpha($img, true); line
for $img2 and see what that does for you.

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux