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

if(isSet($_GET['filtr'])){
  $filtr = $_GET['filtr'];
  switch($filtr){
    case 1 : $filterType = IMG_FILTER_NEGATE;break;
    case 2 : $filterType = IMG_FILTER_GRAYSCALE;break;
    case 3 : $filterType = IMG_FILTER_EDGEDETECT;break;
    case 4 : $filterType = IMG_FILTER_GAUSSIAN_BLUR;break;
    default : die("error\nNieprawidłowe dane.");
  }

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

  if(!imagefilter ($img, $filterType)){
    die("error\nFiltr nie może zostać zastosowany.");
  }
  $imgName = microtime().rand();
  imagejpeg($img, $tempDir.$imgName);
  imagedestroy($img);
  echo $imgName;
}
else{
  if(!($img = imagecreatefromjpeg($imgName))){
    exit();
  }
  header('Content-Type: image/jpeg');
  imagejpeg($img);
  imagedestroy($img);
}
?>