<?php 
	include('code.php'); 
	error_reporting(0);

	$total = count($_FILES['file']['name']);
	$uploadOk = 0;
	$NO_error = 0;
	$base64_encode_error = 0;
	/* Getting file name */
	for ($count_file=0; $count_file < $total ; $count_file++) { 
		# code...
		$filename = $_FILES['file']['name'][$count_file];
		/* Location */
		$location = "upload/".$filename;
		$imageFileType = pathinfo($location,PATHINFO_EXTENSION);
		$location  = "upload/".date('YmdHis-U')."-".($count_file+1).".".$imageFileType;
		/* Valid Extensions */
		$valid_extensions = array("jpg", "jpeg", "png", "gif");
		/* Check file extension */
		if( in_array(strtolower($imageFileType),$valid_extensions) ) {
			$uploadOk = 1;
		}else{
			$uploadOk = 0;
		}
		if($uploadOk == 0){
			$uploadOk = 0;
		}else{
			/* Upload file */
			if(move_uploaded_file($_FILES['file']['tmp_name'][$count_file],'../'.$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);
				if($data_base64_encode = base64_encode(file_get_contents('../'.$new_images))){
					$data_base64_encode = "data:image/".$imageFileType.";base64,".$data_base64_encode;
					unlink('../'.$new_images);
				}else{
					$base64_encode_error++;
				}
				//echo '<div id="url_ok_'.($count_file+1).'">'.$new_images.'</div>';
				echo '<textarea id="url_ok_'.($count_file+1).'">'.$data_base64_encode.'</textarea>';
				$uploadOk = 1;
				$NO_error++;
			}else{
				$uploadOk = 0;
			}
		}
	}	

	if ( $uploadOk=='1'&&$total==$NO_error&&$total<=3&&$base64_encode_error==0) { 
		if($total == "1"){
			echo '<textarea id="url_ok_2">no_img</textarea>';
			echo '<textarea id="url_ok_3">no_img</textarea>';
		}else if($total == "2"){
			echo '<textarea id="url_ok_3">no_img</textarea>';
		}
		echo '<div id="url_error">no_error</div>';
	}else{ 
		echo '<textarea id="url_ok_1">no_img</textarea>';
		echo '<textarea id="url_ok_2">no_img</textarea>';
		echo '<textarea id="url_ok_3">no_img</textarea>';
		echo '<div id="url_error">error</div>';
	} 
?>

