Smarter hotlink prevention
Views: 1768
Comments: 0
Posted: 5-2-2007
based on 0 votes.
Faves: 5 | + Faves
Page(s): 1
Usually people prevent hotlinking by replacing the requested image with funny or even pornographic image. This is not the best way, because people will not know the actual owner of image if they are looking similar to this.


Here's the best way I've come so far, with explanation.



.htaccess file



I bet that many of you already know a way to replace stolen image with other, but this is little bit more complicated, so I explain it a bit:
1
2
3
4
5
#insert the .htaccess file to your site's parent directory (it's often public_html)
rewritecond %{http_referer} !^$   #check that previous page refers to requested one
rewritecond %{http_referer} !^http://([^.]*[.]*)?site.com/ [nc]   #if the referring page is yours...
rewritecond %{http_referer} !^http://(www.)?google.com/ [nc]   #...or Google stop processing the script.
rewriterule (.*.(png|gif|jpeg|jpg)) http://www.site.com/watermark.php?image=http://www.site.com/$1   #but if the previous conditions equal true, catch the name of the image that is being requested and replace it with the watermarked one.



That wasn't hard, wasn't it? Next we will create php file that is needed for watermarking the images.




watermark.php



In this script are many functions that are only available for the users of GD library plugin. If you haven't got it, go here.


The script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
 
 
    $
font = "verdana.TTF"// the font what is used for the watermark. must be in the same directory as the script.
    $
text = "(c) site.com"// text for the watermark.
    $
font_size = 16// the size of text in points (pt).
 
    $
size = imagettfbbox (   $font_size, 0, $font, $text   )// find out dimensions of the specified text.
    $
text_width = $size[2] + $size[0] + 8// calculating width...
    $
text_height = $size[1] + $size[7]// ...and height of the text.
 
 
    $
ext = end (   explode (   ".", $_GET["image"]   )   )// find out extension (or type) of the image, so we can use the right function
 
   
switch (   $ext   )   {
 
       
case "jpg"// if the image is in jpg format...
            $
image = imagecreatefromjpeg (   $_GET["image"]   ); break//...we use the function for including jpg images
       
case "jpeg"// ^^
            $
image = imagecreatefromjpeg (   $_GET["image"]   ); break;
       
case "png"// and so on...
            $
image = imagecreatefrompng (   $_GET["image"]   ); break;
       
case "gif":
            $
image = imagecreatefromgif (   $_GET["image"]   ); break;
 
   
}
 
 
    $
image_width = imagesx (   $image   )// find out the width...
    $
image_height = imagesy (   $image   )//...and height of the image.
 
    $
text_x = $image_width - $text_width// and then define the x....
    $
text_y = $image_height - $text_height - 30//...and y coordinate for the text, so the text will be placed on the bottom right corner of the image.
 
    $
shadow_x = $text_x + 2// move the shadow 2 pixels right.
    $
shadow_y = $text_y + 2// same as above but this moves it down.
 
 
   
imagettftext (   $image, $font_size, 0, $shadow_x, $shadow_y, imagecolorallocate (   $image, 0, 0, 0   ), $font, $text   )// the text for the shadow is created first, so it stays on bottom and the real text goes over it.
   
imagettftext (   $image, $font_size, 0, $text_x, $text_y, imagecolorallocate (   $image, 255, 195, 60   ), $font, $text   )// create the "real" text for the watermark. in this example it is orange.
 
 
   
header (   "Content-Type: image/png"   )// set the content type header to image/png so it's not displayed in normal html.
 
   
imagepng (   $image   )// print out the image...
   
imagedestroy (   $image   )// and destroy it immediately.
 
 
 
?>



Hope you liked my little tutorial. Please tell me if there's something that you don't understand.
Page(s): 1
More tutorials from this author:

» No tutorials by this user.

View All Tutorials
magiceye - http://blogi.taikasilma.com
This author is too busy writing tutorials instead of writing a personal profile!
Close