<?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
	@set_time_limit(0);
	@error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
	ini_set('html_errors', '0');

	define('SYSTEM_ROOT', str_replace("\\", '/',substr(dirname(__FILE__),0,-10)));
	include SYSTEM_ROOT.'include/common.inc.php';

	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 "err-1";
		exit(0);
	}

	if (!is_array(@getimagesize($_FILES["Filedata"]["tmp_name"])))
	{
		echo "err-2";
		exit(0);
	}

	if (!isset($_SESSION["file_info"])) {
		$_SESSION["file_info"] = array();
	}
	
	$fileName = $_userid.'_'.time().mt_rand(10000,99999).'.'.strtolower(get_fileext($_FILES["Filedata"]["name"]));
	$path=date('Y-m-d',time()).'/';
	@mkdir(SYSTEM_ROOT.'upload/image/'.$path);
	move_uploaded_file($_FILES["Filedata"]["tmp_name"], "../../upload/image/".$path. $fileName);

	// ผำหฎำก

	setwatermark(SYSTEM_ROOT.'upload/image/'.$path.$fileName);

	echo "FILEID:" . $path.$fileName;
	exit(0);
?>