<?php
$filename = basename($_SERVER["REQUEST_URI"]);
$dir = $_SERVER["DOCUMENT_ROOT"] . dirname($_SERVER["REQUEST_URI"]);
$origfile = realpath($dir . '/../' . $filename);
$newfile = $dir . '/' . $filename;
$nw = 100;
$nh = 80;
if($origfile){
list($width,$height,$otype)=getimagesize($origfile);
switch($otype) {
case 1: $img = imagecreatefromgif($origfile); break;
case 2: $img = imagecreatefromjpeg($origfile); break;
case 3: $img = imagecreatefrompng($origfile); break;
}
if($img){
$scale1 = $scale2 = 99999999;
if($nw) $scale1 = $nw / $height;
if($nh) $scale2 = $nh / $width;
$scale = min($scale1, $scale2);
$new_width = floor($width * $scale);
$new_height = floor($height * $scale);
$newimg = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($newimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
switch($otype) {
case 1: imagegif($newimg, $newfile); header('Content-Type: image/gif'); imagegif($newimg); break;
case 2: imagejpeg($newimg, $newfile); header('Content-Type: image/jpeg'); imagejpeg($newimg); break;
case 3: imagepng($newimg, $newfile); header('Content-Type: image/png'); imagepng($newimg);break;
}
}
}
header('Content-Type: image/gif');
$noimg = $dir . '/noimg.gif';
$handle = fopen($noimg, "r");
print(fread($handle, filesize($noimg)));
fclose($handle);
?>