Hi everyone,
I wanted to apply dynamic watermarks on all my images, so I came up with this simple hack.
enjoy =)
<?
require($GALLERY_BASEDIR . "init.php");
global $gallery;
// A quick little hack to place a watermark on all images
//
// Requirements:
// - Gallery 1.3.3+
// - GD2 module installed with PHP
//
// Installation:
// 1. Save this in the gallery root as watermark.php
//
// 2. Edit classes/Album.php
// - find the function getAlbumDirURL($type)
// - modify the function definition to read:
// function getAlbumDirURL($type, $watermark=0)
// - modify the return of the function:
// if ($watermark == 0) {
// return $gallery->app->albumDirURL . $albumPath;
// }
// else {
// return $gallery->app->photoAlbumURL . "/watermark.php?image_file=" . $albumPath;
// }
//
// 3. Modify calls to getAlbumDirURL()
// - apply watermark by specifying the "1" flag to calls to getAlbumDirURL()
// - for example, edit the return of the function getPhotoTag($index, $full):
// return $photo->getPhotoTag($this->getAlbumDirURL("full", 1), $full);
//
// Further items:
// You will also need to protect the files from direct download from the albums
// directory. This is left as an exercise for the reader =)
// configuration settings
// play with these and see what they do =)
$scale = 25;
$transparency = 50;
$margin_x = 0.1;
$margin_y = 0.1;
$quality = 90;
$watermark_image = "/home/ian/public_html/watermark.jpg";
// get the file we want to watermark
$file = $gallery->app->albumDir . $image_file;
// get the image details and create an image
$image_info = getImageSize($file);
$image_width = $image_info[0];
$image_height = $image_info[1];
$image = ImageCreateFromJPEG($file);
// get the watermark details, and open it
$watermark_info = getImageSize($watermark_image);
$watermark = ImageCreateFromJpeg($watermark_image);
// calculate scale of watermark and create scaled watermark
$watermark_ratio = $watermark_info[0] / $watermark_info[1];
$watermark_height = $image_height / $scale;
$watermark_width = $watermark_height * $watermark_ratio;
$scaled_watermark = imageCreateTrueColor($watermark_width, $watermark_height);
// resize the watermark to the new scale
imageCopyResampled($scaled_watermark, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, $watermark_info[0], $watermark_info[1]);
// set the transparent color (black)
$black = ImageColorAllocate($scaled_watermark, 0, 0, 0);
imagecolortransparent($scaled_watermark, $black);
// add the watermark to the image
ImageCopyMerge($image, $scaled_watermark, $image_width - $watermark_width - ($watermark_width * $margin_x), $image_height - $watermark_height - ($watermark_height * $margin_y), 0, 0, $watermark_info[0], $watermark_info[1], $transparency);
// send out a header
header("content-type:image/jpeg");
// send the image
imagejpeg($image,'',$quality);
// clean up
imagedestroy($image);
?>
Posts: 10
Does this work with all pictures currently uploaded or just ones you upload?
Posts: 8
its dynamic. it works with any images which are loaded.
Posts: 89
Great mod, I especially like the flexibility it has. I'm having some problems though.
All I get is broken images. If I look at the URL for the images, they are all missing the album they are associated with. The path goes straight from the albums directory to the image name, without specifying the unique album the image came from.
Can you think of anything specific I should check?
Posts: 10
Try:
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE><?php</TD></TR></TABLE><!-- BBCode End -->
instead of just
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE> <?</TD></TR></TABLE><!-- BBCode End -->
I'm still getting errors. If you have Red Hat 8.0 you gotta compile GD 2.0 on the server, and upgrade a few things. I'm still working on it.
Posts: 89
Thanks Jmallow. I'll let you know if I get things working.
Posts: 487
Actually, if you want to use GD, then you are better off using php 4.3.x
with the built-in gd library which has additional functionality then the
standard library, such as the ability to read gif files and a few other
things. If you go that path, then you don't need a seperate install of
GD.
Posts: 10
I'm going to upgrade to the newest version of PHP tomorrow. I'll let you all know of my results after testing it.
Posts: 8
what version of gallery are you using?
does the image URL contain the image name at all?
do you have a url demonstrating this?
Posts: 10
http://www.mallowweb.com/gallery/watermark.php?image_file=/aaa.sized.jpg
Posts: 89
I'm running 1.3.3, and the install can be seen <!-- BBCode Start --><A HREF="http://www.malibuboatowners.com/galltest" TARGET="_blank">here</A><!-- BBCode End -->.
The URL does contain the image name, but not it's associated album directory name.
So instead of something like this:
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>http://www.yourdomain.com/gallery/albums/album01/image1.jpg</TD></TR></TABLE><!-- BBCode End -->
you get this:
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>http://www.yourdomain.com/gallery/albums/image1.jpg</TD></TR></TABLE><!-- BBCode End -->
Posts: 10
http://www.mallowweb.com/gallery/watermark.php?image_file=/carshow/aaa.sized.jpg
Your test gallery doesn't work
Posts: 14
The reason it's not working is those warnings being output. You need to disable them.
Posts: 101
Will this mod only work with jpeg images or will it work with all types of inages?
Peter
Posts: 8194
Or you need to install GD 2.0, which is what the warnings are complaining about...
Posts: 101
I have adjusted the script slightly but i can't get it to work.
The script is as follows:-
<?php
require($GALLERY_BASEDIR . "init.php");
global $gallery;
// A quick little hack to place a watermark on all images
//
// Requirements:
// - Gallery 1.3.3+
// - GD2 module installed with PHP
//
// Installation:
// 1. Save this in the gallery root as watermark.php
//
// 2. Edit classes/Album.php
// - find the function getAlbumDirURL($type)
// - modify the function definition to read:
// function getAlbumDirURL($type, $watermark=0)
// - modify the return of the function:
// if ($watermark == 0) {
// return $gallery->app->albumDirURL . $albumPath;
// }
// else {
// return $gallery->app->photoAlbumURL . "/watermark.php?image_file=" . $albumPath;
// }
//
// 3. Modify calls to getAlbumDirURL()
// - apply watermark by specifying the "1" flag to calls to getAlbumDirURL()
// - for example, edit the return of the function getPhotoTag($index, $full):
// return $photo->getPhotoTag($this->getAlbumDirURL("full", 1), $full);
//
// What are the websites (hostnames) that can use this
// image?
// If your site can be accessed with or without the
// "www" prefix, make sure you put both here. Do not put
// any trailing slashes ("/") nor any "http://" prefixes.
// Follow the example below.
$validprefixes = array (
"petergrove.co.uk",
"www.petergrove.co.uk"
) ;
$referrer = getenv( "HTTP_REFERER" ); //Get the refferer URL
function isreferrerokay ( $referrer, $validprefixes )
{
$validreferrer = 0 ;
$authreferrer = current( $validprefixes );
while ($authreferrer) {
if (eregi( "^https?://$authreferrer/", $referrer )) {
$validreferrer = 1 ;
break ;
}
$authreferrer = next( $validprefixes );
}
return $validreferrer ;
}
// What is your email address?
// If you want to be informed when someone tries to use
// this script to access an image illegitimately, you
// must uncomment (remove the "//" prefix) the following
// line and change it to point to your email address.
$email = "peter@petergrove.co.uk" ;
// What is the main page of your website? Visitors will
// be directed here if they type
// "http://www.yourdomain.com/chimage.php"
// in their browser.
$homepage = "http://www.petergove.co.uk/gallery" ;
// configuration settings
// play with these and see what they do =)
$scale = 25;
$transparency = 50;
$margin_x = 0.1;
$margin_y = 0.1;
$quality = 90;
$watermark_image = "/home/public_html/gallery/images/watermark.png";
if (isset($image_file)) {
if (empty($referrer) || isreferrerokay( $referrer, $validprefixes )) {
// get the file we want to watermark
$file = $gallery->app->albumDir . $image_file;
// get the image details and create an image
$image_info = getImageSize($file);
$image_width = $image_info[0];
$image_height = $image_info[1];
//Load in the image correctly
if ($imageinfo[2] == 1) {
$image = ImageCreateFromGIF($file);
$imagetype = "gif" ;
}
elseif ($imageinfo[2] == 2) {
$image = ImageCreateFromJPEG($file);
$imagetype = "jpeg" ;
}
elseif ($imageinfo[2] == 3) {
$image = ImageCreateFromPNG($file);
$imagetype = "png" ;
}
// get the watermark details, and open it
$watermark_info = getImageSize($watermark_image);
//Load in the watermark image correctly
if ($watermark_info[2] == 1) {
$watermark = ImageCreateFromGIF($watermark);
}
elseif ($watermark_info[2] == 2) {
$watermark = ImageCreateFromJPEG($watermark);
}
elseif ($watermark_info[2] == 3) {
$watermark = ImageCreateFromPNG($watermark);
}
// calculate scale of watermark and create scaled watermark
$watermark_ratio = $watermark_info[0] / $watermark_info[1];
$watermark_height = $image_height / $scale;
$watermark_width = $watermark_height * $watermark_ratio;
$scaled_watermark = imageCreateTrueColor($watermark_width, $watermark_height);
// resize the watermark to the new scale
imageCopyResampled($scaled_watermark, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, $watermark_info[0], $watermark_info[1]);
// set the transparent color (black) is this needed?
$black = ImageColorAllocate($scaled_watermark, 0, 0, 0);
imagecolortransparent($scaled_watermark, $black);
// add the watermark to the image
ImageCopyMerge($image, $scaled_watermark, $image_width - $watermark_width - ($watermark_width * $margin_x), $image_height - $watermark_height - ($watermark_height * $margin_y), 0, 0, $watermark_info[0], $watermark_info[1], $transparency);
// send out a header and anticache headers
header("Content-type: image/jpeg");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-control: no-cache,must-revalidate,no-store");
// send the image
imagejpeg($image,'',$quality);
// clean up
imagedestroy($image);
}
else {
//The refferrer is not an allowed one to use your images, so lets email you there details
if (isset($email)) {
mail( $email, "Bandwidth Theft Alert", "WARNING:nn$referrerntried to accessn$imagen", "From: CHImageGuard <$email>" );
}
header( "HTTP/1.0 404 Not Found" );
}
}
else {
//The file requested wasn't a image so send them back to the home page
mail( $email, "File is not a image", "WARNING:nn$referrerntried to accessn$image via the web pagen", "From: CHImageGuard <$email>" );
header( "Location: $homepage" );
}
?>
Any ideas?
Peter
Posts: 3473
phgrove
Have you actually read the error messages?
So, does the file /home/public_html/gallery/images/watermark.png exist? If not, then fix that. Then see what fixing that does.
Posts: 101
Sorry i changed the script before posting it into the forum, so that all my paths weren't given away, and uploaded it which i shouldn't have done! I have fixed it now so you will see the proper messages. I have tried it, if it doesn't work the first time hit reload and you will get the messages.
Thanks
Peter
Posts: 3473
http://www.petergrove.co.uk/gallery/watermark.php?image_file=/album71/07_10.jpg???
There are no error messages there now. However, it's a html file, not a jpeg.
This is where I drop out, as I haven't installed this script. But if that's supposed to be a jpg, there's something really wrong as the contents are
<html><body></body></html>
Posts: 101
I get these warning int he browser:-
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/www/petergro/gallery/watermark.php on line 130
Warning: imagecopymerge(): supplied argument is not a valid Image resource in /home/www/petergro/gallery/watermark.php on line 137
Warning: Cannot modify header information - headers already sent by (output started at /home/www/petergro/gallery/watermark.php:130) in /home/www/petergro/gallery/watermark.php on line 140
Warning: Cannot modify header information - headers already sent by (output started at /home/www/petergro/gallery/watermark.php:130) in /home/www/petergro/gallery/watermark.php on line 141
Warning: Cannot modify header information - headers already sent by (output started at /home/www/petergro/gallery/watermark.php:130) in /home/www/petergro/gallery/watermark.php on line 142
Warning: Cannot modify header information - headers already sent by (output started at /home/www/petergro/gallery/watermark.php:130) in /home/www/petergro/gallery/watermark.php on line 143
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/www/petergro/gallery/watermark.php on line 146
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/www/petergro/gallery/watermark.php on line 149
Any further ideas
Peter
Posts: 3473
Time to learn how to debug. Generally the best approach is to only look at the first error: it's possible all the rest are side effects of that. For instance:
is caused by printing the first error, before sending the html header information. It will disappear when you get rid of the errors.
so this
is your first error, presumably generated by this
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>
// resize the watermark to the new scale
imageCopyResampled($scaled_watermark, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, $watermark_info[0], $watermark_info[1]);
</TD></TR></TABLE><!-- BBCode End -->
You can read the manual page about this function at <!-- BBCode Start --><A HREF="http://www.php.net/manual/en/function.imagecopyresampled.php" TARGET="_blank">http://www.php.net/manual/en/function.imagecopyresampled.php</A><!-- BBCode End -->
but the important thing to note is that the first two arguments should be resources, but at least on of them isn't. But which one?
You need to add a diagnostic. Try adding this, right above line 130:
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>
print "<p>scaled_watermark: <br>"
var_dump($scaled_watermark);
print "<p> watermark<br>";
var_dump($watermark);
</TD></TR></TABLE><!-- BBCode End -->
to find out where the trouble is.
Posts: 101
thanks that will help me
Peter
Posts: 3473
No, they look fine.
I'm out - you'll have to wait until someone who has this script running comes online.
Posts: 101
I got it all sorted, it was what i thourgh wa the problem inthe first place. The wrong varaible name used at the wrong place, too many $image_ arounbd. Thanks for the debug code to dump the varaible out, without that i would have struggled.
Peter :smile:
Posts: 34
Today, i was wondering why the watermarking was still buggy...
so i finally recode a part of it ...
INCLUDING SUPPORT FOR TEXT WATERMARK'S :smile:
Zip, screenshots and tarball availables at :<!-- BBCode Start --><A HREF="http://www.votremedia.com/gallery/watermark_mod" TARGET="_blank">http://www.votremedia.com/gallery/watermark_mod</A><!-- BBCode End -->
Package include : watermark.php and modified classes/Album.php
P.S Please ignore my poor english, je suis Quebecois stie !
comments are welcome
Posts: 101
I have been trying to fix a problem with my watermarking script. Currently it works, but what i would like to do is check that the referrer is from my domain only. For some reason when i get the script to print out what the referrer's doamin is it is empty, as though i couldn't get the enviroment variable HTTP_REFERRER.
This is the function that check the referrer's domain name.
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE> $validprefixes = array (
"petergrove.co.uk",
"www.petergrove.co.uk"
) ;
function isreferrerokay ( $referrer, $validprefixes )
{
$validreferrer = 0 ;
$authreferrer = current( $validprefixes );
while ($authreferrer) {
if (eregi( "^https?://$authreferrer/", $referrer )) {
$validreferrer = 1 ;
break ;
}
$authreferrer = next( $validprefixes );
}
return $validreferrer ;
}
</TD></TR></TABLE><!-- BBCode End -->
This is the beginning of my watermarking script.
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE> if (isset($image_file)) {
$referrer = getenv( "HTTP_REFERER" ); //Get the refferer URL
if (empty($referrer) || isreferrerokay( $referrer, $validprefixes )) {
</TD></TR></TABLE><!-- BBCode End -->
If i remove empty($referrer) from the if statement the script fails as the $referrer variable is empty. The way i have added this script in is via the classesalbums.php file like such
// function getAlbumDirURL($type)
change
// return $gallery->app->albumDirURL . $albumPath;
to.
// return $gallery->app->photoAlbumURL . "/watermark.php?image_file=" . $albumPath;
Any idea's on whay the variable would be empty?
Peter
Posts: 89
Thanks for contributing your script jflaflamme! Just be aware, it is very resource intensive. I guess it's related to the fact that it builds the watermarked image for each view, but I had to take it down because of the additional bandwidth used.
Posts: 1
Posts: 34
Hi, thanks for the information about the missing file, ZIP file has been fixed now...
Well, this is possible to modify the images right after the upload process, is it true that it will help processing time, but for me, i prefer to keep the images intact for now, but maybe in the future i will change it to write them after the upload. This is not complicated.
I don't want to modify to much this script, because un Gallery2, that's gonna be different so ...
About your error :
Did you configure the watermark.php correctly ? line 104, this is where the variable about your image is, this isn't supposed to append
Feel free to email me at
or ICQ 7561558
When i'll have some free time, i will modify the watermark.php to look for the pixel colors where the watermak goes, to have nearly the right background color, this will prevent text/image to appear chunky on the corners/borders, but this isn't 100% perfect, except if all the images have the same background color, which is not common.
Jean-Francois Laflamme
Posts: 8
Well, its been a few weeks since I posted my initial hack, and its suprising to see how many peopl have been discussing it! =)
I have since worked on making the images cacheable, and also fixing a bug that prevented IE5.5 or IE6 from correctly caching the image.
I have now added the following code:
1. Generate unique filename for the image (for those how want "save images as")
// file name (fix it up a little)
$filename = substr($image_file, 1);
$filename = str_replace("/", "-", $filename); // replace /
$filename = str_replace(".JPG", "DOTjpg", $filename);
$filename = str_replace(".jpg", "DOTjpg", $filename);
$filename = str_replace(".", "_", $filename); // replace .
$filename = str_replace("DOTjpg", ".jpg", $filename); // put on extension
header("Content-Disposition: inline; filename="$filename"");
2. Cache images for 3 days
// allow caching
$offset = 60 * 60 * 24 * 3;
$expires = gmdate("D, d M Y H:i:s", time() + $offset);
header("Cache-control: private, max-age=$offset, pre-check=$offset"); // HTTP 1.1
3. Fix ie5.5, ie6 caching bug
I removed the first line which required galleries init.php. Instead of referencing the $gallery global when getting the image name, i just hardcoded it.
// get the file we want to watermark
$file = "/var/www/html/photos/albums" . $image_file;
By not using galleries init.php we save a bit of preocessing, but also we don't send out a whole bunch of cookies that seem to throw ie5.5+ and prevent them from caching the image.
Anyway, I hope people find these modifications useful.
Also, for those of you with access to mod_rewrite (I dont'), you could scrab modifing Albums.php and just use a rewrite in the albums dir to pass the request through the watermark. Thats another challenge I leve up to the reader =)
Posts: 63
The script works great for images, but when I try and open a flash (.swf) file, I'm getting some error messages.
Warning: imagecreatefromjpeg: 'c:/phpdev/imagestore/gallery/albums/dina_meyer/card2_1.swf' is not a valid JPEG file in c:phpdevwwwpersonalnukehtmlmodulesgallerywatermark.php on line 55
Warning: imagecopymerge(): supplied argument is not a valid Image resource in c:phpdevwwwpersonalnukehtmlmodulesgallerywatermark.php on line 75
Warning: Cannot add header information - headers already sent by (output started at c:phpdevwwwpersonalnukehtmlmodulesgallerywatermark.php:55) in c:phpdevwwwpersonalnukehtmlmodulesgallerywatermark.php on line 78
Warning: imagejpeg(): supplied argument is not a valid Image resource in c:phpdevwwwpersonalnukehtmlmodulesgallerywatermark.php on line 81
Warning: imagedestroy(): supplied argument is not a valid Image resource in c:phpdevwwwpersonalnukehtmlmodulesgallerywatermark.php on line 84
What can I do to eliminate the errors?
Thanks
Posts: 8
you'll need to descriminate against jpeg and non-jpeg content. I only use jpeg images, so I can assume that I want to watermark everything.
In the watermark.php code, you should check the extension of the file, and if its .swf (or something else), you should just read the file, send an appropriate Content-Type header and then send the file data.
Alternatively, you could check for this in the modified code in Albums.php, and return the non-watermarked AlbumURL if the file type is not jpeg.
Posts: 8
For those of you who want to see my watermarking script in action, have a look here:
http://www.freshdisko.com/photos/
I'm only using GD 1.0, so no fancy blending which would be possible with GD 2.0.
Please note - DO NOT email the site maintainers of freshdisko.com asking for help with these scripts. If you do, you will be ignored (sorry, no time to reply!) I have posted this link purely as an example.
cheers.
Posts: 6
Perhaps I'm missing something, but does this modify the images on the server or add the watermark dynamically when the image is sent to the browser?
Posts: 8
it does it dynamically. I can change the watermark whenever I want.
Posts: 90
This wouldn't work if images are getting served from a different server though; would it? (I'm using a secondary server hosted with an ISP to serve images on http://mvgals.net ; with the primary server being served from my dsl connection...)
Has anyone came with a simple hack to watermark the pictures as they're being uploaded?
-mark
Posts: 2
Does anyone was able to get the watermark mod to work?
The images are not shown ... but no error message is visible.
Posts: 63
I'm trying to check the extension of images/files in Album.php to decide whether to Watermark or not but I can't figure out how to do the check.
Any Ideas?
Thanks
Posts: 3473
in the AlbumItem class, the image extension is called type, and there's a function called "isImage"
Depends on how you are writing the code, but I expect something like this is what is required:
if ($photo->isImage() {
// watermark
}
Posts: 63
Thanks!
I want to go one step further to check whether it is a .gif vs .jpg type.
Jeff...
Posts: 63
Joan, any suggestions how to check what the file type is in Album.php. Everything I try gives me a non-object error.
Thanks
Posts: 3473
It only makes sense to do this to an image. What image do you want to watermark from Albums.php?
I can only imagine watermarking in view_photo.php
Posts: 63
Joan, well what I've got is an album that has jpeg, gif, mpeg and mov files. The watermarking as defined only works for jpeg files so I want to do a file type check to determine whether to pass to the watermak.php program.
Hope that makes sense.
Jeff...
Posts: 101
This is code you probably are wanting. Hope it helps
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE> <?php
require($GALLERY_BASEDIR . "init.php");
global $gallery;
// A quick little hack to place a watermark on all images
//
// Requirements:
// - Gallery 1.3.3+
// - GD2 module installed with PHP
//
// Installation:
// 1. Save this in the gallery root as watermark.php
//
// 2. Edit classes/Album.php
// - modify the return of the function:
// return $gallery->app->albumDirURL . $albumPath;
// to
// return $gallery->app->photoAlbumURL . "/watermark.php?image_file=" . $albumPath;
/
//
// What are the websites (hostnames) that can use this
// image?
// If your site can be accessed with or without the
// "www" prefix, make sure you put both here. Do not put
// any trailing slashes ("/") nor any "http://" prefixes.
// Follow the example below.
$validprefixes = array (
"petergrove.co.uk",
"www.petergrove.co.uk"
) ;
function isreferrerokay ( $referrer, $validprefixes )
{
$validreferrer = 0 ;
$authreferrer = current( $validprefixes );
while ($authreferrer) {
if (eregi( "^https?://$authreferrer/", $referrer )) {
$validreferrer = 1 ;
break ;
}
$authreferrer = next( $validprefixes );
}
return $validreferrer ;
}
// What is your email address?
// If you want to be informed when someone tries to use
// this script to access an image illegitimately, you
// must uncomment (remove the "//" prefix) the following
// line and change it to point to your email address.
$email = "name@domainname.com" ;
// What is the main page of your website? Visitors will
// be directed here if they type
// "http://www.yourdomain.com/chimage.php"
// in their browser.
$homepage = "http://www.petergrove.co.uk/gallery" ;
// configuration settings
// play with these and see what they do =)
$scale = 4;
$transparency = 40;
$quality = 90;
$watermark_image = "/path_to_image/watermark.png";
if (isset($image_file)) {
$referrer = getenv( "HTTP_REFERER" ); //Get the refferer URL
//empty($referrer) is needed as they might have typed the URL directly!
if (empty($referrer) || isreferrerokay( $referrer, $validprefixes )) {
//Get extension and check that it is of a type image.
$ext=strtolower(substr($image_file,strrpos($image_file,".")+1,strlen($image_file)-strrpos($image_file,".")));
if ((($ext=="jpg")||($ext=="gif")|($ext=="jpeg")|($ext=="png"))) {
// get the file we want to watermark
$file = $gallery->app->albumDir . $image_file;
// get the image details and create an image
$image_info = getImageSize($file);
$image_width = $image_info[0];
$image_height = $image_info[1];
//Load in the image correctly
if ($image_info[2] == 1) {
$image = ImageCreateFromGIF($file);
$imagetype = "gif" ;
}
elseif ($image_info[2] == 2) {
$image = ImageCreateFromJPEG($file);
$imagetype = "jpeg" ;
}
elseif ($image_info[2] == 3) {
$image = ImageCreateFromPNG($file);
$imagetype = "png" ;
}
ImageAlphaBlending($image, true);
// get the watermark details, and open it
$watermark_info = getImageSize($watermark_image);
//Load in the watermark image correctly
if ($watermark_info[2] == 1) {
$watermark = ImageCreateFromGIF($watermark_image);
}
elseif ($watermark_info[2] == 2) {
$watermark = ImageCreateFromJPEG($watermark_image);
}
elseif ($watermark_info[2] == 3) {
$watermark = ImageCreateFromPNG($watermark_image);
}
// calculate scale of watermark and create scaled watermark
$watermark_ratio = $watermark_info[0] / $watermark_info[1];
$watermark_height = $image_height / $scale;
$watermark_width = $watermark_height * $watermark_ratio;
$scaled_watermark = imageCreateTrueColor($watermark_width, $watermark_height);
// resize the watermark to the new scale
imageCopyResampled($scaled_watermark, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, $watermark_info[0], $watermark_info[1]);
// set the transparent color (black) is this needed?
$black = ImageColorAllocate($scaled_watermark, 0, 0, 0);
imagecolortransparent($scaled_watermark, $black);
// add the watermark to the image
ImageCopyMerge($image, $scaled_watermark, (($image_width - $watermark_width)/2), (($image_height - $watermark_height)/2), 0, 0, $watermark_width, $watermark_height, $transparency);
// send out a header and anticache headers
header("Content-type: image/jpeg");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-control: no-cache,must-revalidate,no-store");
// send the image
imagejpeg($image,'',$quality);
// clean up
imagedestroy($image);
//mail( $email, "IMAGE", "image:
$referrer
tried to access
$image_file
", "From: ImageGuard <$email>" );
}
else {
//File is not an image file
header( "HTTP/1.0 404 Not Found" );
}
}
else {
//The refferrer is not an allowed one to use your images, so lets email you there details
if (isset($email)) {
mail( $email, "Bandwidth Theft Alert", "WARNING:
$referrer
tried to access
$image_file
", "From: ImageGuard <$email>" );
}
header( "Location: $referrer" );
}
}
else {
//No image_file was specified so return them to the homepage
header( "Location: $referrer" );
}
?> </TD></TR></TABLE><!-- BBCode End -->
Posts: 63
I replaced my existing watermark.php with the code (Posted: Apr 01, 2003 - 07:14 PM), however when I run gallery all my images show as a red x.
Is there something else I need to do.
BTW, the watermark code I was using before worked fine as long as the image file was jpeg.
Thanks
Jeff...
Posts: 101
The code above works fine for me. Have you got the path to your watermark image right, if you haven't that that could cause it. Basically for the images to have a red X means either it couldn't find the image to have the watermark added or it couldn't find the watermark image file. I have only tried this code with a PNG water image and nothing else. If you want the watermark image get it from http://www.petergrove.co.uk/gallery/images/watermark.png
Peter
Posts: 63
Thanks for the followup!
I checked the path to my jpeg watermark image and it was fine so I created the image as a png file but it still displays the red x on the images.
Unfortunately I'm running this on a localhost so I can't provide access to my development site.
Jeff...
Posts: 44
Hi People,
I download the jflaflamme's script (<!-- BBCode Start --><A HREF="http://www.votremedia.com/gallery/watermark_mod" TARGET="_blank">http://www.votremedia.com/gallery/watermark_mod</A><!-- BBCode End -->) and install it.
But, now, the gallery dont show the pictures and in it show a red "X" !!!!
What is the problem???
My OS is win with php4.3.0 with GD2.
Thanks
Marcelo
Posts: 2
I have exactly the same problem .. not images files are shown :sad:
with the watermark file enabled, then all the images are called like
http://myserver/watermark.php?image_file=/test/myimage.jpg
Is the path correctly directed where the image files are stored? I tried with gallery installed on a linux & windows machine with GD 2.0 installed
Posts: 44
Hi people,
I executed the jflaflamme's script <!-- BBCode Start --><A HREF="http://my.domain.com/gallery/watermark.php?image_file=watermark.jpg" TARGET="_blank">http://my.domain.com/gallery/watermark.php?image_file=watermark.jpg</A><!-- BBCode End --> and I noted a few goods: errors.
My watermark is in the f:\webdoc\gallery\images\watermark.jpg
Thanks
Marcelo :cry:
Posts: 44
HI everyone
How do I do for download/install the IsoLatin1.enc and helvetica.pfb?
I use Debian.
I am sorry for this OFF TOPIC in this forum.
Thanks
Marcelo