<?php
error_reporting(E_ALL);
$img_name = $_GET['pic'];
$max_width = $_GET['wd'];
$max_height = $_GET['ht'];
//this gets an array of information pertaining to the pic!!!!
$size = getimagesize($img_name);

$width_ratio = ($size[0]/$max_width);
$height_ratio = ($size[1]/$max_height);
if ($width_ratio >= $height_ratio){
    $ratio = $width_ratio;
}
if ($height_ratio >= $width_ratio){
    $ratio = $height_ratio;
}
$new_width = $size[0]/$ratio;
$new_height = $size[1]/$ratio;

if ($size[2] == 1){
//headers commit the page to be gif    
header ("Content-type: image/gif");
$src_img = imagecreatefromgif($img_name); 
$exif = @exif_read_data($img_name);
if ($exif){
switch ($exif['Orientation']) {
    case 3:
        $src_img = imagerotate($src_img, 180, 0);
        break;
    case 6:
        $src_img = imagerotate($src_img, -90, 0);
        break;
    case 8:
        $src_img = imagerotate($src_img, 90, 0);
        break;
} 
}
$thumb = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumb,$src_img,0,0,0,0,$new_width,$new_height,$size[0],$size[1]);

imagegif($thumb);
}

if ($size[2] == 2){
 header ("Content-type: image/jpeg");
$src_img = imagecreatefromjpeg($img_name); 
$exif = @exif_read_data($img_name);
if ($exif){
switch ($exif['Orientation']) {
    case 3:
        $src_img = imagerotate($src_img, 180, 0);
        break;
    case 6:
        $src_img = imagerotate($src_img, -90, 0);
        break;
    case 8:
        $src_img = imagerotate($src_img, 90, 0);
        break;
} 
}
$thumb = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumb,$src_img,0,0,0,0,$new_width,$new_height,$size[0],$size[1]);

    imagejpeg($thumb);
    
    
}

if ($size[2] == 3){
header ("Content-type: image/png");
$src_img = imagecreatefromgif($img_name); 
$exif = @exif_read_data($img_name);
if ($exif){
switch ($exif['Orientation']) {
    case 3:
        $src_img = imagerotate($src_img, 180, 0);
        break;
    case 6:
        $src_img = imagerotate($src_img, -90, 0);
        break;
    case 8:
        $src_img = imagerotate($src_img, 90, 0);
        break;
} 
}
$thumb = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumb,$src_img,0,0,0,0,$new_width,$new_height,$size[0],$size[1]);  
    imagepng($thumb);
}




?>