<?
$img_name = $_GET['pic'];
$max_width    = $_GET['wd']; 
$max_height   = $_GET['ht']; 

$size= getimagesize($img_name);//gets height and width into an array!!
$type = $size['mime'];
header("Content-Type: $type");

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

if ($type == 'image/jpeg'){
// Create a new image from file or URL
$src_img = imagecreatefromjpeg($img_name);
//imagecreatetruecolor() returns an image identifier representing a black image of size x_size  by y_size.
$thumb = imagecreatetruecolor($new_width,$new_height);
/*imagecopyresampled() copies a rectangular portion of one image to another image,
 smoothly interpolating pixel values so that, in particular,
  reducing the size of an image still retains a great deal of clarity.
  */

imagecopyresampled($thumb, $src_img, 0,0,0,0,$new_width,$new_height,$size[0],$size[1]);//Output image to browser or file
imagejpeg($thumb,'',100);
}

if ($type == 'image/png'){

// Create a new image from file or URL
//imagecreatetruecolor() returns an image identifier representing a black image of size x_size  by y_size.
$thumb = imagecreatetruecolor($new_width,$new_height);
imagealphablending($thumb,false);

$src_img = imagecreatefrompng($img_name);
imagealphablending($src_img,true);

imagecopyresized($thumb, $src_img, 0,0,0,0,$new_width,$new_height,$size[0],$size[1]);//Output image to browser or file
imagepng($thumb);
}


if ($type == 'image/gif'){
// Create a new image from file or URL
$src_img = imagecreatefromgif($img_name);
//imagecreatetruecolor() returns an image identifier representing a black image of size x_size  by y_size.
$thumb = imagecreatetruecolor($new_width,$new_height);
/*imagecopyresampled() copies a rectangular portion of one image to another image,
 smoothly interpolating pixel values so that, in particular,
  reducing the size of an image still retains a great deal of clarity.
  */

imagecopyresampled($thumb, $src_img, 0,0,0,0,$new_width,$new_height,$size[0],$size[1]);//Output image to browser or file
imagegif($thumb,'',100);
}
imagedestroy($src_img);
imagedestroy($thumb);
?> 
