watermarking and headers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

watermarking and headers

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't call it with include.. you call it like it's an image.. with <img> tags...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

:?:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a script can be used anywhere a URL request get's generated. :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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"');
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I don't see how it is getting the original filename of watermark.jpg
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply