Canvas Padded thumbnails using PHP and GD in Zend Framework Application

function showThumbnail($imageFileName='', $width='', $height='', $cwidth='', $cheight='')
{
	$cfg = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', 'production');
	$path = $cfg->resources->frontController->imageupload->path;
	$publicpath = $cfg->resources->frontController->imageupload->publicpath;

	$pi = pathinfo($imageFileName);
	$filename = $pi['filename'];
	$ext = $pi['extension'];

	$thumbname = $filename.'-'.$width.'X'.$height.'.'.$ext;

	$fspath = APPLICATION_PATH.'/..'.$path.'/'.$imageFileName;
	$fspathThumb = APPLICATION_PATH.'/..'.$path.'/'.$thumbname;

	if(!file_exists($fspathThumb)) {
		thumb($fspath, $width, $height, $cwidth, $cheight, $fspathThumb);
	}

	return $publicpath.'/'.$thumbname;
}

function thumb($img, $w, $h, $cw='', $ch='', $thumb, $fill='') {
	if (!extension_loaded('gd') && !extension_loaded('gd2')) {
		trigger_error("No dispones de la libreria GD para generar la imagen.", E_USER_WARNING);
		return false;
	}

	$imgInfo = getimagesize($img);
	switch ($imgInfo[2]) {
		case 1: $im = imagecreatefromgif($img); break;
		case 2: $im = imagecreatefromjpeg($img);  break;
		case 3: $im = imagecreatefrompng($img); break;
		default:  trigger_error('Tipo de imagen no reconocido.', E_USER_WARNING);  break;
	}

	if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
		$nHeight = $imgInfo[1];
		$nWidth = $imgInfo[0];
	}else{
		if ($w/$imgInfo[0] < $h/$imgInfo[1]) {
			$nWidth = $w;
			$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
			$ny = ($h - $nHeight)/2;
		}else{
			$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
			$nHeight = $h;
			$nx = ($w - $nWidth)/2;
		}
	}

	$nWidth = round($nWidth);
	$nHeight = round($nHeight);

	$newImg = imagecreatetruecolor($nWidth, $nHeight);
	$newImg = imagecreatetruecolor($w, $h);
	$fillColor = imagecolorallocate($newImg, 0xFF, 0xFF, 0xFF);
	imagefill($newImg, 0, 0, $fillColor);

	imagecopyresampled($newImg, $im, $nx, $ny, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

	switch ($imgInfo[2]) {
		case 1: imagegif($newImg , $thumb); break;
		case 2: imagejpeg($newImg, $thumb);  break;
		case 3: imagepng($newImg, $thumb); break;
		default:  trigger_error('Imposible mostrar la imagen.', E_USER_WARNING);  break;
	}

	imagedestroy($newImg);
}