Advertisement:

Watermarking images on upload using GD2

Aloittaja Burpee, marraskuu 14, 2004, 02:35:36 IP

« edellinen - seuraava »

Burpee

I know that this is theoretically possible and I also know of no other forum software that does this.

The concept is fairly basic: Whenever you attach an image (anything other than a .gif, due to some legal issues) it automatically adds a watermark on it that is located somewhere on the server.

The advantages to this? Well, I think they're rather clear...
If and when people hotlink to an image on your site, they are immediatly also advertising for your site and forum... and thus you can allow your members to upload their own photo's to your forum and let them hotlink to those foto's on other forums... in which case it will become a good thing that people are hotlinking to your site, because they are advertising for it ... :P

I've already cooperated with some people on the Coppermine forums and they have a working script that will do the trick in Coppermine. This script should be relatively easy to adapt so that it works in SMF... right?

Anyway, I don't really think this should be a regular feature, seeing as not nearly every server has GD2 installed and some servers use ImageMagick instead. If it were to become a mod though, it would be very cool :)

Anguz

Cristián Lávaque http://cristianlavaque.com

Grudge

It's really fairly easy to do this, but I wouldn't think it would be a standard feature - mod sounds good though.
I'm only a half geek really...

dschwab9

#3
I do this for images on my site with htaccess.  I detect whether or not the referer is me.  If it is, they just get the normal image.  If it's not, they get a watermarked image with increase compression.  I plan to incorporate it into SMF's dlattach function, but I haven't got around to it.

Here's the code I use:

<?php
// path to the directory this file is in
$path "/var/www/bbs/public_html";

// load the image
if (stristr($image,'jpg'))
$photoImage ImageCreateFromJPEG("$path/$image") or die('Image creation failed');
elseif (
stristr($image,'gif'))
$photoImage ImageCreateFromGIF("$path/$image") or die('Image creation failed');
elseif (
stristr($image,'png'))
$photoImage ImageCreateFromPNG("$path/$image") or die('Image creation failed');

// This makes the underlying image show through the transparent areas.  Transparency doesn't work with a GIF source
// maybe should convert to png first if source is gif?
ImageAlphaBlending($photoImagetrue); 

// Load the logo
$logoImage ImageCreateFromPNG("hotlink/bbslogo.png"); 

// Set the width and height to the same as the original file
$logoW ImageSX($logoImage); 
$logoH ImageSY($logoImage); 

// overlay the files
ImageCopy($photoImage$logoImage1100$logoW$logoH); 

// send the headers.  We don't want this cached
Header "Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header "Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
Header "Cache-Control: no-store, no-cache, must-revalidate");
Header "Cache-Control: post-check=0, pre-check=0"false);
Header "Pragma: no-cache");
Header "Content-type: image/jpeg");
Header "Content-Disposition: inline; filename=$image");

// output to browser in jpeg format regardless of input file type
ImageJPEG($photoImage); 

// free up the memory
ImageDestroy($photoImage); 
ImageDestroy($logoImage); 
?>


The rewrite engine changes the URL to something like http://bbs.zuwharrie.com/watermark.php?image=/Themes/default/images/selogo.gif, which is outputted like this:



You'd have to change the paths to match your server of course.  The logo itself is a transparent PNG.   Make sure you destroy any images you create, otherwise you'll fill up the RAM and bring down the server.

Burpee

Sounds like a great idea, Derek...

However, this is only usable for servers that don't have a lot of images hotlinked.
I believe that about 300 images on our site are being hotlinked right now on other forums which are also fairly heavily visited.
Applying your script to my server (or any large site's server) would probably bring it down rather quickly because all the watermarking happens on-the-fly.. Which means that the server needs to apply the watermark at every request that is made for that certain image...

Personally, I'd much rather see it stored on the server with the watermark so that it doesn't have such a big effect on the server... Applying the watermark only once, when an image is uploaded, would be the perfect solution for that...

dschwab9

It doesn't take any significant CPU power unless the images are HUGE.  I have a ton of stuff hotlinked on some high traffic forums, and it doesn't affect me.  I had about 20,000 hits to that script last month.

BUT, if you want to save it to a file, try something like this instead of ImageJPEG($photoImage)

ImageJPEG($photoImage,"/path/to/the/destination/file",75);

Kindred

My suggestion would be to check the coppermine forums. I know there are a couple of mods on those forums for permanently watermarking (and resaving, local copies of) pictures.


As a matter of fact, this thread has some code which seems moderately self-contained and might be used to do the watermarking...

http://coppermine.sourceforge.net/board/index.php?topic=7160.0
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Burpee

#7
* Burpee coughs

Lainaus käyttäjältä: Burpee - marraskuu 14, 2004, 02:35:36 IP
I've already cooperated with some people on the Coppermine forums and they have a working script that will do the trick in Coppermine. This script should be relatively easy to adapt so that it works in SMF... right?

And as for your suggestion, if it doesn't have that big of a load on the server then it's certainly an option... But, the idea behind watermarking is that you "copyright" your image to your site... so if a version without a copyright is still available on your own forum it's still not really thoroughly watermarked...
Personally, I'd prefer having both images watermarked and have them saved with the watermark... and only with attached files... I'd do it myself, if I actually knew how to :P

Kindred

whoops...   sorry about that Burpee, I totally missed that coppermine comment in the original post.

* Kindred hides head in shame
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

dschwab9

In my case, I don't want the watermark to show on my site.  I just want the free advertising when someone hotlinks it.

asstreet

Lainaus käyttäjältä: dschwab9 - marraskuu 14, 2004, 10:51:30 IP
I do this for images on my site with htaccess.  I detect whether or not the referer is me.  If it is, they just get the normal image.  If it's not, they get a watermarked image with increase compression.  I plan to incorporate it into SMF's dlattach function, but I haven't got around to it.

Here's the code I use:

<?php
// path to the directory this file is in
$path "/var/www/bbs/public_html";

// load the image
if (stristr($image,'jpg'))
$photoImage ImageCreateFromJPEG("$path/$image") or die('Image creation failed');
elseif (
stristr($image,'gif'))
$photoImage ImageCreateFromGIF("$path/$image") or die('Image creation failed');
elseif (
stristr($image,'png'))
$photoImage ImageCreateFromPNG("$path/$image") or die('Image creation failed');

// This makes the underlying image show through the transparent areas.  Transparency doesn't work with a GIF source
// maybe should convert to png first if source is gif?
ImageAlphaBlending($photoImagetrue); 

// Load the logo
$logoImage ImageCreateFromPNG("hotlink/bbslogo.png"); 

// Set the width and height to the same as the original file
$logoW ImageSX($logoImage); 
$logoH ImageSY($logoImage); 

// overlay the files
ImageCopy($photoImage$logoImage1100$logoW$logoH); 

// send the headers.  We don't want this cached
Header "Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header "Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
Header "Cache-Control: no-store, no-cache, must-revalidate");
Header "Cache-Control: post-check=0, pre-check=0"false);
Header "Pragma: no-cache");
Header "Content-type: image/jpeg");
Header "Content-Disposition: inline; filename=$image");

