<?php

/* Getting file name */
$filename = $_FILES['file']['name'];

/* Location */
$location = "upload/".$filename;
$uploadOk = 1;
$imageFileType = pathinfo($location,PATHINFO_EXTENSION);

$location  = "upload/".date('YmdHis-U').".".$imageFileType;
/* Valid Extensions */
$valid_extensions = array("jpg", "jpeg", "png", "gif");
/* Check file extension */
if( !in_array(strtolower($imageFileType),$valid_extensions) ) {
   $uploadOk = 0;
}

if($uploadOk == 0){
   echo 0;
}else{
   /* Upload file */
   if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
   		$images = $location;
   		$exif = exif_read_data($images);
		$new_images = "Resizeupload/".$location;
		$width=1000; //*** Fix Width & Heigh (Autu caculate) ***//
		$size=GetimageSize($images);
		$height=round($width*$size[1]/$size[0]);
		$images_orig = ImageCreateFromJPEG($images);
		$photoX = ImagesX($images_orig);
		$photoY = ImagesY($images_orig);
		$images_fin = ImageCreateTrueColor($width, $height);
		ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
		
		if (!empty($exif['Orientation'])) {
	        //$images_fin = imagecreatefromjpeg($images);
	       	switch ($exif['Orientation']) {
	    		case 3:$images_fin = imagerotate($images_fin, -180, 0);break;
	    		case 6:$images_fin = imagerotate($images_fin, -90, 0); break;
	    		case 8:$images_fin = imagerotate($images_fin, 90, 0);break;
			} 
		}

		$url_error = ImageJPEG($images_fin,$new_images);
		ImageDestroy($images_orig);
		ImageDestroy($images_fin);
		unlink($location);
      	echo $new_images;
   }else{
      echo 0;
      
   }
}
?>
<div id="url_ok"><?=$new_images?></div>
<?php if ($url_error) { ?>
	<div id="url_error">no_error</div>
<?php }else{ ?>
	<div id="url_error">error</div>
<?php } ?>