watermarking

semisphere

Joined: 2003-02-19
Posts: 8
Posted: Wed, 2003-02-19 15:33

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

?>

 
Jmallow
Jmallow's picture

Joined: 2003-02-19
Posts: 10
Posted: Thu, 2003-02-20 12:32

Does this work with all pictures currently uploaded or just ones you upload?

 
semisphere

Joined: 2003-02-19
Posts: 8
Posted: Thu, 2003-02-20 15:42
Quote:
Does this work with all pictures currently uploaded or just ones you upload?

its dynamic. it works with any images which are loaded.

 
skicrave

Joined: 2002-10-25
Posts: 89
Posted: Thu, 2003-02-20 21:04

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?

 
Jmallow
Jmallow's picture

Joined: 2003-02-19
Posts: 10
Posted: Thu, 2003-02-20 23:35

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.

 
skicrave

Joined: 2002-10-25
Posts: 89
Posted: Fri, 2003-02-21 01:51

Thanks Jmallow. I'll let you know if I get things working.

 
vallimar

Joined: 2002-08-15
Posts: 487
Posted: Fri, 2003-02-21 03:16

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.

 
Jmallow
Jmallow's picture

Joined: 2003-02-19
Posts: 10
Posted: Fri, 2003-02-21 05:04

I'm going to upgrade to the newest version of PHP tomorrow. I'll let you all know of my results after testing it.

 
semisphere

Joined: 2003-02-19
Posts: 8
Posted: Fri, 2003-02-21 06:45
Quote:
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?

what version of gallery are you using?
does the image URL contain the image name at all?
do you have a url demonstrating this?

 
Jmallow
Jmallow's picture

Joined: 2003-02-19
Posts: 10
Posted: Fri, 2003-02-21 12:15
 
skicrave

Joined: 2002-10-25
Posts: 89
Posted: Fri, 2003-02-21 18:50

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

 
Jmallow
Jmallow's picture

Joined: 2003-02-19
Posts: 10
Posted: Fri, 2003-02-21 23:26
 
Otto

Joined: 2003-02-16
Posts: 14
Posted: Fri, 2003-02-21 23:53

The reason it's not working is those warnings being output. You need to disable them.

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Sat, 2003-02-22 12:27

Will this mod only work with jpeg images or will it work with all types of inages?

Peter

 
alindeman
alindeman's picture

Joined: 2002-10-06
Posts: 8194
Posted: Sat, 2003-02-22 16:45

Or you need to install GD 2.0, which is what the warnings are complaining about...

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Wed, 2003-02-26 08:37

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

 
joan
joan's picture

Joined: 2002-10-21
Posts: 3473
Posted: Wed, 2003-02-26 10:24

phgrove

Have you actually read the error messages?

Quote:
Warning: getimagesize(/home/public_html/gallery/images/watermark.png) [function.getimagesize]: failed to create stream: No such file or directory in /home/www/petergro/gallery/watermark.php on line 111

So, does the file /home/public_html/gallery/images/watermark.png exist? If not, then fix that. Then see what fixing that does.

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Wed, 2003-02-26 10:45

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

 
joan
joan's picture

Joined: 2002-10-21
Posts: 3473
Posted: Wed, 2003-02-26 10:51

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>

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Wed, 2003-02-26 11:47

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

 
joan
joan's picture

Joined: 2002-10-21
Posts: 3473
Posted: Wed, 2003-02-26 12:38

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:

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

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

Quote:
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/www/petergro/gallery/watermark.php on line 130

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.

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Wed, 2003-02-26 13:07

thanks that will help me

Peter

 
joan
joan's picture

Joined: 2002-10-21
Posts: 3473
Posted: Wed, 2003-02-26 13:21
Quote:
+ all the other messages. Is it a case that the 2 images must be of the same type, eg both jpegs?

No, they look fine.

I'm out - you'll have to wait until someone who has this script running comes online.

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Wed, 2003-02-26 19:19

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:

 
jflaflamme

Joined: 2003-02-26
Posts: 34
Posted: Thu, 2003-02-27 01:11

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

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Sun, 2003-03-02 11:03

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

 
skicrave

Joined: 2002-10-25
Posts: 89
Posted: Tue, 2003-03-04 01:23

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.

 
MaxHeadroom

Joined: 2003-03-05
Posts: 1
Posted: Wed, 2003-03-05 14:13
Quote:
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

</BLOCKQUOTE></FONT></TD></TR><TR><TD><HR></TD></TR></TABLE><!-- BBCode Quote End -->

The zip doesnt include albumb.php in the /classes directory, just the directory...

However, i downloaded the tarball instead, and... well, i get this error when entering a direct image (in the album i get the not found/not recognized-icon):

