From 2839646dee4447e17b545efe493841d2671de687 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 19 Oct 2023 10:29:24 +0200 Subject: [PATCH] avoid upscaling images. fixes #186 If an image is smaller than the wanted thumbnail size, keep it in it's original size. --- classes/BasicFormatter.php | 34 +++++++++++++++++++++++++--------- classes/XHTMLFormatter.php | 5 ++--- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/classes/BasicFormatter.php b/classes/BasicFormatter.php index 8084876..699e3a1 100644 --- a/classes/BasicFormatter.php +++ b/classes/BasicFormatter.php @@ -13,7 +13,7 @@ class BasicFormatter { /** @var Options */ protected $options; - /** @var \Doku_Renderer */ + /** @var \Doku_Renderer */ protected $renderer; /** @@ -74,23 +74,39 @@ class BasicFormatter /** * Calculate the thumbnail size + * + * @param Image $image + * @param int|float $retina The retina scaling factor + * @return array */ - protected function getThumbnailSize(Image $image) + protected function getThumbnailSize(Image $image, $retina = 1) { - $crop = $this->options->crop; + $thumbW = $this->options->thumbnailWidth * $retina; + $thumbH = $this->options->thumbnailHeight * $retina; + + // if image size is unknown, use the configured thumbnail size if (!$image->getWidth() || !$image->getHeight()) { - $crop = true; + return [$thumbW, $thumbH]; } - if (!$crop) { + + // avoid upscaling + if ( + $image->getWidth() < $thumbW && + $image->getHeight() < $thumbH + ) { + return [$image->getWidth(), $image->getHeight()]; + } + + if (!$this->options->crop) { [$thumbWidth, $thumbHeight] = $this->fitBoundingBox( $image->getWidth(), $image->getHeight(), - $this->options->thumbnailWidth, - $this->options->thumbnailHeight + $thumbW, + $thumbH ); } else { - $thumbWidth = $this->options->thumbnailWidth; - $thumbHeight = $this->options->thumbnailHeight; + $thumbWidth = $thumbW; + $thumbHeight = $thumbH; } return [$thumbWidth, $thumbHeight]; } diff --git a/classes/XHTMLFormatter.php b/classes/XHTMLFormatter.php index d6122a4..953481f 100644 --- a/classes/XHTMLFormatter.php +++ b/classes/XHTMLFormatter.php @@ -108,9 +108,8 @@ class XHTMLFormatter extends BasicFormatter global $ID; // thumbnail image properties - [$w, $h] = $this->getThumbnailSize($image); - $w *= 2; // retina - $h *= 2; + [$w, $h] = $this->getThumbnailSize($image, 2); + $img = []; $img['width'] = $w; $img['height'] = $h;