From 6799052ff52aab274fcd56634b4c63323fae3d26 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 18 Aug 2021 13:08:15 +0100 Subject: [PATCH] [ATTACHMENTS] Ensure thumbnail dimensions are bounded and change way cropping is implemented --- plugins/ImageEncoder/ImageEncoder.php | 11 ++++++----- src/Controller/Attachment.php | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/plugins/ImageEncoder/ImageEncoder.php b/plugins/ImageEncoder/ImageEncoder.php index 3179589fe4..3b13979c1b 100644 --- a/plugins/ImageEncoder/ImageEncoder.php +++ b/plugins/ImageEncoder/ImageEncoder.php @@ -177,7 +177,12 @@ class ImageEncoder extends Plugin $old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit')); try { try { - $image = Vips\Image::thumbnail($source, $width, ['height' => $height]); + if (!$smart_crop) { + $image = Vips\Image::thumbnail($source, $width, ['height' => $height]); + } else { + $image = Vips\Image::newFromFile($source, ['access' => 'sequential']); + $image = $image->smartcrop($width, $height, [Vips\Interesting::ATTENTION]); + } } catch (Exception $e) { Log::error(__METHOD__ . ' encountered exception: ' . get_class($e)); // TRANS: Exception thrown when trying to resize an unknown file type. @@ -196,10 +201,6 @@ class ImageEncoder extends Plugin $type = self::preferredType(); $mimetype = image_type_to_mime_type($type); - if ($smart_crop) { - $image = $image->smartcrop($width, $height); - } - $width = $image->width; $height = $image->height; diff --git a/src/Controller/Attachment.php b/src/Controller/Attachment.php index 60ad6cc7d9..3a87cfa437 100644 --- a/src/Controller/Attachment.php +++ b/src/Controller/Attachment.php @@ -147,6 +147,8 @@ class Attachment extends Controller throw new ClientException(_m('The requested thumbnail dimensions are not allowed'), 400); // 400 Bad Request } + [$width, $height] = AttachmentThumbnail::predictScalingValues($attachment->getWidth(), $attachment->getHeight(), $width, $height, $crop); + $thumbnail = AttachmentThumbnail::getOrCreate(attachment: $attachment, width: $width, height: $height, crop: $crop); $filename = $thumbnail->getFilename();