<?php 
function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    if ($newheight > 400 || $newwidth > 400) {
      $font_size = 18;
      $black = imagecolorallocate($dst, 210,210,210);
      $font = "/home/itemir/ilknurgurcan.com/media.ilknurgurcan.com/DejaVuSansMono.ttf";
      $text = "www.ilknurgurcan.com";

      ImageTTFText ($dst, $font_size, 0, round($newwidth / 2)-145, $newheight-40, $black, $font,$text);
    }

    return $dst;
}

$base = "/home/itemir/ilknurgurcan.com/media.ilknurgurcan.com/paintings";

$file = $_GET['f'];
$size = $_GET['s'];

$file = $base."/".preg_replace("/[^a-zA-Z0-9\.\-_\ ]/", "", $file);
$size = intval ($size);

if ($file && $size && file_exists ($file) && $size <= 1024) {
  $img = resize_image ($file, $size, $size);

  header("Content-Type: image/jpeg");
  imagejpeg($img);
  imagedestroy($img);
} 
else {
  echo "Not found";
}
?>
