From: Matthias Schmidt Date: Sat, 5 Jan 2019 13:35:23 +0000 (+0100) Subject: Improve `ViewableMedia::getElementTag()` X-Git-Tag: 5.2.0_Alpha_1~373 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=7a62c92a82326c788b2737a2510f5b6e13985498;p=GitHub%2FWoltLab%2FWCF.git Improve `ViewableMedia::getElementTag()` … to show the actual image instead of an icon even if the requested size is smaller than the delivered image. Close #2576 --- diff --git a/wcfsetup/install/files/lib/data/media/ViewableMedia.class.php b/wcfsetup/install/files/lib/data/media/ViewableMedia.class.php index f972631b4c..7c2f7f8e87 100644 --- a/wcfsetup/install/files/lib/data/media/ViewableMedia.class.php +++ b/wcfsetup/install/files/lib/data/media/ViewableMedia.class.php @@ -123,10 +123,67 @@ class ViewableMedia extends DatabaseObjectDecorator { * @return string */ public function getElementTag($size) { - if ($this->isImage && $this->tinyThumbnailType) { - $tinyThumbnail = Media::getThumbnailSizes()['tiny']; - if ($size <= $tinyThumbnail['width'] && $size <= $tinyThumbnail['height']) { - return '' . StringUtil::encodeHTML($this->altText) . 'title ? 'title="'.StringUtil::encodeHTML($this->title).'" ' : '').'style="width: ' . $size . 'px; height: ' . $size . 'px;">'; + if ($this->isImage) { + $width = $size; + $height = $size; + $link = null; + + if ($this->tinyThumbnailType) { + $link = $this->getThumbnailLink('tiny'); + + if ($size <= $this->tinyThumbnailWidth && $size <= $this->tinyThumbnailHeight) { + if ($this->tinyThumbnailHeight < $this->tinyThumbnailWidth) { + $height = round($this->tinyThumbnailHeight / $this->tinyThumbnailWidth * $size); + } + else { + $width = round($this->tinyThumbnailWidth / $this->tinyThumbnailHeight * $size); + } + } + else if ($size <= $this->tinyThumbnailWidth) { + $height = round($this->tinyThumbnailHeight / $this->tinyThumbnailWidth * $size); + } + else if ($size <= $this->tinyThumbnailHeight) { + $width = round($this->tinyThumbnailWidth / $this->tinyThumbnailHeight * $size); + } + else { + $link = null; + } + + $marginTop = floor(($size - $height) / 2); + } + + if ($link === null) { + $link = $this->getLink(); + + // round smaller dimension to + if ($size <= $this->width && $size <= $this->height) { + if ($this->height < $this->width) { + $height = round($this->height / $this->width * $size); + } + else { + $width = round($this->width / $this->height * $size); + } + } + else if ($size <= $this->width) { + $height = round($this->height / $this->width * $size); + } + else if ($size <= $this->height) { + $width = round($this->width / $this->height * $size); + } + else { + $width = $this->width; + $height = $this->height; + } + + $marginTop = floor(($size - $height) / 2); + } + + if ($link !== null) { + return ' + ' . StringUtil::encodeHTML($this->altText)
+						. 'title ? 'title="' . StringUtil::encodeHTML($this->title) . '" ' : '') + . 'style="width: ' . $width . 'px; height: ' . $height . 'px; margin-top: ' . $marginTop . 'px;"> + '; } }