Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Buggy thumbnail url generation #1928 #1938

Merged
merged 4 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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