<!-- <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> -->
<?php
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form") && ($_SESSION['FirstName'] !== "")) {
	
	// file needs to be jpg,gif,bmp,x-png and 4 MB max
	if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png" || $_FILES["image_upload_box"]["type"] == "image/png") && ($_FILES["image_upload_box"]["size"] < 4000000))
	//if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/jpg") && ($_FILES["image_upload_box"]["size"] < 4000000))

	{
		

	include('includes/smart_resize_image.php');
	$max_upload_width = 1200; //2592;
	$max_upload_height = 800; //1944;
	$max_thumb_width = 250; 
	$max_thumb_height = 250; 
	// echo $_FILES['image_upload_box']['name'];
	// echo $itemKey;
	// die;
	$imageName     = '';
	$dir           = realpath('../images/menu/');
	$filenameParts = pathinfo($_FILES['image_upload_box']['name']);
	$filename      = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $filenameParts['filename']));
	$ext           = strtolower($filenameParts['extension']);
	//$newFilename   = $filename.'.'.$ext;
	//$thumbName     = $filename.'_thumb'.'.'.$ext;
	$tmp           = $_FILES['image_upload_box']['tmp_name'];
	$tmp1          = $_FILES['image_upload_box']['tmp_name'];
	$i             = time();

	//while (file_exists($dir.'/'.$newFilename)) {
		$newFilename = $filename.'-'.$i.'.'.$ext;
		$thumbName     = $filename.'-'.$i.'_thumb'.'.'.$ext;
	//}

	$dest   = $dir.'/'.$newFilename;
	$dest1   = $dir.'/'.$thumbName;
	$thDest = $dir.'/'.$thumbName;
	
	if ( move_uploaded_file($tmp,$dest) ) {
		
		list($width, $height) = getimagesize($dest);
		if ($width > $max_upload_width || $height > $max_upload_height) {
			if ($width >= $height) {
				smart_resize_image($dest, $max_upload_width, 0, true, $dest);				
			} else{
				smart_resize_image($dest, 0, $max_upload_height, true, $dest);
			}
		}
	
		copy($dest, $dest1);
		list($width, $height) = getimagesize($dest1);
		if ($width > $max_thumb_width || $height > $max_thumb_height) {				
			if ($width >= $height) {				
				smart_resize_image($dest1, $max_thumb_width, 0, true, $dest1);
			} else{
				smart_resize_image($dest1, 0, $max_thumb_height, true, $dest1);
			}
		}
		//copy($dest1, $dir.'/'.$thumbName);
		$imageName = $newFilename;
		$query1 = "update menuItem set imagePath = '" . $imageName . "' where itemKey =" . $itemKey ;
		mysql_query($query1);

	}
	
	
	}
	else{
    //error during upload, msg here
		//header("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.';
	}
}
?>