All pastes #48651 Raw Edit

[M]ax

public php v1 · immutable
#48651 ·published 2006-04-08 18:18 UTC
rendered paste body
<?/************************************************************************************* * * image generator with hotlink protection * * Copyright 2004 - 2005 Ryan Cummins * All rights reserved * ************************************************************************************* *		filename: app/image.php ************************************************************************************* *///@error_reporting(E_ALL);session_start();// Check to see if the Referer is from the same server.//if(!is_null($_SERVER['HTTP_REFERER']))	{	$referer = substr($_SERVER['HTTP_REFERER'], 7, strlen($_SERVER['HTTP_HOST']));	if($referer != $_SERVER['HTTP_HOST'])		{		die('HOT LINK PROTECTION');		}	}$doc_path_exp = explode('/', $_SERVER["DOCUMENT_ROOT"]);$doc_count = count($doc_path_exp) - 1;$x = 0;while($x != $doc_count)	{	$doc_path .= '/'.$doc_path_exp[$x];	$x++;	}$doc_path = substr($doc_path, 1, 50);// set path varsini_set('include_path', '.:/php/includes:'. $doc_path . '/images');// build an acceptable array//	$acceptable_array = array('jpeg', 'png', 'gif');// check path value for any descrepencies//	$path = preg_replace("/[^a-z0-9_.]/", '', $_GET['path']);	// get extension from path//	$extension = explode('.', $path);	$extension = $extension[1];// add extension type for image creation//	if($extension == "jpg")		{		$extension = "jpeg";		}		$type_upper = strtoupper($extension);// functions////initial create image from file functionfunction create($type_upper, $path)	{	switch(strtolower($type_upper))		{		case "png":		$im = @ImageCreateFromPNG($path);		break;		case "gif":		$im = @ImageCreateFromGIF($path);		break;		case "jpeg":		$im = @ImageCreateFromJPEG($path);		break;		default:		$im = @ImageCreateFromGD($path);		break;		}	return $im;	}	// output image as (type) Dynamicaly select filefunction output($type_upper, $im)	{		switch(strtolower($type_upper))		{		case "png":		ImagePNG($im);		break;		case "gif":		ImageGIF($im);		break;		case "jpeg":		ImageJPEG($im);		break;		default:		ImagePNG($im);		break;		}	return;	}function file_exists_include_path($file) {	$win = strpos($_SERVER['SERVER_SOFTWARE'], 'Win') !== false;	$incp = explode(($win ? ';' : ':'), ini_get('include_path'));	for ($i=count($incp);$i--;) {		$tfile = $incp[$i] . '/' . $file;		if (file_exists($tfile)) {			return realpath($tfile);		}	}	return false;}// function to build and output the image//// this includes a resize/resample function proportionally to its relative size.function build($type_upper, $path, $extension, $acceptable_array, $content_type, $resize, $type, $value)	{	if(in_array($extension, $acceptable_array))		{		$content_type = "Content-type: image/" . $extension;		header($content_type);		if(file_exists_include_path($path))			{			$path = $doc_path.$path;			if($resize == TRUE)				{				if($type == 'percent')					{					$path_is = getimagesize($path);					$path_w = ((int)$path_is[0] / 100) * $value;					$path_h = ((int)$path_is[1] / 100) * $value;					$im = imagecreatetruecolor ($path_w,$path_h) or die ("Cannot Create image");					$img = @create($type_upper, $path);					imagecopyresampled($im,$img,0,0,0,0,$path_w,$path_h,imagesx($img),imagesy($img)) or die("Problem In resizing");					}				elseif($type == 'height')					{					$path_is = getimagesize($path);					$path_w = ((int)$path_is[0] * (int)$value) / (int)$path_is[1];					/*orignal height * new width / original width */					$path_h = (int)$value;					$im = imagecreatetruecolor ($path_w,$path_h) or die ("Cannot Create image");					$img = @create($type_upper, $path);					imagecopyresampled($im,$img,0,0,0,0,$path_w,$path_h,imagesx($img),imagesy($img)) or die("Problem In resizing");					}				else					{					$im = @create($type_upper, $path);					}				}			else				{				$im = @create($type_upper, $path);				}			}		else			{			$width = 120;			$height = 120;			$im = imagecreatetruecolor($width, $height);			imagefill($im, 0, 0, 0x999999);			$arr = imagettfbbox(10, 0, './arial.ttf', 'Image Not Found');			$center = imagesx($im) / 2 - $arr[2] / 2;            imagerectangle($im, 1, 1, imagesx($im)-1, imagesy($im)-1, 0x333333);            imagefilledrectangle($im, 10, 10, imagesx($im)-10, imagesy($im)-10, 0x999999);            imagettftext($im, 10, 0, $center, imagesy($im)/2, 0xffffff, './arial.ttf', 'Image Not Found');				imagealphablending($im, true); 			}		output($type_upper, $im);		}	return;	}$resize = $_GET['resize'];$type = $_GET['type'];$value = $_GET['value'];echo build($type_upper, $path, $extension, $acceptable_array, $content_type, $resize, $type, $value);?>