Keep aspect ratio when generating local thumbnails

This commit is contained in:
Brion Vibber 2010-11-08 17:51:53 -08:00
parent 694448e0aa
commit 504529e8cd

View File

@ -127,8 +127,9 @@ class MediaFile
$outname = File::filename($this->user->getProfile(), 'thumb-' . $this->filename, $this->mimetype); $outname = File::filename($this->user->getProfile(), 'thumb-' . $this->filename, $this->mimetype);
$outpath = File::path($outname); $outpath = File::path($outname);
$width = common_config('attachments', 'thumb_width'); $maxWidth = common_config('attachments', 'thumb_width');
$height = common_config('attachments', 'thumb_height'); $maxHeight = common_config('attachments', 'thumb_height');
list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight);
$image->resizeTo($outpath, $width, $height); $image->resizeTo($outpath, $width, $height);
File_thumbnail::saveThumbnail($this->fileRecord->id, File_thumbnail::saveThumbnail($this->fileRecord->id,
@ -137,6 +138,19 @@ class MediaFile
$height); $height);
} }
function scaleToFit($width, $height, $maxWidth, $maxHeight)
{
$aspect = $maxWidth / $maxHeight;
$w1 = $maxWidth;
$h1 = intval($height * $maxWidth / $width);
if ($h1 > $maxHeight) {
$w2 = intval($width * $maxHeight / $height);
$h2 = $maxHeight;
return array($w2, $h2);
}
return array($w1, $h1);
}
function rememberFile($file, $short) function rememberFile($file, $short)
{ {
$this->maybeAddRedir($file->id, $short); $this->maybeAddRedir($file->id, $short);