<?php
header("Connection: close");
$max_width = 600;
$max_height = 600;
$imagequality = 80;
$watermark = true;
// Path of the original image, ideally get this from an id passed as a querystring
// or use .htaccess to redirect all images though this script
$path = "./images/file.jpg";
if (!file_exists($path)) {
header('HTTP/1.0 404 Not Found');
header('Content-type: image/jpeg');
$img = imagecreatefromjpeg("./images/404.jpg");
imagejpeg($img, null, 50);
imagedestroy($img);
exit;
} Else {
//Hotlinking Referer Check
if (preg_match('/www\.yoursite\.co\.uk/i', $_SERVER["HTTP_REFERER"])) {
$refererpassed = true;
} else {
$refererpassed = false;
}
if(!$refererpassed){
header('HTTP/1.0 403 Forbidden');
header('Content-type: image/gif');
$img = imagecreatefromgif("./images/image_to_show_if_hotlinked.jpg");
imagegif($img, null, 100);
imagedestroy($img);
mysql_close($connectionid);
exit;
}
// - End Referer check
// Cache control
header("Cache-Control: public");
$last_modified = filemtime($path);
// check if the browser has this image in its cache,
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
} else {
$if_modified_since = 0;
}
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $last_modified) . " GMT");
//if not modified tell browser to used cached version
if (strtotime($if_modified_since) >= $last_modified) {
header('HTTP/1.0 304 Not Modified');
exit;
}
$image = imagecreatefromstring($imagedata[0]['content']);
$width_orig = imagesx($image);
$height_orig = imagesy($image);
$ratio_orig = $width_orig/$height_orig;
if ($max_width/$max_height > $ratio_orig) {
$width = $max_height*$ratio_orig;
} else {
$height = $max_width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
// Resample/Resize image
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
if($watermark){
// Colours
$black = imagecolorallocate($image_p, 0, 0, 0 );
$white = imagecolorallocate($image_p, 255, 255, 255);
// Add black border at top
imagefilledrectangle($image_p,0,0,$width,20,$black);
$tag = "Copyright http://www.yoursite.co.uk/";
/*The first parameter is our handle, then font size, rotation,
starting X, starting Y, text color, font, and finally our text. */
ImageTTFText($image_p, 10, 0, $width - 290, 15, $white, "./Verdana_Bold.ttf", $tag);
}
switch(file_extension($path)){
case "jpg":
case "jpeg":
header('Content-type: image/jpeg');
imagejpeg($image_p, null, $imagequality);
break;
case "gif":
header('Content-type: image/gif');
imagegif($image_p, null, $imagequality);
break;
case "png":
header('Content-type: image/png');
imagepng($image_p, null, $imagequality);
break;
default:
header('Content-type: image/jpeg');
imagejpeg($image_p, null, $imagequality);
break;
}
//cleanup
imagedestroy($image);
imagedestroy($image_p);
}
function file_extension($filename)
{
$path_info = pathinfo($filename);
return $path_info['extension'];
}
?>