<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
function getExtension($filename) {
	$parts = explode('.', $filename);
	if(!count($parts)) {
	  return '';
	}
	return strtolower($parts[count($parts)-1]);
}

define('DEFAULT_MAX_WIDTH', 175);
define('DEFAULT_MAX_HEIGHT', 131);


$prefix = "mini_";

if(isset($_GET['prefix'])) {
  $prefix .= $_GET['prefix']."_";
}

if(isset($_GET['filename'])) {
  $filename = $_GET['filename'];
}
else {
	// default filename
  $filename = '';
}

if(isset($_GET['max_width'])) {
  $maxX = $_GET['max_width'];
}
else {
  // default maximum image width
  $maxX = DEFAULT_MAX_WIDTH;
}

if(isset($_GET['max_height'])) {
  $maxY = $_GET['max_height'];
}
else {
  // default macimum image height
  $maxY = DEFAULT_MAX_HEIGHT;
}


if(strlen($filename) > 4) {
	$ext = strtolower(getExtension($filename));

	$contentTypeArray = array("gif"=>"gif","jpg"=>"jpeg","jpeg"=>"jpeg","png"=>"png");
	foreach($contentTypeArray as $label=>$extension) {
	  if($label == $ext) {
			$type = $extension;
	    break;
	  }
	}

	$fileSource = $_GET["filename"];
	$fileDest   = "Repositories/Cache/$prefix".str_replace('/', '_', $fileSource.".jpg");

	if(!file_exists($fileDest)) {

		switch($type) {
		  case 'gif':
		  	$imgSource = ImageCreateFromGif($fileSource);
		    break;
			case 'jpeg':
				$imgSource = ImageCreateFromJpeg($fileSource);
				break;
			case 'png':
				$imgSource = ImageCreateFromPng($fileSource);
				break;
		  default :
		    exit();
		    break;
		}

		$widthSrc  = imagesx($imgSource);
		$heightSrc = imagesy($imgSource);

		$widthDest = $widthSrc;
		$heightDest = $heightSrc;

		if($widthSrc > $maxX || $heightSrc > $maxY) {

			if($heightSrc < $widthSrc) {
				$widthDest = $maxX;
				$ratioY = $widthDest/$widthSrc;
				$heightDest = $heightSrc*$ratioY;
			}
			else {
				$heightDest = $maxY;
				$ratioX = $heightDest/$heightSrc;
				$widthDest = $widthSrc*$ratioX;
			}
		}

		if ($widthDest < 1) {
			$widthDest = 1;
		}

		if ($heightDest < 1) {
			$heightDest = 1;
		}

		$imgDest = ImageCreateTrueColor ($widthDest, $heightDest);
		ImageCopyResampled($imgDest, $imgSource, 0, 0, 0, 0, $widthDest, $heightDest, $widthSrc, $heightSrc);
		ImageJpeg($imgDest, $fileDest, 70);
		imagedestroy($imgDest);
	}

	$handle = fopen ($fileDest, "r");
	$contents = fread ($handle, filesize ($fileDest));
	fclose ($handle);

	header("Content-type: image/jpeg");
	print($contents);
}
?>