<?php
class Image{
	function uploadFile($dir, $file, $imgsize) //이미지 사이즈 인자값 추가 (함수 호출 부분에서도)
	{
		$unique_name = uniqid().'_'.$file["name"];
		$uploadOk = 1; 
		$isUploaded = false;

		if(getimagesize($file["tmp_name"]) == false) {
			echo "File is not an image.";
			$uploadOk = 0;
		}
		if($uploadOk == 0) {
			echo "Sorry, your file was not uploaded.";
		}else{
			$img = new SimpleImage();
			$img->load($file["tmp_name"]);
			
			$img->resize(600, 600);
			$img->save($dir.'large_'.$unique_name);
			
			$img->resize(300, 300);
			$img->save($dir.'medium_'.$unique_name);
			
			$img->resize(80, 80);
			$img->save($dir.'small_'.$unique_name);

			$result[success] = true;
			$result[org_name] = basename($file["name"]);
			$result[unique_name] = 'medium_'.$unique_name;
			
			// else 
			// {
				// $result[success] = false;
				// echo "upload fail";
			// }
			return $result;
		}
		
		// $unique_name = uniqid().'_'.$file[name];
		// $uploadOk = 1; 
		// $isUploaded = false;

		// if(getimagesize($file["tmp_name"]) == false) {
			// echo "File is not an image.";
			// $uploadOk = 0;
		// }
		// if($uploadOk == 0) {
			// echo "Sorry, your file was not uploaded.";
		// }else{
			
			// $tempImage = $file["tmp_name"];
			// list($width, $height, $type) = getimagesize($tempImage);

			// switch($type)
			// {
				// case IMAGETYPE_PNG: $image = imagecreatefrompng($tempImage);    break;
				// case IMAGETYPE_GIF: $image = imagecreatefromgif($tempImage);    break;
				// case IMAGETYPE_JPEG:$image = imagecreatefromjpeg($tempImage);   break;
				// default: break;
			// }

			// $createdImage = imagecreatetruecolor(600, 600);
			// imagecopyresampled($createdImage, $image, 0,0,0,0, 600, 600, $width, $height);
			// imagejpeg($createdImage, $tempImage, 100);
			// $isUploaded = move_uploaded_file($tempImage, $dir.'large_'.$unique_name);
			
			// if($isUploaded)
			// {
				// $createdImage = imagecreatetruecolor(300, 300);
				// imagecopyresampled($createdImage, $image, 0,0,0,0, 300, 300, $width, $height);
				// imagejpeg($createdImage, $tempImage, 100);
				// echo $tempImage;
				// $isUploaded = move_uploaded_file($tempImage, $dir.'medium_'.$unique_name);
				// echo '>>>>>';
				// echo $isUploaded;
				// echo '<<<<<';
				
				// if($isUploaded)
				// {
					// echo 'dddddddddddddddddddddd';
					// $createdImage = imagecreatetruecolor(80, 80);
					// imagecopyresampled($createdImage, $image, 0,0,0,0, 80, 80, $width, $height);
					// imagejpeg($createdImage, $tempImage, 100);
					// $isUploaded = move_uploaded_file($tempImage, $dir.'small_'.$unique_name);
				// }
			// }
			
			// if($isUploaded)
			// {
				// $result[success] = true;
				// $result[org_name] = basename($file["name"]);
				// $result[unique_name] = 'medium_'.$unique_name;
			// }
			// else 
			// {
				// $result[success] = false;
				// echo "upload fail";
			// }
			// return $result;
		// }
	}
}

/* 이미지 크기별로 불러오기 */
$app->action('img/resize', function() use($auth){
	$a = $_REQUEST;
	
	echo $_FILES[0]."\n";
	echo $a[filename]."\n";
	echo json_encode($a);
	echo "\n\n";
	if($a[filename])
	{
		$orgfn = '../../roseimg/playlist_cover/'.$a[filename];
		$newfn = '../../roseimg/playlist_cover/'.$a[size].'_'.$a[filename];
	}
	// else if($_FILES[0]["newfile"])
	// {
		// echo "2222\n";
		// $orgfn = $_FILES["newfile"];
		// $filename = uniqid().'_'.$orgfn[name];
		// $newfn = '../../roseimg/playlist_cover/'.$a[size].'_'.$filename;
	// }
	
	switch($a[size])
	{
		case "large":	$size = 600;	break;
		case "medium":	$size = 300;	break;
		case "small":	$size = 80;		break;
	}
	
	$img = new SimpleImage();
	$img->load($orgfn);
	$img->resize($size, $size);
	$img->save($newfn);
	header('Content-Type: image/jpeg');
	readfile($newfn);
});
?>