// output to browser in jpeg format regardless of input file type
ImageJPEG($photoImage); 

// free up the memory
ImageDestroy($photoImage); 
ImageDestroy($logoImage); 
?>


The rewrite engine changes the URL to something like hxxp:bbs.zuwharrie.com/watermark.php?image=/Themes/default/images/selogo.gif [nonactive], which is outputted like this:



You'd have to change the paths to match your server of course.  The logo itself is a transparent PNG.   Make sure you destroy any images you create, otherwise you'll fill up the RAM and bring down the server.




Hi,

Would you please tell me where i am going to put this code? I am sorry about this dumb question but i am new on php thing. Please show me the way out on this code. I really need that asap.
Thank you.

NoRad

Brilliant idea. Any clue on how to make it happen with IIS?

Gargoyle

Lainaus käyttäjältä: dschwab9 - marraskuu 14, 2004, 10:51:30 IP
I do this for images on my site with htaccess.  I detect whether or not the referer is me.  If it is, they just get the normal image.  If it's not, they get a watermarked image with increase compression.  I plan to incorporate it into SMF's dlattach function, but I haven't got around to it.

Here's the code I use:

<?php
// path to the directory this file is in
$path "/var/www/bbs/public_html";

// load the image
if (stristr($image,'jpg'))
$photoImage ImageCreateFromJPEG("$path/$image") or die('Image creation failed');
elseif (
stristr($image,'gif'))
$photoImage ImageCreateFromGIF("$path/$image") or die('Image creation failed');
elseif (
stristr($image,'png'))
$photoImage ImageCreateFromPNG("$path/$image") or die('Image creation failed');

// This makes the underlying image show through the transparent areas.  Transparency doesn't work with a GIF source
// maybe should convert to png first if source is gif?
ImageAlphaBlending($photoImagetrue); 

// Load the logo
$logoImage ImageCreateFromPNG("hotlink/bbslogo.png"); 

// Set the width and height to the same as the original file
$logoW ImageSX($logoImage); 
$logoH ImageSY($logoImage); 

// overlay the files
ImageCopy($photoImage$logoImage1100$logoW$logoH); 

// send the headers.  We don't want this cached
Header "Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header "Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
Header "Cache-Control: no-store, no-cache, must-revalidate");
Header "Cache-Control: post-check=0, pre-check=0"false);
Header "Pragma: no-cache");
Header "Content-type: image/jpeg");
Header "Content-Disposition: inline; filename=$image");

// output to browser in jpeg format regardless of input file type
ImageJPEG($photoImage); 

// free up the memory
ImageDestroy($photoImage); 
ImageDestroy($logoImage); 
?>


The rewrite engine changes the URL to something like http://bbs.zuwharrie.com/watermark.php?image=/Themes/default/images/selogo.gif, which is outputted like this:



You'd have to change the paths to match your server of course.  The logo itself is a transparent PNG.   Make sure you destroy any images you create, otherwise you'll fill up the RAM and bring down the server.

Anyway do to this with a remotely hosted windows server ?

dschwab9

Lainaus käyttäjältä: Gargoyle - syyskuu 08, 2005, 07:52:18 IP
Anyway do to this with a remotely hosted windows server ?

If you're running Apache, yes, it all should work just the same.

Gargoyle

No I am not running apache.... Just windows....

dschwab9


kegobeer

Lainaus käyttäjältä: dschwab9 - syyskuu 10, 2005, 12:04:52 AP
Won't work on IIS as far as I know.

The htaccess file won't work, but I believe there are ways to do mod rewrite with IIS by using ISAPI filters.  Check out the following links:

http://www.motobit.com/help/url-replacer-rewriter/iis-mod-rewrite.asp
http://www.isapirewrite.com/docs/

I did a quick Google - I recommend doing some good surfing to find out more.  Visit IIS specific sites as I'm sure they will yield a lot of good information.
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

Advertisement: