One of the biggest problems with running a website is the use of your material without attribution. I run two websites where photography is a large part - Jeber.com and travelblog.org.

What is Hotlinking?


Hotlinking is the practice of using an image hosted on one website in another. Most commonly this is done without attribution (giving the owner of the image credit for the image), without permission - and to be fair most web users don't realise that it is very bad practice - technically it breaks copyright - but as a webmaster that is the least of the problem, the main problem is the leeching of bandwidth, additional load on the webserver, add to this - visitors to the page don't even know where the image comes from.

Protection by watermarking


Some websites watermark all their images, placing a transparent image over the top of the original image before publishing on the web. This is a solution, but a drastic one, images don't look as good with a watermark.

Here is a solution that provides a solution that works in many ways:

Images that are on your site - show unwatermarked.
Images that are hotlinked - lower the quality and add a watermark, your site logo and address for example.

How to protect your images


By combining a number of tools, most importantly mod_rewrite, imagemagick and a little scripting you can create a generic protection for your images that also has the added advantage of being a little advert for your site every time it's hotlinked. (If bandwidth is the major concern - use -F to forbid the images in the .htaccess file.)

Well with no further ado - here is the code and the instructions:

<?php
/* =====
attribute.php - V0.1
--
Creates a new image that contains a watermarked jpeg images (only)
Designed to be used with mod_rewrite to ensure that hotlinked images
are low size as well attributed correctly.

June 2005 - Alistair Watters
http://www.jeber.com/ and http://www.travelblog.org/

Requirements:

http://www.php.net/ and http://www.imagemagick.org/
Mod_rewrite.

Example: .htaccess
--
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(.+.)?example.org [NC]
RewriteRule (.*.jpg)$ /attribute.php?file=$1 [NC]

--
Beware of the workaround http://www.evildomain.com/example.org/hotlinking-page.html!
if all your images are on www use (www.)?example.org [NC] above.

Writable directory _imgcache - eg. mkdir _imgcache; chown apache _imagecache
watermark.png - put your copyright/original message here.

Released under the terms of the GPLL license.
====== */


$file= $_GET['file'];

$filename = $_SERVER['DOCUMENT_ROOT'] . "/$file";
$cachefile = $_SERVER['DOCUMENT_ROOT'] . "/_imgcache/$file";
$watermark = $_SERVER['DOCUMENT_ROOT'] . "/watermark.png";

if (file_exists($filename) and eregi("jpg$", $filename)) {

if (file_exists($cachefile)) {
// The cache file
header('Content-type: image/jpeg');
readfile($cachefile);
exit();
}

// 1 make the directory.
utils_create_directory($cachefile);

// 2 water mark and drop quality
$cmd = "/usr/bin/composite -gravity SouthEast -watermark 30x30 $watermark +profile '*' -quality 35 $filename $cachefile";
$string =  '$cmd';

header('Content-type: image/jpeg');
readfile($cachefile);
exit();


} else {
header("HTTP/1.0 404 Not Found");
}


// Functions

function utils_create_directory($dirname) { // same as utils.inc
        $dirs = explode('/',$dirname);
        $dirs[count($dirs)-1] = ''; // The last is the filename - drop it.
                                                                                
        foreach ($dirs as $next) {
if (!$next =='') {
                $dir .=  "/$next";                                  
               if (!is_dir($dir)) {
                     mkdir($dir);
                }
                }
        }
                                                                                
}

?>




And to show it in practice - here is an image hotlinked from travelblog.org:


Options: Contact Ali Watters
Code: [blog=4]