<?php
function make_thumb($src, $dest, $desired_width) {
     $ext = pathinfo($src, PATHINFO_EXTENSION);
    if ($ext='pdf' OR $ext='PDF') {
   $compressionList = [Imagick::COMPRESSION_UNDEFINED,
                    Imagick::COMPRESSION_BZIP,
                    Imagick::COMPRESSION_LZW,
                    Imagick::COMPRESSION_RLE,
                    Imagick::COMPRESSION_ZIP];
    
    $image = new Imagick($src."[0]");

$image->setImageColorspace(255); // prevent image colors from inverting
$image->setimageformat("png");
$image->setCompression($compressionList[2]);
$image->setImageCompressionQuality(50);
        // Si 0 est fourni comme paramètre de hauteur ou de largeur,
// les proportions seront conservées
$image->thumbnailImage($desired_width, 0);

$image->writeImage($dest);

//echo $image;
$image->clear();
$image->destroy();
    
    
   
}
    
      
  
    
    
    
    
    }
// A list of permitted file extensions
$allowed = array('pdf', 'PDF');


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

	$extension = pathinfo('photos/'.$_POST['filedir'].'/doc/'.$_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'].'/doc/'.$_FILES['upl']['name'])){
        
   
    $src='photos/'.$_POST['filedir'].'/doc/'.$_FILES['upl']['name'];
        
   
       
        $dest1='photos/'.$_POST['filedir'].'/tmb/'.$_FILES['upl']['name'];
        $dest1=  preg_replace('"\.pdf$"', '.png',  $dest1);
        make_thumb($src, $dest1, 200);
        
        $dest2='photos/'.$_POST['filedir'].'/tmb2/'.$_FILES['upl']['name'];
        $dest2=  preg_replace('"\.pdf$"', '.png',  $dest2);
        make_thumb($src, $dest2, 490);
        $dest2='photos/'.$_POST['filedir'].'/'.$_FILES['upl']['name'];
        $dest2=  preg_replace('"\.pdf$"', '.png',  $dest2);
        make_thumb($src, $dest2, 490);
        
        echo '{"status":"success"}';
		exit;
	}
}

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