diff options
author | Brion Vibber <brion@status.net> | 2010-11-08 17:51:53 -0800 |
---|---|---|
committer | Brion Vibber <brion@status.net> | 2010-11-08 17:51:53 -0800 |
commit | 504529e8cd8fbaf5e8e1b980260d1d87d9e880ac (patch) | |
tree | 2bedd9f3d703e80c9f6c9383ab15d81d72ab18d2 | |
parent | 694448e0aa81edb7b010f102ee9ee0e6961f6f7c (diff) |
Keep aspect ratio when generating local thumbnails
-rw-r--r-- | lib/mediafile.php | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/mediafile.php b/lib/mediafile.php index febf4603a..a41d7c76b 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -127,8 +127,9 @@ class MediaFile $outname = File::filename($this->user->getProfile(), 'thumb-' . $this->filename, $this->mimetype); $outpath = File::path($outname); - $width = common_config('attachments', 'thumb_width'); - $height = common_config('attachments', 'thumb_height'); + $maxWidth = common_config('attachments', 'thumb_width'); + $maxHeight = common_config('attachments', 'thumb_height'); + list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight); $image->resizeTo($outpath, $width, $height); File_thumbnail::saveThumbnail($this->fileRecord->id, @@ -137,6 +138,19 @@ class MediaFile $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) { $this->maybeAddRedir($file->id, $short); |