4

I want to know if it is possible to protect images in my host that are loaded from outside by adding a watermark using .htaccess?

That is, if another site uses my image URL http://example.com/1.jpg in a img tag in their own websites.

The plan is the when a foreign request comes to my host, I add a watermark to it and send it to the user browsing the foreign site.

hakre
  • 193,403
  • 52
  • 435
  • 836
bizzr3
  • 1,925
  • 4
  • 24
  • 37
  • I don't think it's possible with htaccess proper. You'd have to probably redirect the user to some script (PHP, etc.) that watermarked the image. – Ian Hunter Jan 28 '12 at 09:00
  • ok i can d othis right now ! but i want to detect foreign request comming to my host from another website !! – bizzr3 Jan 28 '12 at 09:02
  • @bizzare Check referer field of the request. – Cheery Jan 28 '12 at 09:03
  • 1
    @bizzare You're looking to stop something called hotlinking. Check this out: http://www.javascriptkit.com/howto/htaccess10.shtml – Ian Hunter Jan 28 '12 at 09:05
  • @Beanland yeah i was check that older , and my problem is detecting foreign request that using my image URL in his/her own img tags ! – bizzr3 Jan 28 '12 at 09:07
  • @bizzare it is simple: RewriteCond %{HTTP_REFERER} !^http ://www\.yoursite\.com [NC] RewriteRule ^(.*\.(png|gif|jpg)) /your_script.php?image=$1 [NC,L] ps: no space after http - it just breaks here ) – Cheery Jan 28 '12 at 09:10
  • @cheery can you show me a complete sample please ? – bizzr3 Jan 28 '12 at 09:12

3 Answers3

2

What you basically want to do, is start with this tutorial:

http://www.alistapart.com/articles/hotlinking/

This shows you how to redirect images that come from external sites to a PHP page. Then, you can use that PHP page to watermark your image, with something like this:

<?php
header('content-type: image/jpeg');
$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['pic']);
$size = getimagesize($_GET['pic']);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
cha0site
  • 10,517
  • 3
  • 33
  • 51
  • hummm i cant understand this part of htaccess `RewriteCond %{HTTP_REFERER} !yoursite\.com [NC]` i'm working on localhost with wamp server with windows xp – bizzr3 Jan 28 '12 at 09:50
  • 1
    @bizzare: It's a condition for this rewrite rule, you need to modify it. It basically means that the request didn't come from *your* site, that is, it's a foreign request. – cha0site Jan 28 '12 at 09:52
  • you means that i must change it to my own website URL ? – bizzr3 Jan 28 '12 at 09:53
  • ok ! is it ture ? `RewriteCond %{HTTP_REFERER} !http://localhost/hotlinking [NC]` – bizzr3 Jan 28 '12 at 09:56
  • 1
    @bizzare: Just `RewriteCond %{HTTP_REFERER} !localhost [NC]` will do ("everything that's coming from localhost is fine"), but no one can hotlink you this way... You can't really test it – cha0site Jan 28 '12 at 10:00
  • sorry , something wrong here ! the request do not redirect to `watermark.php` im using this line : `RewriteRule (.*) watermark.php?pic=$1` – bizzr3 Jan 28 '12 at 10:36
  • Try `RewriteRule (.*) /watermark.php?pic=$1`. Also, how are you testing this? – cha0site Jan 28 '12 at 10:37
  • i'm testing with wamp server and something work fine like when i use image URL from another place in `www` directory or with IP address the ALT tag of images appears and the image was not loaded but the watermark do not work currectly ! – bizzr3 Jan 28 '12 at 10:39
  • oh no ! i open watermark.php and it say : `can not display image because it contain some errors` – bizzr3 Jan 28 '12 at 10:50
  • 1
    @bizzare: Perhaps you should open another question. – cha0site Jan 28 '12 at 10:55
1

The .htaccess cannot add watermarks to your images. However, it can restrict access to your images.

However, you can add watermarks using PHP GD Library . The below code shows you how to add a watermark to your image through PHP GD.

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

$stamp is the content what you want to be on watermark (eg: Copyrights Reserved) and $im is your actual image which has to be protected.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • thanks men ! but this is not solve my problem ! i just want to detect foreign referer that using my image files and then redirect it to GD enabled php file like `watermark.php` – bizzr3 Jan 28 '12 at 09:15
0

you must use HTTP_REFERER to determinate if user came from which link and then compare it with your own domain. if request domain was different then use php gd lib to add watermark on your images. you should check if HTTP_REFERER is set or not. by default if user not came from another page to your site and came directly it will be empty and not set.

if(isset($_SERVER['HTTP_REFERER'])) {
      #check if its from external domin
      # do something here 
}
Osiris
  • 131
  • 3
  • 16