<?php
if(empty($_POST)){}
//  exit(header('Location: ../recorte_de_ft-jog/'));
else{
  sleep(1);
  $fileType = $_POST['imgType'];
  $imgName = $_POST['imgName'];

  define('OUTPUT', 'ft-jog/thumbnail-' . $imgName);
  if($fileType == 'image/png'){
    $img = imagecreatefrompng('../../ft-jog/' . $imgName);
  }
  else{
    $img = imagecreatefromjpeg('../../ft-jog/' . $imgName);
  }

  $imgWidth = imagesx($img);
  $imgHeight = imagesy($img);

  $newImage = imagecreatetruecolor(320,320);
  imagecopyresampled($newImage, $img, 0, 0, $_POST['x'], $_POST['y'], 320, 320, $_POST['w'], $_POST['h']);
  if($fileType == 'image/png')
    $finalImage = imagepng($newImage, '../../'.OUTPUT);
  else
    $finalImage = imagejpeg($newImage, '../../'.OUTPUT);

  if($finalImage) {
    echo '<img id="thumbnail" src="' . OUTPUT . '" /><input type="hidden" id="nome_imagem" name="nome_imagem" value="' . $imgName . '" />';
  }else{
    echo 'Ocorreu um erro ao tentar criar a nova imagem';
  }
}
?>