Skip to content

Commit

Permalink
feat: infer global thumbs format for placeholder uri
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Mar 17, 2022
1 parent 4c43174 commit cae43be
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions classes/KirbyExtended/BlurryPlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ public static function image(\Kirby\Cms\File $file, $ratio = null): string
$pixelTarget = option('kirby-extended.blurry-placeholder.pixel-target', 60);

// Aims for an image of ~P pixels (w * h = ~P)
$placeholderHeight = sqrt($pixelTarget / ($ratio ?? $file->ratio()));
$placeholderWidth = $pixelTarget / $placeholderHeight;
$height = sqrt($pixelTarget / ($ratio ?? $file->ratio()));
$width = $pixelTarget / $height;

$placeholderImage = $file->thumb([
'width' => round($placeholderWidth),
'height' => round($placeholderHeight),
$options = [
'width' => round($width),
'height' => round($height),
'crop' => true,
'quality' => 60
])->dataUri();
'quality' => 40
];

$svgHeight = number_format($placeholderHeight, 2, '.', '');
$svgWidth = number_format($placeholderWidth, 2, '.', '');
if ($format = option('thumbs.format')) {
$options['format'] = $format;
}

$uri = $file->thumb($options)->dataUri();

$svgHeight = number_format($height, 2, '.', '');
$svgWidth = number_format($width, 2, '.', '');

// Wrap the blurred image in a SVG to avoid rasterizing the filter
$svg = <<<EOD
Expand All @@ -39,7 +45,7 @@ public static function image(\Kirby\Cms\File $file, $ratio = null): string
<feFuncA type="discrete" tableValues="1 1"></feFuncA>
</feComponentTransfer>
</filter>
<image filter="url(#b)" x="0" y="0" width="100%" height="100%" href="{$placeholderImage}"></image>
<image filter="url(#b)" x="0" y="0" width="100%" height="100%" href="{$uri}"></image>
</svg>
EOD;

Expand Down

0 comments on commit cae43be

Please sign in to comment.