<?php
function uploadtheimg($imgsrc, $thediry, $i_h, $i_w, $thenewbase) {


	$image_info = getImageSize($imgsrc);
	
	//Determine the new width and height for the image
	$image_old_type = $image_info[2];
	$image_old_w = $image_info[0];
	$image_old_h = $image_info[1];
	
	if (($image_old_h > $i_h) || ($image_old_w > $i_w))
	{
	  $d_h = $i_h / $image_old_h;
	  $d_w = $i_w / $image_old_w;
	  
	  if ($d_h < $d_w)
	  {
	    $d_c = $d_h;
	  }
	  else
	  {
	    $d_c = $d_w;
	  }
	}
	else
	{
	  $d_c = 1;
	}
	
	$image_new_h = $image_old_h * $d_c;
	$image_new_w = $image_old_w * $d_c;

	switch ($image_old_type)
	{
	  case 1:
	    $content_type = "image/gif";
		//we cannot modify a gif image.
	    break;
		
	  case 2:
	    $content_type = "image/jpeg";
		$image_old_handle = imageCreateFromJPEG($imgsrc);
		//$image_old_handle = imageCreate($image_old_w, $image_old_h);
		//$image_old_handle = imageCreateTrueColor($imgsrc);
	    break;
		
	  case 3:
	    $content_type = "image/jpeg";
		$image_old_handle = imageCreateFromPNG($imgsrc);
		//$image_old_handle = imageCreate($image_old_w, $image_old_h);	
		//$image_old_handle = imageCreateTrueColor($imgsrc);
		break;
		
	  default:
	    $content_type = "image";
		//we cannot modify an unknown image.
	}

	if (($image_old_type == 2) || ($image_old_type == 3))
	{
		  $image_new_handle = imageCreateTrueColor($image_new_w, $image_new_h);
		  imageCopyResampled($image_new_handle, $image_old_handle, 0, 0, 0, 0, $image_new_w, $image_new_h, $image_old_w, $image_old_h);
		  imageJPEG($image_new_handle, $thediry . $thenewbase);
	} else {
		copy($imgsrc, $thediry . $thenewbase);
	}

    return basename($thediry . $thenewbase);


}

?>