Hello. I specialize in graphic design for web and print projects.

TylerM – Tyler Menezes \ Blog \ Enforcing your CC-style copyright on hotlinkers

Enforcing your CC-style copyright on hotlinkers

March 1, 2008 at 12:57 pm

Introduction

Picture the following scenario: you run a blog that posts pictures of cats. These pictures seem to get stolen a lot. You’re okay with this, because your content is licensed under a Creative Commons-style license. But nobody is respecting your copyright! In fact they’re linking to images directly from your server!

In fact this scenario is not all that uncommon. Thousands (if not millions) of blog owners are unaware of other websites hot linking to their images without so much as attribution! This tutorial will show you how to turn the websites of those copyright-discarding, hotlinking fiends into free advertising. More after the jump.

This is assuming you have a reasonable rate for bandwidth, and are okay with the idea of people using your bandwidth on their site. If you’re not, I recommend this tutorial by David Airey.

The Effect

Let’s start with this great picture of a cat (props to oskay frow Flickr):

Multimeter Cat

Looks awesome, but how does this become advertising for my site? Through the power of mod_rewrite and PHP, the image is displayed as the following when embedded on someone else’s page:

Hotlinker view

Using mod_rewrite to detect hotlinkers

The technology that makes this all possible is a module built into the Apache Web Server, mod_rewrite. We can detect hotlinkers by creating a file named .htaccess in the web root. In this file we need to place some code to enable mod_rewrite. So on the first line, add this:

RewriteEngine On

It’s worth refreshing the page to make sure that your web hosting provider has not disabled mod_rewrite, if they have you’ll get an error from Apache. In that case, sorry, but this tutorial won’t work for you.

At this point, I’m assuming you have wod_rewrite enabled, and you haven’t gotten any server errors. Great! Now onto the hotlinker detection.

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?YOUR DOMAIN\.YOUR TLD/ [NC]
RewriteCond %{HTTP_REFERER} !^$

The first line detects people who are seeing images from your website. The second line detects people who are not telling the server where they are viewing the image from. Some overly-paranoid internet users disable the sending of this information, and we can’t forget about them.

To actually redirect the user, we need another line of code. In the end, your code should look like:

Header append Vary Referer
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?YOUR DOMAIN\.YOUR TLD/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /hotlinkers.php?image=$0 [L

PHP to add the watermark

So far we've learned how to internally redirect people viewing hotlinked images to a PHP script. If you view your site right now, however, you'll most likely get an error. That's because the PHP script referenced, /hotlinkers.php, has not been created yet.

I can't explain exactly how PHP works. If you don't have a basic idea, I'd recommend you find a good tutorial on it. I'll assume you at least know how it's structured from this point on.

Without further ado, here is the code. The comments explain what it does, so I shouldn't need to.

  1.  
  2. //Explicit declairations:
  3.  //The PATH to the font to use.
  4.  $font = '/nfsn/content/tylerm/public/profont.ttf';    //Text to show when the width of the image is <= 250px.
  5.  $short = "TylerM.Info";
  6. //Text to show when the width is > 250px.
  7.  $long = "Photo Credit: TylerM.info";
  8. //Code
  9. $file = $_GET['image']; //Set $file to the image passed by the browser.
  10. $info = getimagesize($file); //Get file info
  11. $type = split("/", $info['mime']); //Find the type of image
  12. //This switch will create the correct GD object for different image types.
  13. switch($type[1]){
  14.        case "png":
  15.        $image = ImageCreateFromPNG($file);
  16.        break;
  17. case "gif":
  18.        $image = ImageCreateFromGIF($file);
  19.        break;
  20. case "jpeg":
  21.        $image = ImageCreateFromJPEG($file);
  22.        break;
  23. default:
  24.        echo "Cannot open non-image filetype.";
  25.        exit;
  26.        break;
  27. }
  28.  
  29. //The following code will  select a string of text appropriate to the image.
  30. if($info[0] <= 250){
  31.  $str = $short;
  32. }else{
  33.  $str = $long;
  34. }
  35.  
  36. //Make the text
  37. ImageTTFText($image, 8, 0, 2, $info[1], imagecolorallocate($image, 0x00, 0x00, 0x00), $font, $str); //Shadow
  38. ImageTTFText($image, 8, 0, 1, $info[1] - 1, imagecolorallocate($image, 0xFF, 0xFF, 0xFF), $font, $str); //Text
  39.  
  40. //Send the image to the browser
  41. header("Content-Type: image/png");
  42. ImagePng ($image);
  43.  
  44. //Cleanup
  45. imagedestroy($image);
  46. ?>
  47.  

Possible Improvements

  •  I'm not 100% sure on the security implications at blindly including files. They are checked to ensure they're PNG, GIF, or JPG, but there may be better ways of doing this.
  • I'm running PHP4 here, I don't know how (or if) this works on later releases.
  • Watermarking with a PNG instead of text might be nice.

Update!

Thanks to alexgieg and Jordon (at the NearlyFreeSpeech.NET forums) for the following script, which forwards hotlinked images to a cache service, so you get very little of the hotlinked traffic while still getting the advertising.

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.*\.)?google\..*(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.*\.)?yahoo\..*(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteCond %{HTTP_USER_AGENT} !.*CoralWebPrx.*
RewriteCond %{QUERY_STRING} !(^|&)coral-no-serve$
RewriteRule ^(.*)\.(bmp|gif|ico|jpe?g|png)$ http://www.example.com.nyud.net/watermark.php?image=$1.$2 [R=302,L,NC]

RewriteCond %{QUERY_STRING} (^|&)coral-no-serve$
RewriteRule ^.*$ - [F,L]

Say your words!

XHTML: You can use the tags <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your reply.

Comment Policy: Be nice. If you disagree, feel free to do so, but in a fact-supported manner. (E.g. "You are wrong. For example, the Wikipedia page for this subject says..." is okay, "You are a noob because you disagree with me!" is not.) Swearing is okay, provided it's not just there for the point of swearing and it's not excessive. Trolls aren't okay. This is not YouTube. By commenting you give me a perpetual, non-revokable license to publish your comments on my site. I can modify and delete them at will from this point.

* Your email will never be displayed, and I won't share it with anyone. If you have a Gravitar it will automatically be used.