<?php
session_start();
$nazwa = $_SESSION['nazwa'];
$ds = DIRECTORY_SEPARATOR; 
$storeFolder = 'upload/'.$_SESSION['nazwa'].'/galeria-1/duze/';
$katalog_m = 'upload/'.$_SESSION['nazwa'].'/galeria-1/male/';
$katalog_d = 'upload/'.$_SESSION['nazwa'].'/galeria-1/duze/';  
	if (!empty($_FILES)) {
		$tempFile = $_FILES['file']['tmp_name'];         
		$targetPath = dirname( __FILE__ ).$ds.$storeFolder.$ds; 
		$targetFile =  $targetPath.$_FILES['file']['name']; 
		//echo $targetFile.'<br>';
		
		$m_width = 126;
		$m_height = 80;
		$d_width = 1260;
		$d_height = 800;
		
		$nf = resizeImage($_FILES['file']['name'], $d_width, $d_height);
		imagepng($nf, $katalog_d.'d_'.$_FILES['file']['name']);
		
		$nf = resizeImage($_FILES['file']['name'], $m_width, $m_height);
		imagepng($nf, $katalog_m.'m_'.$_FILES['file']['name']);
		
		} else {                                                           
			$result  = array();
			$files = scandir($storeFolder); 
			if ( false!==$files ) {
				foreach ( $files as $file ) {
					if ( '.'!=$file && '..'!=$file) {   
						$obj['name'] = $file;
						$obj['size'] = filesize($storeFolder.$ds.$file);
						$result[] = $obj;
					}
				}
			}		 
			header('Content-type: text/json');    
			header('Content-type: application/json');
			echo json_encode($result);
		}
	
	//------------------------------------------------
	function resizeImage($filename, $max_width, $max_height) {
		list($orig_width, $orig_height) = getimagesize($_FILES["file"]["tmp_name"]);

		$width = $orig_width;
		$height = $orig_height;

		if ($height > $max_height) {
			$width = ($max_height / $height) * $width;
			$height = $max_height;
		}

		if ($width > $max_width) {
			$height = ($max_width / $width) * $height;
			$width = $max_width;
		}

		$image_p = imagecreatetruecolor($width, $height);
		$image = imagecreatefromjpeg($_FILES["file"]["tmp_name"]);
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, 
						   $width, $height, $orig_width, $orig_height);
		return $image_p;
}
		
?>  