Skip to content

Commit

Permalink
Merge pull request #1938 from luistar15/bugfix-thumbnail-url
Browse files Browse the repository at this point in the history
Fix Buggy thumbnail url generation #1928
  • Loading branch information
bobdenotter committed Oct 4, 2020
2 parents db7ecf1 + a9f486d commit 15cccc9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
30 changes: 18 additions & 12 deletions src/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class ImageController
/** @var Config */
private $config;

private $thumbnailOptions = ['w', 'h', 'fit'];

/** @var Server */
private $server;

Expand Down Expand Up @@ -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];
}
}
}
Expand All @@ -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) {
Expand Down
24 changes: 9 additions & 15 deletions src/Utils/ThumbnailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 15cccc9

Please sign in to comment.