Search code examples
php.htaccesswatermark

Htaccess and watermark


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.


Solution

  • 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);
    ?>