Publishing System Settings Logout Login Register
Smarter hotlink prevention
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on May 2nd, 2007
2430 views
PHP Coding
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:
#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:

<?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.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
magiceye

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top