<?php
function make_thumb($src, $dest, $desired_width) {
    
    $ext = pathinfo($src, PATHINFO_EXTENSION);
    if ($ext='jpg' OR $ext='JPG') {
   	/* read the source image */
	$source_image = imagecreatefromjpeg($src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	
	/* find the "desired height" of this thumbnail, relative to the desired width  */
	$desired_height = floor($height * ($desired_width / $width));
	
	/* create a new, "virtual" image */
	$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
	
	/* copy source image at a resized size */
	imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
	
	/* create the physical thumbnail image to its destination */
	imagejpeg($virtual_image, $dest);
}
    
        if ($ext=='jpeg' OR $ext=='JPEG') {
   	/* read the source image */
	$source_image = imagecreatefromjpeg($src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	
	/* find the "desired height" of this thumbnail, relative to the desired width  */
	$desired_height = floor($height * ($desired_width / $width));
	
	/* create a new, "virtual" image */
	$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
	
	/* copy source image at a resized size */
	imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
	
	/* create the physical thumbnail image to its destination */
	imagejpeg($virtual_image, $dest);
}
     if ($ext=='png' OR $ext=='PNG') {
   	/* read the source image */
	$source_image = imagecreatefrompng($src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	
	/* find the "desired height" of this thumbnail, relative to the desired width  */
	$desired_height = floor($height * ($desired_width / $width));
	
	/* create a new, "virtual" image */
	$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
	
	/* copy source image at a resized size */
	imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
	
	/* create the physical thumbnail image to its destination */
	imagejpng($virtual_image, $dest);
}
    
    
    
    
    
    }
// A list of permitted file extensions
$allowed = array('png', 'jpg','jpeg');


if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

	$extension = pathinfo('photos/'.$_POST['filedir'].'/'.$_FILES['upl']['name'], PATHINFO_EXTENSION);

	if(!in_array(strtolower($extension), $allowed)){
		echo '{"status":"error"}';
		exit;
	}

	//if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
    if(move_uploaded_file($_FILES['upl']['tmp_name'],  'photos/'.$_POST['filedir'].'/'.$_FILES['upl']['name'])){
        
   
    $src='photos/'.$_POST['filedir'].'/'.$_FILES['upl']['name'];
        
    $dest1='photos/'.$_POST['filedir'].'/tmb/'.$_FILES['upl']['name'];
        
        make_thumb($src, $dest1, 200);
        
        $dest2='photos/'.$_POST['filedir'].'/tmb2/'.$_FILES['upl']['name'];
        
        make_thumb($src, $dest2, 980);
        
        
        
        echo '{"status":"success"}';
		exit;
	}
}

echo '{"status":"error"}';
exit;