<?php
include "../../db_function.php";  

error_reporting(0);
$change="";
$abc="";
 define ("MAX_SIZE","1024");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 	$image =$_FILES["uploadfile"]["name"];
	$uploadedfile = $_FILES['uploadfile']['tmp_name'];
	 ///Rename Image
	 if(!empty($image)){
				$i=1;
				$str="SELECT * FROM tbl_moreimages WHERE image ='$image' ";
				$sql=mysql_query($str);
				while ($row=mysql_fetch_array($sql)){
				$str="SELECT * FROM tbl_moreimages WHERE image ='$image' ";
				$sql=mysql_query($str);
				$num_rows = mysql_num_rows($sql);
					 if($num_rows > 0) {
						$nimage=$_FILES["uploadfile"]["name"]; 
						list ($iname,$ext)=preg_split('/[\.,]+/',$nimage);
						$image=$iname."_".$i.".".$ext;
					 }
					 
					$i++;
				}
			///End Rename/////////////////////////////////////////////////////////
	}
 	if ($image) 
 	{
 		$filename = stripslashes($image);
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
		
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
 			$change='<div class="msgdiv">Unknown Image extension </div> ';
 			$errors=1;
 		}
 		else
 		{

 $size=filesize($_FILES['uploadfile']['tmp_name']);
if ($size > MAX_SIZE*1440)
{
	$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
	$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);}
else if($extension=="png")
{
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else 
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);

		$newwidth=660;
		$newheight=($height/$width)*$newwidth;
		$tmp=imagecreatetruecolor($newwidth,$newheight);

		//$newheight1=45;
		//$newwidth1=($width/$height)*$newheight1;
		//$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

		//imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);


		$filename = "../../Site/images/more_images/".$image;

		//$filename1 = "../../Site/images/imageRoomMore/".$image;



if(imagejpeg($tmp,$filename,80)){
	$aid=$_GET['aid'];
	$query="INSERT INTO tbl_moreimages(aid,image) values('$aid','$image')";
	mysql_query($query);
	
  echo "success"; 
} else {
	echo "error";
}

imagedestroy($src);
imagedestroy($tmp);
//imagedestroy($tmp1);
}}

}
?>