diff --git a/src/Controller/ImageController.php b/src/Controller/ImageController.php index e5f2151c9..9a88fd565 100644 --- a/src/Controller/ImageController.php +++ b/src/Controller/ImageController.php @@ -20,8 +20,6 @@ class ImageController /** @var Config */ private $config; - private $thumbnailOptions = ['w', 'h', 'fit']; - /** @var Server */ private $server; @@ -136,22 +134,25 @@ private function buildResponse(string $filename): Response private function parseParameters(string $paramString): void { - $raw = explode('×', str_replace('x', '×', $paramString)); + $raw = explode('×', $paramString); $this->parameters = [ 'w' => is_numeric($raw[0]) ? (int) $raw[0] : 400, - 'h' => ! empty($raw[1]) && is_numeric($raw[1]) ? (int) $raw[1] : 300, - 'fit' => ! empty($raw[2]) ? $this->parseFit($raw[2]) : 'default', + 'h' => is_numeric($raw[1]) ? (int) $raw[1] : 300, + 'fit' => 'default', + 'location' => 'files', ]; - foreach ($raw as $rawParameter) { - if (mb_strpos($rawParameter, '=') !== false) { - [$key, $value] = explode('=', $rawParameter); + if (isset($raw[3])) { + $this->parameters['fit'] = $this->parseFit($raw[2]); + $this->parameters['location'] = $raw[3]; + } elseif (isset($raw[2])) { + $posible_fit = $this->parseFit($raw[2]); - // @todo Add more thumbnailing options here, perhaps. - if (in_array($key, $this->thumbnailOptions, true) && ! in_array($key, $this->parameters, true)) { - $this->parameters[$key] = $value; - } + if ($this->testFit($posible_fit)) { + $this->parameters['fit'] = $posible_fit; + } else { + $this->parameters['location'] = $raw[2]; } } } @@ -172,6 +173,11 @@ private function isImage(string $filename): bool return array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $imageExtensions, true); } + private function testFit(string $fit): bool + { + return (bool) preg_match('/^(contain|max|fill|stretch|crop)(-.+)?/', $fit); + } + public function parseFit(string $fit): string { switch ($fit) { diff --git a/src/Utils/ThumbnailHelper.php b/src/Utils/ThumbnailHelper.php index 39744a94b..23e2e8fdb 100644 --- a/src/Utils/ThumbnailHelper.php +++ b/src/Utils/ThumbnailHelper.php @@ -17,7 +17,7 @@ public function __construct(?Config $config = null) $this->config = $config; } - public function parameters(?int $width = null, ?int $height = null, ?string $location = null, ?string $path = null, ?string $fit = null): string + private function parameters(?int $width = null, ?int $height = null, ?string $fit = null, ?string $location = null): string { if (! $width && ! $height) { $width = $this->config->get('general/thumbnails/default_thumbnail/0', 320); @@ -30,21 +30,11 @@ public function parameters(?int $width = null, ?int $height = null, ?string $loc $height = 10000; } - $paramString = sprintf('%s×%s', $width, $height); - - if ($fit) { - $paramString .= '×' . $fit; - } - - if ($location && $location !== 'files') { - $paramString .= '×location=' . $location; + if ($location === 'files') { + $location = null; } - if ($path) { - $paramString .= '×path=' . $path; - } - - return $paramString; + return implode('×', array_filter([$width, $height, $fit, $location])); } public function path(?string $filename = null, ?int $width = null, ?int $height = null, ?string $location = null, ?string $path = null, ?string $fit = null): string @@ -53,7 +43,11 @@ public function path(?string $filename = null, ?int $width = null, ?int $height return '/assets/images/placeholder.png'; } - $paramString = $this->parameters($width, $height, $location, $path, $fit); + if ($path) { + $filename = $path . '/' . $filename; + } + + $paramString = $this->parameters($width, $height, $fit, $location); $filename = Str::ensureStartsWith($filename, '/'); return sprintf('/thumbs/%s%s', $paramString, $filename);