Skip to content

Commit 2839646

Browse files
committed
avoid upscaling images. fixes #186
If an image is smaller than the wanted thumbnail size, keep it in it's original size.
1 parent ceadb4d commit 2839646

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

classes/BasicFormatter.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class BasicFormatter
1313
{
1414
/** @var Options */
1515
protected $options;
16-
/** @var \Doku_Renderer */
16+
/** @var \Doku_Renderer */
1717
protected $renderer;
1818

1919
/**
@@ -74,23 +74,39 @@ protected function renderImage(Image $image)
7474

7575
/**
7676
* Calculate the thumbnail size
77+
*
78+
* @param Image $image
79+
* @param int|float $retina The retina scaling factor
80+
* @return array
7781
*/
78-
protected function getThumbnailSize(Image $image)
82+
protected function getThumbnailSize(Image $image, $retina = 1)
7983
{
80-
$crop = $this->options->crop;
84+
$thumbW = $this->options->thumbnailWidth * $retina;
85+
$thumbH = $this->options->thumbnailHeight * $retina;
86+
87+
// if image size is unknown, use the configured thumbnail size
8188
if (!$image->getWidth() || !$image->getHeight()) {
82-
$crop = true;
89+
return [$thumbW, $thumbH];
8390
}
84-
if (!$crop) {
91+
92+
// avoid upscaling
93+
if (
94+
$image->getWidth() < $thumbW &&
95+
$image->getHeight() < $thumbH
96+
) {
97+
return [$image->getWidth(), $image->getHeight()];
98+
}
99+
100+
if (!$this->options->crop) {
85101
[$thumbWidth, $thumbHeight] = $this->fitBoundingBox(
86102
$image->getWidth(),
87103
$image->getHeight(),
88-
$this->options->thumbnailWidth,
89-
$this->options->thumbnailHeight
104+
$thumbW,
105+
$thumbH
90106
);
91107
} else {
92-
$thumbWidth = $this->options->thumbnailWidth;
93-
$thumbHeight = $this->options->thumbnailHeight;
108+
$thumbWidth = $thumbW;
109+
$thumbHeight = $thumbH;
94110
}
95111
return [$thumbWidth, $thumbHeight];
96112
}

classes/XHTMLFormatter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ protected function renderImage(Image $image)
108108
global $ID;
109109

110110
// thumbnail image properties
111-
[$w, $h] = $this->getThumbnailSize($image);
112-
$w *= 2; // retina
113-
$h *= 2;
111+
[$w, $h] = $this->getThumbnailSize($image, 2);
112+
114113
$img = [];
115114
$img['width'] = $w;
116115
$img['height'] = $h;

0 commit comments

Comments
 (0)