<?php
// Part 2: Using your knowledge of conditionals, add the ability to thumbnail pngs and gifs to the thumbnail script we did this week

//step1 figure out the image type
$pic = $_GET['pic'];
$max_width = $_GET['wd'];
$max_height = $_GET['ht'];
/* 1=>'GIF',
        2=>'JPEG',
        3=>'PNG',
*/        
$size = getimagesize($pic);
//print_r($size);
///Array ( [0] => 600 [1] => 450 [2] => 1 [3] => width="600" height="450" [bits] => 8 [channels] => 3 [mime] => image/gif ) 
//let's say $max_width is 200 (from url window)
//let's say $max_height is 200 (from url window)

$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;
}

$width = $size[0];
$height = $size[1];
$new_width=$size[0]/$ratio;
$new_height=$size[1]/$ratio;
//this is a gif!
if ($size[2] == 1){
   header ("Content-type: image/gif");

$src_img = imagecreatefromgif($pic);
$thumb = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumb,$src_img,0,0,0,0,$new_width,$new_height,$size[0],$size[1]);
imagegif($thumb);   
}
//this is a jpeg!
if ($size[2] == 2){
    header ("Content-type: image/jpeg");

    $src_img = imagecreatefromjpeg($pic);
$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){
    //this is a png!

    header ("Content-type: image/png");
$src_img = imagecreatefrompng($pic);
$thumb = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($thumb,$src_img,0,0,0,0,$new_width,$new_height,$size[0],$size[1]);
imagepng($thumb); 
}
?>