Skip to content

Commit

Permalink
Merge pull request #1679 from bolt/bugfix/thumbnails
Browse files Browse the repository at this point in the history
Prettify thumbnail paths. Use Bolt 4 cropping options
  • Loading branch information
bobdenotter authored Aug 5, 2020
2 parents fe05522 + 20c957b commit 3f04b55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ private function saveAsFile(string $paramString, string $filename): void
}

$filesystem = new Filesystem();

$filePath = sprintf('%s%s%s%s%s', $this->getPath('thumbs'), DIRECTORY_SEPARATOR, $paramString, DIRECTORY_SEPARATOR, $filename);
$folderMode = $this->config->get('general/filepermissions/folders', 0775);
$fileMode = $this->config->get('general/filepermissions/files', 0664);
Expand Down Expand Up @@ -141,14 +140,15 @@ private function parseParameters(string $paramString): void
$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',
];

foreach ($raw as $rawParameter) {
if (mb_strpos($rawParameter, '=') !== false) {
[$key, $value] = explode('=', $rawParameter);

// @todo Add more thumbnailing options here, perhaps.
if (in_array($key, $this->thumbnailOptions, true)) {
if (in_array($key, $this->thumbnailOptions, true) && ! in_array($key, $this->parameters, true)) {
$this->parameters[$key] = $value;
}
}
Expand All @@ -170,4 +170,28 @@ private function isImage(string $filename): bool

return array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $imageExtensions, true);
}

public function parseFit(string $fit): string
{
switch ($fit) {
case 'n':
case 'contain':
case 'default':
return 'contain';
case 'm':
case 'max':
return 'max';
case 'f':
case 'fill':
return 'fill';
case 's':
case 'stretch':
return 'stretch';
case 'c':
case 'crop':
return 'crop';
default:
return $fit;
}
}
}
2 changes: 1 addition & 1 deletion src/Utils/ThumbnailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function parameters(?int $width = null, ?int $height = null, ?string $loc
$paramString = sprintf('%s×%s', $width, $height);

if ($fit) {
$paramString .= '×fit=' . $fit;
$paramString .= '×' . $fit;
}

if ($location && $location !== 'files') {
Expand Down

0 comments on commit 3f04b55

Please sign in to comment.