<?php
include "../../db_function.php";  
$table='tbl_imagegallery';	
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 $table WHERE item ='$image' ";
		$sql=mysql_query($str);
		while ($row=mysql_fetch_array($sql)){
		$str="SELECT * FROM $table WHERE item ='$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);

		if($width > 800 ){
			$newwidth = 800;
		}else{
			$newwidth = $width;
		}
		$newheight=($height/$width)*$newwidth;
		$tmp=imagecreatetruecolor($newwidth,$newheight);

		
		$newheight1 = 139;
		$newwidth1=($width/$height)*$newheight1;
		$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
		
		$newheight2 = 50;
		$newwidth2=($width/$height)*$newheight2;
		$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
		
		if($extension=="png" || $extension=="gif")
		{
			imagealphablending($tmp, false);
			imagesavealpha($tmp,true);
			$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
			imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
			
			imagealphablending($tmp1, false);
			imagesavealpha($tmp1,true);
			$transparent = imagecolorallocatealpha($tmp1, 255, 255, 255, 127);
			imagefilledrectangle($tmp1, 0, 0, $newwidth, $newheight, $transparent);
			
			imagealphablending($tmp2, false);
			imagesavealpha($tmp2,true);
			$transparent = imagecolorallocatealpha($tmp1, 255, 255, 255, 127);
			imagefilledrectangle($tmp2, 0, 0, $newwidth, $newheight, $transparent);
		}
		
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

		imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
		
		imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);


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

		$filename1 = "../../Site/images/gallery_th/".$image;
		
		$filename2 = "../../Site/images/gallery_sth/".$image;

		if($extension=="png" || $extension=="gif")
		{
			imagepng($tmp,$filename);
			imagepng($tmp1,$filename1);
			imagepng($tmp2,$filename2);
		}
		else{
			imagejpeg($tmp,$filename,90);
			imagejpeg($tmp1,$filename1,90);
			imagejpeg($tmp2,$filename2,90);
		}


	$aid=$_GET['aid'];
	$type=0;
	$query="INSERT INTO $table values (0 , '$type' ,'$aid', '$image')";
	$suc=mysql_query($query);
	if($suc){
  echo "success"; 
} else {
	echo 'error';
}

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

?>