list($base_width, $base_height) = getimagesize($filename);
	$ratioh = $req_height/$base_height;
	$ratiow = $req_width/$base_width;
	$ratio = max($ratioh, $ratiow);
	
	// New dimensions
	$new_width = intval($ratio*$base_width);
	$new_height = intval($ratio*$base_height); 
	
	//Offset required
	$offset_width = ($new_width - $req_width) / 2;
	$offset_height = ($new_height - $req_height) / 2;
	
	$canvas = imagecreatetruecolor($req_width, $req_height);
	imagecopyresampled($canvas, $img, 0, 0, $offset_width, $offset_height, $new_width, $new_height, $base_width, $base_height);
	
	return $canvas;
Some code i was just working on 
