Page 1 of 2

watermarking and headers

Posted: Tue Aug 09, 2005 10:03 am
by s.dot
Watermark code:

Code: Select all

<?
header('content-type: image/jpeg');  
$watermark = imagecreatefrompng('images/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);
?>
Problems:
#1. I need the watermarked image to be created dynamically on a text/html page. Can you specify two header content-types? Such as image/jpeg and text/html.

#2. When trying to save the image, the image name is 'watermark.jpg'.. how would I preserve the original filename?

Posted: Tue Aug 09, 2005 10:07 am
by Grim...
1) Don't know. Try it.
2) Can you not store the name of the file in a variable before you add the watermark?

Posted: Tue Aug 09, 2005 10:09 am
by feyd
1: not possible. HTML and images are passed in seperate streams from the server to client.
2: You need to write a script that just outputs the image to the client. Call this script with enough information to watermark the correct image (I'd suggest encrypted a bit) from the "html" that another script generates.

Posted: Tue Aug 09, 2005 10:20 am
by s.dot
So let me try to get this correct.

The filename part is unimportant, just a preference, so I won't concentrate on that for now.

Right now, I have the watermarking script on it's own page, so the header content type is not a problem.

Now if I call this script from the text/html page... using include() ? won't it tell me that the header information cannot be modified?

Posted: Tue Aug 09, 2005 10:45 am
by feyd
you don't call it with include.. you call it like it's an image.. with <img> tags...

Posted: Tue Aug 09, 2005 11:01 am
by josh
You can use a rewrite rule (in .htaccess) to preserve the filename.

Code: Select all

RewriteRule ^images/watermark.jpg images/watermark.php
Optionally you could tell php to parse jpg files and rename your script, however I'd advise against this as you don't want php parsing every image file on your host, could:
1) slow down / hinder performance
2) cause problems if php tries to parse something that isn't actually php (unlikely but probably possible)

Posted: Tue Aug 09, 2005 11:10 am
by s.dot
feyd wrote:you don't call it with include.. you call it like it's an image.. with <img> tags...
<img src="path/to/script.php" alt="pic">

:?:

Posted: Tue Aug 09, 2005 11:19 am
by josh
yes, unless youre using rewrite then it would be path/to/script.jpg

remember the path/to/script is a URL not a path on the filesystem, as the clients browser will request the script.

Posted: Tue Aug 09, 2005 11:25 am
by s.dot
Nice, I shall give this a try in a bit and see how it turns out. I never knew scripts could be executed inside images tags. Learning rox my sox.

Posted: Tue Aug 09, 2005 11:39 am
by feyd
a script can be used anywhere a URL request get's generated. :)

Posted: Tue Aug 09, 2005 4:59 pm
by s.dot
OK, watermarking the pictures works great.

Now, onto the file name. I guess in order to preserve the filename, I'd have to understand what part of the script (in the very first post) makes the picture named 'watermark.jpg' ?

Right now the URL I'm using to generate the watermark is http://www.domain.com/watermark.php?src ... icture.jpg

Posted: Tue Aug 09, 2005 5:27 pm
by feyd

Code: Select all

header('Content-Disposition: attachment; filename="downloaded.pdf"');
added note: remember to send a content-length header. You'll need to know this for IE to store the images correctly, otherwise it'll get confused and save them as BMP.

Posted: Tue Aug 09, 2005 5:40 pm
by s.dot
So this should change the filename to whatever the filename is set to in the header?

I tried it, but it still saves as watermark.jpg

Note: I didn't add the content length header, and I placed that header information on watermark.php like this:

Code: Select all

header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="image.jpg"');

Posted: Tue Aug 09, 2005 5:47 pm
by feyd
may potentially get it to trick with adding "image.jpg" in a second (ignored) url parameter.. although something tells me that won't work...

Posted: Tue Aug 09, 2005 5:52 pm
by s.dot
I don't see how it is getting the original filename of watermark.jpg