if($_GET['crops']) $_GET['crop'] = $_GET['crops'];
if(!isset($_GET['debug'])) $_GET['debug'] = false;
$src_x = 0;
$src_y = 0;
//resizing con funzione di caching ;)
//
//header('Content-Disposition: attachment; filename='.$_GET['file']); list($object) = explode('/', $_GET['file']);
if(!is_file($_GET['file']))
{
list($object) = explode('/',$_GET['file']);
if(is_file($object.'.jpg')) $_GET['file'] = $object.'.jpg';
else $_GET['file'] = 'not_inserted.png';
}
function mkdir_r($dirName, $rights=0777){
$dirs = explode('/', $dirName);
$dir='';
foreach ($dirs as $part) {
$dir.=$part.'/';
//echo $dir;
if (!is_dir($dir) && strlen($dir)>0)
mkdir($dir, $rights);
}
}
list(,$ext) = explode('.', $_GET['file']);
$ext = strtolower($ext);
list($width_orig, $height_orig) = getimagesize($_GET['file']);
if(is_numeric($_GET['imgWidth']) and is_numeric($_GET['imgHeight']))
{
$req_width = $width = $_GET['imgWidth'];
$req_height = $height = $_GET['imgHeight'];
}
else if(is_numeric($_GET['imgWidth']) and !is_numeric($_GET['imgHeight']))
{
$req_width = $width = $_GET['imgWidth'];
$req_height = $height = round($height_orig * $req_width / $width_orig);
}
else if(!is_numeric($_GET['imgWidth']) and is_numeric($_GET['imgHeight']))
{
$req_height = $height = $_GET['imgHeight'];
$req_width = $width = round($width_orig * $req_height / $height_orig);
}
else
{
$req_width = $width = $width_orig;
$req_height = $height = $height_orig;
}
if($_GET['debug'])
{
ini_set('display_errors', 'On');
}
else
{
header('Content-type: image/jpg');
}
if(isset($_GET['crop']))
{
$cache = 'crops';
}
else
{
$cache = 'thumbs';
}
if(is_file("$cache/{$_GET['imgWidth']}/{$_GET['imgHeight']}/{$_GET['file']}"))
{
if($_GET['debug']) echo 'CACHE HIT
';
echo(file_get_contents("$cache/{$_GET['imgWidth']}/{$_GET['imgHeight']}/{$_GET['file']}"));
exit(0);
}
else
{
if($_GET['debug']) echo 'CACHE MISS
';
if(isset($_GET['crop']))
{
$rapp_larg = $width_orig / $width;
$rapp_alt = $height_orig / $height;
if($rapp_larg > $rapp_alt)
{
$src_x = ($width_orig - ($width * $rapp_alt)) / 2;
$width_orig -= $src_x * 2;
}
else if ($rapp_larg < $rapp_alt)
{
$src_y = ($height_orig - ($height * $rapp_larg)) / 2;
$height_orig -= $src_y * 2;
}
}
else
{
if ($width && ($width_orig < $height_orig))
{
$width = round(($height / $height_orig) * $width_orig);
}
else
{
$height = round(($width / $width_orig) * $height_orig);
}
}
switch($ext)
{
case "jpg":
case 'jpeg':
$imageSrc = imagecreatefromjpeg($_GET['file']);
break;
case "png":
$imageSrc = imagecreatefrompng($_GET['file']);
break;
case 'gif':
$imageSrc = imagecreatefromgif($_GET['file']);
break;
}
// The recommended approach is the usage of the GD2.x functions.
// Create an empty thumbnail image.
$imageDest = imagecreatetruecolor($width, $height);
// Try to create the thumbnail from the source image.
imagecopyresampled($imageDest, $imageSrc, 0, 0, $src_x, $src_y, $width, $height, $width_orig, $height_orig);
// save the thumbnail image into a file.
$partial_path = explode("/", $_GET['file']);
array_pop($partial_path);
$partial_path = implode("/", $partial_path);
mkdir_r("$cache/{$_GET['imgWidth']}/{$_GET['imgHeight']}/$partial_path");
imagejpeg($imageDest, "$cache/{$_GET['imgWidth']}/{$_GET['imgHeight']}/{$_GET['file']}", 80);
}
imagejpeg($imageDest, NULL, 80);
if($imageSrc) imagedestroy($imageSrc);
imagedestroy($imageDest);
?>