Hi guys. Total newb to PHP... but I have PHP Nuke and now I like it. 😉 Anyway, I looked at my web statistics today and found out I have like 20 people using my bandwidth to show my pictures on their sites, especially in forums and crap. Well, it really doesn't bother me THAT much because my host gives me like 20 gigs of bandwidth and my site is pretty low key... I don't even think I achieve 1gb per month, but anyway... so I figure they can hotlink all the want but my way to get more hits to my site while they do it is to do the watermark.

Anyone firmiliar with cardomain probably knows what I'm really wanting here, when you view the image on cardomain's site it's all fine and dandy, but then you hotlink and it shows their logo in the bottom corner.

Well, now that I've totally shown my newbness, I found a site that said to basically do the same thing you use an .htaccess file on your image folder...

AddHandler watermarked .jpg
AddHandler watermarked .jpeg
AddHandler watermarked .gif
AddHandler watermarked .png

Action watermarked /images/watermarker.php

So fine and dandy, then the watermarker.php file...

<?php

phpinfo(); die();
$watermark = "watermark.png";
$image = $_SERVER["PATH_TRANSLATED"];

if (empty($image)) die();

if (!file_exists($image)) {
header("404 Not Found");
echo "File Not Found."; die();
}

$outputType = getFileType($image);

watermark($image, $watermark, $outputType);

/**
Outputs the image $source with $watermark in the lower right corner.
@ $source the source image
@ $watermark the watermark to apply
@ $outputType the type to output as (png, jpg, gif, etc.)
defaults to the image type of $source if left blank
*/
function watermark($source, $watermark, $outputType="") {
$sourceType = getFileType($source);
$watermarkType = getFileType($watermark);

if (empty($outputType)) $outputType = $sourceType;
header("Content-type:image/$outputType");

// Derive function names
$createSource = "ImageCreateFrom".strtoupper($sourceType);
$showImage = "Image".strtoupper($outputType);
$createWatermark = "ImageCreateFrom".strtoupper($watermarkType);

// Load original and watermark to memory
$output = $createSource($source);
$logo = $createWatermark($watermark);
ImageAlphaBlending($output, true);

// Find proper coordinates so watermark will be in the lower right corner
$x = ImageSX($output) - ImageSX($logo);
$y = ImageSY($output) - ImageSY($logo);

// Display
ImageCopy($output, $logo, $x, $y, 0, 0, ImageSX($logo), ImageSY($logo));
$showImage($output);

// Purge
ImageDestroy($output);
ImageDestroy($logo);
}

function getFileType($string) {
$type = strtolower(eregi_replace("(.*).","",$string));
if ($type == "jpg") $type = "jpeg";
return $type;
}

Well, for one I tried it and this is what happens to the images...

The main thing with the above script though, is that from what I see it doesn't recognize whether or not the image is being accessed by my domain or someone else's site.. I've tried finding more information and found something that said to use $_server[HTTP_REFERER] but I don't know how to. Could someone please point me in the right direction here?

I did a search on the forum, because I figured this was a pretty popular question and although I did find a lot of watermark threads, most said "using GD" and what not, and I'm not using GD. Anyway, any help is greatly appreciated, sorry for the newb-ness. 😉 😃

    just so you know, you are using gd. its a library packed with php to manipulate /create images.

    probably the most simple way would be to replace this:

    $watermark = "watermark.png"; 
    

    with this:

    
    $watermark = ''; // default to no watermark 
    
    // check if browser send a referer header
    if (!empty($_SERVER['HTTP_REFERER'])) {
    
    // check if the referer header contains your website
    if (false === strpos(strtolower($_SERVER['HTTP_REFERER']), 'mywebsite.com')) {
        $watermark = 'come check out mywebsite.com';
    }
    
    }
    
    

    the referer is an OPTIONAL header that browsers are NOT required to send. so some browser wont always send it.

    so what this does now,
    if the browser did NOT send a referer, it will NOT watermark it.
    if the browser DID send a refer header, and the referer DID come from your website, it will NOT watermark it.

    otherwise, it WILL watermark it.

      I take it you are aware the $watermark variable is a file name relative to the images folder in this case and not text to drop in.

      Also, you may want to rely on a regex for your referrer check as the perpertrator(s) may inject your domain in the URL like:

      http://www.theothersite.com/post/?topic=yourwebsite.com

      This probably would not work in most commercial forums but could be done on a home-brewed site.

      You might try:

      $watermark = 'invisible.png'; // default to no watermark
      
      // check if browser send a referer header
      if (!empty($_SERVER['HTTP_REFERER'])) {
      
      // check if the referer header contains your website   
      if (!eregi("https?\:\/\/[^\/]?mywebsite\.com", $_SERVER['HTTP_REFERER'])) {
      	$watermark = 'yourlogo.png';
      }
      
      }
      

      P.S. my escapes for foward-slashes (/) above are not showing up in this forum. if you are unfamiliar with escaping metacharacters in regular expressions, read up!

      Beware my code is untested!

        aha! Got it working, thanks a bunch guys! 😃

          No, thank you! Very cool thing to do. I was unaware that this could even be done (I never had the need or never conceptualized it.)

          😃

            Haha yeah, it was luck that I stumbled across it. It works perfectly, and does span subfolders too. I put the .htaccess into a general folder with a couple subdirectories for categories of pictures and what not, and tested one picture from each section being posted to an off-site forum and sure enough it watermarked it perfectly. 😃

              Write a Reply...