<?php
$imgName = "obraz1.jpg";
$tempDir = "./temp/";

if(isSet($_GET['sx']) && isSet($_GET['sy'])){
  $sx = $_GET['sx'];
  $sy = $_GET['sy'];

  if(!is_numeric($sx) || !is_numeric($sy) ||
     $sx < 1 || $sy < 1){
    die("error\nNieprawidłowe dane.");
  }

  if(!($img = imagecreatefromjpeg($imgName))){
    die("error\nBrak dostępu do obrazu.");
  }

  $sW = imagesx($img);
  $sH = imagesy($img);

  $width = $sW * $sx / 100;
  $height = $sH * $sy / 100;

  $tempImg = imagecreatetruecolor($width, $height);
  imagecopyresampled($tempImg, $img, 0, 0, 0, 0, $width, $height, $sW, $sH);

  $imgName = microtime().rand();
  imagejpeg($tempImg, $tempDir.$imgName);
  imagedestroy($tempImg);
  imagedestroy($img);
  echo $imgName;
}
else{
  if(!($img = imagecreatefromjpeg($imgName))){
    exit();
  }
  header('Content-Type: image/jpeg');
  imagejpeg($img);
  imagedestroy($img);
}
?>