Fatal error: Call to undefined function: imagecreatefromjpeg() in /home/vhosts/battlefield1942.nu/bfds/gallery/watermark.php on line 104

how can that be fixed? to get it on working?

Btw, an idea is to watermark images on upload, and have them watermarked from begginning. Would take less cpu on bigger sites. Possible?

 
jflaflamme

Joined: 2003-02-26
Posts: 34
Posted: Sat, 2003-03-08 17:30

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

 
semisphere

Joined: 2003-02-19
Posts: 8
Posted: Tue, 2003-03-11 00:05

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

 
jdr0606

Joined: 2003-01-06
Posts: 63
Posted: Wed, 2003-03-12 20:31

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

 
semisphere

Joined: 2003-02-19
Posts: 8
Posted: Thu, 2003-03-13 01:57

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.

 
semisphere

Joined: 2003-02-19
Posts: 8
Posted: Thu, 2003-03-13 22:35

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.

 
zake

Joined: 2002-10-20
Posts: 6
Posted: Fri, 2003-03-14 10:16

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?

 
semisphere

Joined: 2003-02-19
Posts: 8
Posted: Sat, 2003-03-15 04:43

it does it dynamically. I can change the watermark whenever I want.

 
diomark
diomark's picture

Joined: 2003-03-14
Posts: 90
Posted: Mon, 2003-03-24 01:05

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

 
nunor

Joined: 2003-03-25
Posts: 2
Posted: Tue, 2003-03-25 21:57

Does anyone was able to get the watermark mod to work?
The images are not shown ... but no error message is visible.

 
jdr0606

Joined: 2003-01-06
Posts: 63
Posted: Sun, 2003-03-30 02:56

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

 
joan
joan's picture

Joined: 2002-10-21
Posts: 3473
Posted: Sun, 2003-03-30 13:03

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
}

 
jdr0606

Joined: 2003-01-06
Posts: 63
Posted: Sun, 2003-03-30 20:11

Thanks!

I want to go one step further to check whether it is a .gif vs .jpg type.

Jeff...

 
jdr0606

Joined: 2003-01-06
Posts: 63
Posted: Tue, 2003-04-01 19:44

Joan, any suggestions how to check what the file type is in Album.php. Everything I try gives me a non-object error.

Thanks

 
joan
joan's picture

Joined: 2002-10-21
Posts: 3473
Posted: Tue, 2003-04-01 23:12

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

 
jdr0606

Joined: 2003-01-06
Posts: 63
Posted: Wed, 2003-04-02 02:59

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

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Wed, 2003-04-02 07:14

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

 
jdr0606

Joined: 2003-01-06
Posts: 63
Posted: Wed, 2003-04-02 22:56

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

 
phgrove

Joined: 2002-10-10
Posts: 101
Posted: Thu, 2003-04-03 07:06

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

 
jdr0606

Joined: 2003-01-06
Posts: 63
Posted: Thu, 2003-04-03 07:31

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

 
mlaia

Joined: 2002-11-01
Posts: 44
Posted: Thu, 2003-04-03 12:45

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

 
nunor

Joined: 2003-03-25
Posts: 2
Posted: Fri, 2003-04-04 02:00

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 &amp; windows machine with GD 2.0 installed

 
mlaia

Joined: 2002-11-01
Posts: 44
Posted: Fri, 2003-04-04 11:13

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.

Quote:
Warning: getimagesize(f:/webdoc/gallery/albumswatermark.jpg) [function.getimagesize]: failed to create stream: No such file or directory in f:webdocgallerywatermark.php on line 110

In this case, need a bar before watermark.jpg

Warning: imagecreatefromjpeg(f:/webdoc/gallery/albumswatermark.jpg) [function.imagecreatefromjpeg]: failed to create stream: No such file or directory in f:webdocgallerywatermark.php on line 113

In this case, need a bar before watermark.jpg, too

Warning: imagecopymerge(): supplied argument is not a valid Image resource in f:webdocgallerywatermark.php on line 150

In this case, I unknown the problem, too.

Warning: Cannot modify header information - headers already sent by (output started at f:webdocgallerywatermark.php:110) in f:webdocgallerywatermark.php on line 154

In this case, I unknown the problem, too.

Warning: imagejpeg(): supplied argument is not a valid Image resource in f:webdocgallerywatermark.php on line 157

In this case, I unknown the problem, too.

Warning: imagedestroy(): supplied argument is not a valid Image resource in f:webdocgallerywatermark.php on line 160

My watermark is in the f:\webdoc\gallery\images\watermark.jpg

Thanks

Marcelo :cry:

 
mlaia

Joined: 2002-11-01
Posts: 44
Posted: Sat, 2003-04-12 12:55

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