Bluehost.com Web Hosting $6.95

png transparency operations

This is a discussion on png transparency operations within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I found the following code to add a watermark to an image. It takes a png source as a watermark. ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-09-2005
Sir Loin of Beef
 
Posts: n/a
Default png transparency operations

I found the following code to add a watermark to an image. It takes a
png source as a watermark.
Problem is, it doesn't appear to handle transparancy. My png has a
single color background that I want to set as transparent, which I've
done in paintshop, but when I run it through the code below, the
output image displays the entire png as a rectangular block with no
transparency.

Are there any transparency operations for png possible with php?


<?php

header('content-type: image/jpeg');

$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['src']);
$size = getimagesize($_GET['src']);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0,
$watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

?>
Reply With Quote
  #2 (permalink)  
Old 02-09-2005
Dave Patton
 
Posts: n/a
Default Re: png transparency operations

test@gesg.com (Sir Loin of Beef) wrote in
news:4209613d.4694221@news.starhub.net.sg:

> I found the following code to add a watermark to an image. It takes a
> png source as a watermark.


And you are using imagejpeg(), so it doesn't output a PNG image.

> Are there any transparency operations for png possible with php?


http://www.php.net/manual/en/functio...hablending.php

--
Dave Patton
Canadian Coordinator, Degree Confluence Project
http://www.confluence.org/
My website: http://members.shaw.ca/davepatton/
Reply With Quote
  #3 (permalink)  
Old 02-09-2005
Sir Loin of Beef
 
Posts: n/a
Default Re: png transparency operations

Yes, that's right.
I have 2 source files - a picture jpg, and a watermark png.
The output is a jpg.

Can I still maintain the transparency of the png watermark in the
output jpg?

Dave Patton <spam@trap.invalid> wrote:

>test@gesg.com (Sir Loin of Beef) wrote in
>news:4209613d.4694221@news.starhub.net.sg:
>
>> I found the following code to add a watermark to an image. It takes a
>> png source as a watermark.

>
>And you are using imagejpeg(), so it doesn't output a PNG image.
>
>> Are there any transparency operations for png possible with php?

>
>http://www.php.net/manual/en/functio...hablending.php
>
>--
>Dave Patton
>Canadian Coordinator, Degree Confluence Project
>http://www.confluence.org/
>My website: http://members.shaw.ca/davepatton/


Reply With Quote
  #4 (permalink)  
Old 02-09-2005
Martin
 
Posts: n/a
Default Re: png transparency operations

On Wed, 09 Feb 2005 01:04:29 GMT, test@gesg.com (Sir Loin of Beef)
wrote:

>I found the following code to add a watermark to an image. It takes a
>png source as a watermark.
>Problem is, it doesn't appear to handle transparancy. My png has a
>single color background that I want to set as transparent, which I've
>done in paintshop, but when I run it through the code below, the
>output image displays the entire png as a rectangular block with no
>transparency.


As viewed in what browser?

Internet Explorer has difficulty displaying a transparent .PNG image
correctly.

>
>Are there any transparency operations for png possible with php?
>
>
><?php
>
>header('content-type: image/jpeg');
>
>$watermark = imagecreatefrompng('watermark.png');
>$watermark_width = imagesx($watermark);
>$watermark_height = imagesy($watermark);
>$image = imagecreatetruecolor($watermark_width, $watermark_height);
>$image = imagecreatefromjpeg($_GET['src']);
>$size = getimagesize($_GET['src']);
>$dest_x = $size[0] - $watermark_width - 5;
>$dest_y = $size[1] - $watermark_height - 5;
>
>imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0,
>$watermark_width, $watermark_height, 100);
>imagejpeg($image);
>imagedestroy($image);
>imagedestroy($watermark);
>
>?>


Reply With Quote
  #5 (permalink)  
Old 02-09-2005
Andy Hassall
 
Posts: n/a
Default Re: png transparency operations

On Wed, 09 Feb 2005 01:04:29 GMT, test@gesg.com (Sir Loin of Beef) wrote:

>I found the following code to add a watermark to an image. It takes a
>png source as a watermark.
>Problem is, it doesn't appear to handle transparancy. My png has a
>single color background that I want to set as transparent, which I've
>done in paintshop, but when I run it through the code below, the
>output image displays the entire png as a rectangular block with no
>transparency.
>
>Are there any transparency operations for png possible with php?
>
>
><?php
>
>header('content-type: image/jpeg');
>
>$watermark = imagecreatefrompng('watermark.png');
>$watermark_width = imagesx($watermark);
>$watermark_height = imagesy($watermark);
>$image = imagecreatetruecolor($watermark_width, $watermark_height);
>$image = imagecreatefromjpeg($_GET['src']);
>$size = getimagesize($_GET['src']);
>$dest_x = $size[0] - $watermark_width - 5;
>$dest_y = $size[1] - $watermark_height - 5;
>
>imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0,
>$watermark_width, $watermark_height, 100);
>imagejpeg($image);
>imagedestroy($image);
>imagedestroy($watermark);
>
>?>


I've tried that exact code here, with a 48x48 256-colour palettised PNG of a
star shape with the background set transparent using Paint Shop Pro 8, and it
works exactly as expected - watermark appears in bottom right of JPEG selected,
with the JPEG image showing through the transparent parts of the watermark.

What version of PHP are you on? This was on PHP 5.0.3 which includes a fairly
recent version of GD, the library that does image handling in PHP. (I think
4.3.10 has about the same version).

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:24 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0