<?php
/*
processImage.php
Copyright (C) 2004-2006 Peter Frueh (http://www.ajaxprogrammer.com/)
Additional code contributions and modifications by David Fuller, Olli Jarva, and Simon Jensen

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

// required params: imageName

header("Content-Type: text/plain");
if($_REQUEST['mod'] == 1)
 {
 $originalDirectory = "../static/uploadfiles/homebanners/";
 }
elseif($_REQUEST['mod'] == 2)
 {
 $originalDirectory = "../static/uploadfiles/banners/";
 }
 elseif($_REQUEST['mod'] == 3)
 {
 $originalDirectory = "../static/uploadfiles/galleries/";
 }
 elseif($_REQUEST['mod'] == 4)
 {
 $originalDirectory = "../static/uploadfiles/members/";
 }
 elseif($_REQUEST['mod'] == 5)
 {
 $originalDirectory = "../static/uploadfiles/events/";
 }
 elseif($_REQUEST['mod'] == 6)
 {
 $originalDirectory = "../static/uploadfiles/updates/";
 }
 elseif($_REQUEST['mod'] == 7)
 {
 $originalDirectory = "../static/uploadfiles/expquotes/";
 }

$imageName = $_REQUEST["imageName"];
//print_r($_REQUEST);
if (empty($imageName) || !file_exists($originalDirectory.$imageName)) {
        echo "{imageFound:false}";
        exit;
}

/*echo $originalDirectory.$imageName;
exit;*/
$fileInfo = pathinfo($editDirectory.$imageName);
$extension = strtolower($fileInfo['extension']);

		$out_w = $_REQUEST["w"];
		$out_h = $_REQUEST["h"];
		if (!is_numeric($out_w) || $out_w < 1 || $out_w > 10000 || !is_numeric($out_h) || $out_h < 1 || $out_h > 10000) { exit; }
		list($in_w, $in_h) = getimagesize($originalDirectory.$imageName);

		if ($extension == "jpg" || $extension == "jpeg") {
			$in = imagecreatefromjpeg($originalDirectory.$imageName);
		}
		if ($extension == "gif") {
			$in = imagecreatefromgif($originalDirectory.$imageName);
		}
		if ($extension == "png") {
			$in = imagecreatefrompng($originalDirectory.$imageName);
		}
		
		
		if($_REQUEST['type'] == 1)
		  {
		    if($in_w > $out_w || $in_h > $out_h)
			{
				if($in_w >= $in_h)
				{
				 $out_h = ($out_w*$in_h)/$in_w;
				} 
				else
				{
				 $out_w = ($out_h * $in_w)/$in_h;
				}
			}
			else
			{ 
			 $out_h = $in_h;
			 $out_w = $in_w;
			}
		  }else if($_REQUEST['type'] == 2){
		  
		  	 if($in_h > $out_h){
				 $out_w = ($out_h * $in_w)/$in_h;
			 }else{
			 	$out_h = $in_h;
				$out_w = $in_w;
			 }
		  
		  }
		//echo $originalDirectory.$imageName;
        //exit;
		$out = imagecreatetruecolor($out_w, $out_h);
		imagecopyresampled($out, $in, 0, 0, 0, 0, $out_w, $out_h, $in_w, $in_h);
		if ($extension == "jpg" || $extension == "jpeg") {
			imagejpeg($out);
		}
		if ($extension == "gif") {
			imagegif($out);
		}
		if ($extension == "png") {
			imagepng($out);
		}
		
	//	imagedestroy($in);
	//	imagedestroy($out);
?>