<?php
	/* Note: This thumbnail creation script requires the GD PHP Extension.
		If GD is not installed correctly PHP does not render this page correctly
		and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
	 */

	// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug
	if (isset($_POST["PHPSESSID"])) {
		session_id($_POST["PHPSESSID"]);
	}

	session_start();
	ini_set("html_errors", "0");

	// Check the upload
	if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
		echo "ERROR:invalid upload";
		exit(0);
	}

	$extern = strtolower(substr($_FILES["Filedata"]["name"], strrpos($_FILES["Filedata"]["name"], '.')+1));
	// Get the image and create a thumbnail
		if($extern == 'jpeg' || $extern == 'jpg')
			$img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
		elseif ($extern == 'png' )
			$img = imagecreatefrompng($_FILES["Filedata"]["tmp_name"]);
		elseif ($extern == 'gif')
			$img = imagecreatefromgif($_FILES["Filedata"]["tmp_name"]);
		if (!$img )
		    {
				echo "ERROR:could not create image handle ". $_FILES["Filedata"]["tmp_name"];
				exit(0);
			}

	$width = imageSX($img);
	$height = imageSY($img);

	if (!$width || !$height) {
		echo "ERROR:Invalid width or height";
		exit(0);
	}

	// Build the thumbnail ..................
	$target_width = 134;
	$target_height = 103;
	$target_ratio = $target_width / $target_height;

	$img_ratio = $width / $height;

	if ($target_ratio > $img_ratio) {
		$new_height = $target_height;
		$new_width = $img_ratio * $target_height;
	} else {
		$new_height = $target_width / $img_ratio;
		$new_width = $target_width;
	}

	if ($new_height > $target_height) {
		$new_height = $target_height;
	}
	if ($new_width > $target_width) {
		$new_height = $target_width;
	}

	$new_img = ImageCreateTrueColor(124, 93);
	if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) {	// Fill the image black
		echo "ERROR:Could not fill new image";
		exit(0);
	}

	if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) {
		echo "ERROR:Could not resize image";
		exit(0);
	}

	if (!isset($_SESSION["file_info"])) {
		$_SESSION["file_info"] = array();
	}

	// ends thumbnail.....................


	//saving original file inside folder..
		$save_path = "files/uploads/";
		$upload_name = "Filedata";
		$b = md5(time());
		$path_info = pathinfo($_FILES["Filedata"]["name"]);
		$file_extension = $path_info["extension"];

		$file_data = explode(".", $_FILES[$upload_name]["name"]);
		$name_array = explode("_", $file_data[0]);
		$orgfile_name = '';
		foreach($name_array as $values)
			{
				$orgfile_name.=$values;
			}
		$file_name = 'temp_'.$orgfile_name.'_'.$b.'.'.$file_extension;
		//echo $save_path.$file_name;
		if (!@move_uploaded_file($_FILES["Filedata"]["tmp_name"], $save_path.$file_name)) {
			HandleError("File could not be saved.");
			exit(0);
		}
	//saving thumbnail inside the folder..
		$save_path = "files/uploads/thumbs/";
		$file_name = 'temp_'.$orgfile_name.'_'.$b.'_thumb.'.$file_extension;
		$file_name_original  = $_FILES["Filedata"]["name"];
		//echo $save_path.$file_name;

		if (!@imagejpeg($new_img, $save_path.$file_name)) {
			HandleError("File could not be saved.");
			exit(0);
		}


	$file_id = $save_path.$file_name;
	echo "FILEID:" . $file_id.'#'.$file_name_original;	// Return the file id to the script

	function HandleError($message) {
	echo $message;
}
?>