<?php //ini_set("memory_limit", "128M"); // for large images so that we do not get "Allowed memory exhausted"?>

<?php
// upload the file
if ($_SESSION['FirstName'] !== "") {
	//echo "type: ".$_FILES["footer_upload_box"]["type"];
	// file needs to be jpg,gif,bmp,x-png and 4 MB max
	if (($_FILES["footer_upload_box"]["type"] == "image/jpeg" || $_FILES["footer_upload_box"]["type"] == "image/pjpeg" || $_FILES["footer_upload_box"]["type"] == "image/gif" || $_FILES["footer_upload_box"]["type"]=="image/x-png" || $_FILES["footer_upload_box"]["type"]== "image/png") && ($_FILES["footer_upload_box"]["size"] < 4000000))
	//if (($_FILES["footer_upload_box"]["type"] == "image/jpeg" || $_FILES["footer_upload_box"]["type"] == "image/pjpeg" || $_FILES["footer_upload_box"]["type"] == "image/jpg") && ($_FILES["footer_upload_box"]["size"] < 4000000))

	{
		
  
		// some settings
		$max_upload_width = 900; //2592;
		$max_upload_height = 200; //1944;
		//$max_thumb_width = 300; //2592;
		//$max_thumb_height = 100; //1944;
		  
		// if user chose properly then scale down the image according to user preferances
		/*
		if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
			$max_upload_width = $_REQUEST['max_width_box'];
		}    
		if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
			$max_upload_height = $_REQUEST['max_height_box'];
		}	
		*/
		
		// if uploaded image was JPG/JPEG
		if($_FILES["footer_upload_box"]["type"] == "image/jpeg" || $_FILES["footer_upload_box"]["type"] == "image/pjpeg"){	
			$image_source = imagecreatefromjpeg($_FILES["footer_upload_box"]["tmp_name"]);
			$ext = ".jpg";
		}		
		// if uploaded image was GIF
		if($_FILES["footer_upload_box"]["type"] == "image/gif"){	
			$image_source = imagecreatefromgif($_FILES["footer_upload_box"]["tmp_name"]);
			$ext = ".gif";
		}	
		// BMP doesn't seem to be supported so remove it form above image type test (reject bmps)	
		// if uploaded image was BMP
		//if($_FILES["footer_upload_box"]["type"] == "image/bmp"){
			//$image_source = imagecreatefromwbmp($_FILES["footer_upload_box"]["tmp_name"]);
		//}
		// if uploaded image was PNG
		if($_FILES["footer_upload_box"]["type"] == "image/x-png" || $_FILES["footer_upload_box"]["type"] == "image/png"){
			$image_source = imagecreatefrompng($_FILES["footer_upload_box"]["tmp_name"]);
			$ext = ".png";
		}
		
    $name="template". $_POST[recordkey] ."_footer_". time().$ext ;
    //$thumb_name="item" . $itemKey ."_thumb". $ext ;
    
		$remote_file = "../custom/images/email/".$_FILES["footer_upload_box"]. $name;
		//$remote_thumb_file = "../custom/images/email/".$_FILES["footer_upload_box"]. $thumb_name;
		
		//echo "remote_file= " . $remote_file  ;
		//$remote_file = "../images/". $name;
		imagejpeg($image_source,$remote_file,100);
		chmod($remote_file,0644);
		
		//imagejpeg($image_source,$remote_thumb_file,100);
		//chmod($remote_thumb_file,0644);
	

		// get width and height of original image, determine new proportionate size
		list($image_width, $image_height) = getimagesize($remote_file);
	
		if($image_width>$max_upload_width || $image_height >$max_upload_height){
			$proportions = $image_width/$image_height;
			
			if($image_width>$image_height){
				$new_width = $max_upload_width;
				$new_height = round($max_upload_width/$proportions);
			}		
			else{
				$new_height = $max_upload_height;
				$new_width = round($max_upload_height*$proportions);
			}		
			
			
			$new_image = imagecreatetruecolor($new_width , $new_height);
			$image_source = imagecreatefromjpeg($remote_file);
			
			imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
			imagejpeg($new_image,$remote_file,100);
			
			imagedestroy($new_image);
		}
		
		// get width and height of original image, determine new proportionate size for thumbnail version
		/*
		list($image_width, $image_height) = getimagesize($remote_thumb_file);
	
		if($image_width>$max_thumb_width || $image_height >$max_thumb_height){
			$proportions = $image_width/$image_height;
			
			if($image_width>$image_height){
				$new_width = $max_thumb_width;
				$new_height = round($max_thumb_width/$proportions);
			}		
			else{
				$new_height = $max_thumb_height;
				$new_width = round($max_thumb_height*$proportions);
			}		
			
			
			$new_image = imagecreatetruecolor($new_width , $new_height);
			$image_source = imagecreatefromjpeg($remote_thumb_file);
			
			imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
			imagejpeg($new_image,$remote_thumb_file,100);
			
			imagedestroy($new_image);
		}
		*/
		imagedestroy($image_source);
		
    //all ok, msg here
		//footer("Location: ". $_SERVER['PHP_SELF'] . "?key=" . $nt[couponNumber] . "&upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["footer_upload_box"]["name"]);
		//exit;
  //$pathname = "../images/menu/". $name ;
		$pathname = "../custom/images/email/". $name ;
		rename($remote_file, $pathname);
		//$thumb_pathname = "../custom/images/email/". $thumb_name ;
		//rename($remote_thumb_file, $thumb_pathname);
		//save image name to coupon table
		$query1 = "update email_templates set footer_image_url = '$name' where recordKey =" . $_POST[recordkey] ;
		mysql_query($query1);
		
		//echo ' upload ok.';
	}
	else{
    //error during upload, msg here
		//footer("Location: ". $_SERVER['PHP_SELF'] . "?key=" . $nt[couponNumber] . "&upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
		//exit;
		echo ' error during image upload.';
	}
}
?>