Location: PHPKode > scripts > Image Hotlink Log and Control > imgproc.php
<?php

// Hotlink Log and Image Watermarking Script
// Copyright (C) 2005  SillyJokes.co.uk

require 'hotlinklogconfig.php';

// Send the requested image to the client's browser. Watermark it if $watermark is TRUE,
// but don't watermark it if it's below a certain size.
function ProcessImage($imgname,$watermark)
{
	global $watermarksettings;
	
	$ext = substr(strrchr($imgname, '.'), 1);
	
	// image format handling stuff.
	// the array index is the file extension.
	// 'imgcreate' is the name of the function to call to create the image.
	// 'imgsquirt' is the function called to squirt the image back to client
	// 'header' is the header to send for this image format.
	// expandable to cover other image types
	$filetypes = array(
		'jpg' => array('imgcreate'=>'imagecreatefromjpeg',	'imgsquirt'=>'imagejpeg',	'header'=>'Content-type: image/jpeg'),
		'gif' => array('imgcreate'=>'imagecreatefromgif',		'imgsquirt'=>'imagegif',	'header'=>'Content-type: image/gif'),
		);
	
	// legal file extension?
	if (empty($filetypes[$ext]))
	{
		return FALSE;
	}
	
	$ft = $filetypes[$ext];

	$im = call_user_func($ft['imgcreate'],$imgname);
	
	// I should maybe split this into another function
	if ($watermark)
	{
		$string='';
		$font=0;
		$drawstring = FALSE;
		$imggirth = imagesx($im);

		// decide which font and string to use based on the width of the image		
		foreach($watermarksettings as $t)
		{
			if ($imggirth>=$t['width'])
			{
				$font = $t['font'];
				$string = $t['caption'];
				$drawstring = TRUE;
				break;
			}
		}
		
		if ($drawstring)
		{
			$fontwidth = imagefontwidth($font);
			$fontheight = imagefontheight($font);
			$strgirth = $fontwidth * strlen($string);
			$colour = imagecolorallocate($im, 64, 64, 128);
			$bgcolour = imagecolorallocate($im, 255, 255, 255);
			$px    = ($imggirth - $strgirth) / 2;
			$py    = imagesy($im) - $fontheight -2;
			
			imagefilledrectangle($im, $px, $py, $px+$strgirth, $py+$fontheight, $bgcolour);
			imagestring($im, $font, $px, $py, $string, $colour);
		}
	}
	
	// send content type header ro client browser
	header($ft['header']);
	
	// squirt the image to the client browser
	call_user_func($ft['imgsquirt'],$im);
		
	imagedestroy($im);
	
	return TRUE;	// success
}



////// MAIN SECTION //////////////////////////////////////////////

$imgname = strip_tags($_REQUEST['pic']);
$action = DEFAULT_ACTION;
$param = '';

// Is there an action for this referrer?

foreach($referrerfilterlist as $r)
{
	if (stristr($_SERVER['HTTP_REFERER'],$r['str']))
	{
		$action = $r['action'];
		if (isset($r['param']))
		{
			$param = $r['param'];
		}
		break;
	}
}

// if no action is found, the default is used

$success = TRUE;	// set to FALSE if something bad happens. Not used at the moment.

switch($action)
{
	case ACTION_PASSTHROUGH:
		$success = ProcessImage($imgname,FALSE);
		// squit image to client
		break;
	case ACTION_BLOCK:
		// do nowt
		break;
	case ACTION_WATERMARK:
		// water..mm..mmm.mmmmm..arrrkk
		$success = ProcessImage($imgname,TRUE);
		break;
	case ACTION_SUBSTITUTE:
		// send a different image to the client, such as a picture of a kitten with 'MEOW' written on it.
		// Send the default image unless another is specfied by the 'param' variable
		$success = ProcessImage(empty($param)?$defaultsubimage:$param,FALSE);
		break;
	default:
		break;
}


// log this event
if (WRITELOG)
{
	$f = fopen(LOGFILE,'a');
	// log image filename, date and time, and referrer. Put referrer last in the line because it might have commas in it.
	$thetime = time();
	$imgname = strip_tags($_REQUEST['pic']);
	fwrite($f,"$imgname,$thetime,$action,$_SERVER[HTTP_REFERER]\n");
	fclose($f);
}


// update counter
if (file_exists(COUNTERFILE))
{
	// could use file() here but it gives me more trouble than it's worth
	$f=fopen(COUNTERFILE,'rb');
	for ($i=0; $i<NUM_ACTIONS; ++$i)
	{
		$wmcount[$i] = intval(fgets($f));
	}
	fclose($f);
	
} else {
	for ($i=0; $i<NUM_ACTIONS; ++$i)
	{
		$wmcount[$i] = 0;
	}
}

// increment appropriate counter
++$wmcount[$action];

$f=fopen(COUNTERFILE,'w');
for ($i=0; $i<NUM_ACTIONS; ++$i)
{
	fwrite($f,intval($wmcount[$i])."\n");
}
fclose($f);

?>
Return current item: Image Hotlink Log and Control