Advertisements

Very interesting things  Blog RSS Feed

Protect photos with PHP and .htaccessApril 14 2008

Protect photos with PHP and .htaccessFor sometime now I was looking for a way to protect my photos from been downloaded from my site without my permission. I also wanted to make sure that no images from this site would be hot linked.

There are several methods that can be used to accomplice that. Some people suggest using javascript to disable right clicking of course this is absolutely useless for anyone that has the slightest knowledge of web design as it's very easy to copy the image url from the document source. Others suggest resizing images small enough so they won't be worth copying but again this is no good when have a web gallery showcasing your photos.

The solution to this issue comes by apache and php. Using .htaccess we can disallow access to any folders and prevent hot linking while with php we can easily create a script that resizes the photos and adds a watermark.

Apache .htaccess
This is the easy part:

# Enable mod_rewrite, start rewrite engine 
Options +FollowSymLinks 

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?antonioschrysakis.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://www.antonioschrysakis.com/watermark?src=images/forbid.jpg [NC,R,L]

We create a file called .htaccess it is very important to add the dot in the beginning and place it on the root of our site. First we enable the Apache rewrite engine and then we use two different lines of code.

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?antonioschrysakis.com [NC]

This tells Apache which is our domain and that it should allow photos to be displayed if the referrer is this domain.

RewriteRule \.(jpg|jpeg|png|gif)$ http://www.antonioschrysakis.com/watermark?src=images/forbid.jpg [NC,R,L]

The second line says that if anyone requests any image type file but the referrer is not our website then display a small image called forbid.jpg instead of the original image

That's it really no one can access our photos by typing the url on the browser and no one can hot link our photos on their website.

PHP watermark.php
This is a simple php script which takes an image and places another image on top of it as a watermark. The good thing with it is that we can upload our original photos on the server unmodified and the server will do all the work for us.
header("Content-type: image/jpeg");
$src="../".$src;

    $source = imagecreatefromjpeg($src);
$orig_w=imagesx($source);
    $orig_h=imagesy($source);

//Watemark
$watermark = @imagecreatefrompng('watermark.png');
$watermarkwidth =  imagesx($watermark);
$watermarkheight =  imagesy($watermark);
imagecopy($source, $watermark,  0, 0, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($source);
imagedestroy($source);
imagedestroy($watermark);

The script calculates the with and height of our photo and then calculates the width and height of our watermark. Finally creates a new image by combining the 2 photos. In my case the watemark is my website name but it could be some lines or any other pattern that would prevent someone from copying the photo.

In our html we place a simple image tag with source: 

src="watermark.php?src=images/photos/myphoto.jpg"

but we replace the link to the image with the watermark.php and the directory of the photo.

With this simple script our photos will be watermarked and because of the .htaccess configuration nobody can access the photos directly.

Of course all these techniques are preventing copying and stealing but in general we should never forget the rule that if we don't want something to be taken it's better to avoid adding it in a public space as the internet. It's also recommend that we resize and format our photos to a more web friendly format. It's always easier to protect a photo when it's too small to be printed.

 

Article listed in "Tutorials"

Bookmark and Share

Add a comment

 

Advertisements

 

Comments

search engine image protection says:August 17 2011

Thanks so much for this information! I've thought about this off and on, but didn't quite know what to do about it. Now I have options! Thanks again

Aneeq says:June 6 2012

Watermarking an image in PHP is very easy. If you follow the code below, you can do it in 2 minutes. Note: This code is being provided to you as a help by http://www.phpHelp.co without any warranty and liability Place all the files in a folder on web server. Do not forget to upload the font file – arial.ttf. Once the image is watermarked, it will be placed in the folder named ‘watermarked’. You might need to give ‘write permission’ or ’777 permission’ to the folder named ‘watermarked’ as the new watermarked image will be written in this folder. Source: http://addr.pk/a730e AND http://phphelp.co/2012/06/01/how-to-watermark-an-image-in-php/

 

Add a comment

Security image

Back to all posts

Advertisements