does anyone happen to have a function using GD to create thumbnails maintaining their proportions while being padded to a fixed resolution?
Here is the only function I have, it resizes the image to a specified size while maintaining the aspect ratio. But the way it does it, is by cropping the larger of the 2 dimensions. What I'm looking for is something that won't crop, but apply padding instead.
<?
function makeThumb($file)
{
$dstWidth = 80;
$dstHeight = 60;
list(, , $type, ) = getimagesize($file);
//1 = GIF, 2 = JPG, 3 = PNG
if ($type == 2)
$handle = @imagecreatefromjpeg($file);
elseif ($type == 3)
$handle = @imagecreatefrompng($file);
elseif ($type == 1)
$handle = @imagecreatefromgif($file);
else
return false;
if (!$handle)
return false;
$srcWidth = @imagesx($handle);
$srcHeight = @imagesy($handle);
if ($srcWidth >= $dstWidth && $srcHeight >= $dstHeight) {
$newHandle = @imagecreatetruecolor($dstWidth, $dstHeight);
if (!$newHandle)
return false;
if($srcHeight < $srcWidth) {
$ratio = (double)($srcHeight / $dstHeight);
$cpyWidth = round($dstWidth * $ratio);
if ($cpyWidth > $srcWidth) {
$ratio = (double)($srcWidth / $dstWidth);
$cpyWidth = $srcWidth;
$cpyHeight = round($dstHeight * $ratio);
$xOffset = 0;
$yOffset = round(($srcHeight - $cpyHeight) / 2);
}
else {
$cpyHeight = $srcHeight;
$xOffset = round(($srcWidth - $cpyWidth) / 2);
$yOffset = 0;
}
}
else {
$ratio = (double)($srcWidth / $dstWidth);
$cpyHeight = round($dstHeight * $ratio);
if ($cpyHeight > $srcHeight) {
$ratio = (double)($srcHeight / $dstHeight);
$cpyHeight = $srcHeight;
$cpyWidth = round($dstWidth * $ratio);
$xOffset = round(($srcWidth - $cpyWidth) / 2);
$yOffset = 0;
}
else {
$cpyWidth = $srcWidth;
$xOffset = 0;
$yOffset = round(($srcHeight - $cpyHeight) / 2);
}
}
if (!@imagecopyresampled($newHandle, $handle, 0, 0, $xOffset, $yOffset, $dstWidth, $dstHeight, $cpyWidth, $cpyHeight))
return false;
@imagedestroy($handle);
if ($type == 3)
@imagepng($newHandle, 'tn_' . $file);
elseif ($type == 2)
@imagejpeg($newHandle, 'tn_' . $file, 90);
elseif ($type == 1)
@imagegif($newHandle, 'tn_' . $file);
else
return false;
@imagedestroy($newHandle);
return true;
}
else {
die('<meta HTTP-EQUIV="REFRESH" content="2; url=admin.php"> <b>Sorry, that image is too small. The image must be at least ' . $dstWidth . 'x' . $dstHeight . ' pixels in size.</b>');
}
}
?>
By the way, is it my imagination or when this board came back up from last night maintenance, they switched forum scripts from phpbb to vbulletin.