Transparent PNGs: Merging

Transparent PNGs: Merging

am 12.12.2009 06:02:30 von Alex Davies

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:


//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);


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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Transparent PNGs: Merging

am 12.12.2009 12:20:21 von Ashley Sheridan

--=-BtM7qijR0/JXHF22mZQm
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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:
>
>
> //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);
>

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



--=-BtM7qijR0/JXHF22mZQm--

Re: Transparent PNGs: Merging

am 12.12.2009 15:03:19 von Alex Davies

--000e0cd1b12edb57a6047a888143
Content-Type: text/plain; charset=ISO-8859-1

Hi Ash,

Thanks for your suggestion. Unfortunately,that did not help but i've found a
post via Google which gave a simple (and slightly counter-intuitive)
solution - replace

imagecopymerge($img2, $img, $yoffset, 5, 0, 0, imagesx($img), imagesy($img),
100);
with
imagecopy($img2, $img, $yoffset, 5, 0, 0, imagesx($img), imagesy($img));

And it works :)

It seems imagecopymerge does not work well with PNGs and Alpha Channels.

Many thanks for your help,

Alex

On Sat, Dec 12, 2009 at 11:20 AM, Ashley Sheridan
wrote:

> 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:
>
>
> //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);
>

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


--
Alex Davies

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the sender immediately by e-mail and delete this e-mail permanently.

--000e0cd1b12edb57a6047a888143--

Re: Transparent PNGs: Merging

am 12.12.2009 16:10:27 von TedD

At 5:02 AM +0000 12/12/09, Alex Davies wrote:
>-snip-
>
>Suggestions appreciated, i'm lost!
>
>Many thanks,
>
>Alex

Watermark?

Try this:

http://webbytedd.com/b/watermark/

The code is there and the dog is actual size.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php