Skip to content

Commit

Permalink
feat: allow passing imgproxy filters (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarlovi authored Jan 22, 2024
1 parent 006e29e commit 10f65a8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/Bridge/Twig/Extension/ThumbnailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ public function __construct(private RequestStack $requestStack, private ?string
public function getFunctions(): array
{
return [
new TwigFunction('yassg_thumbnail', function (array $context, string $path, array $options = []): string {
new TwigFunction('yassg_thumbnail', /** @param array<string, string>|array{self: object} $options */ function (array $context, string $path, array $options = []): string {
if (str_starts_with($path, './')) {
if (! isset($context['_path'])) {
if (isset($options['self'])) {
if (! \is_object($options['self']) || ! property_exists($options['self'], '__path')) {
throw new \RuntimeException('Cannot use yassg_thumbnail() with {self: object} as the second argument, pass {self: object} as the second argument');
}
$context['_path'] = $options['self']->__path;
} else {
$candidates = [];
Expand Down Expand Up @@ -60,15 +63,33 @@ public function getFunctions(): array
throw new \RuntimeException('Cannot use yassg_thumbnail options without sigwin_yassg.imgproxy_url configured');
}

$filters = [];

$filter .= 'rs:fill';
if (isset($options['width'])) {
$filter .= ':'.(string) $options['width'];
if (! is_numeric($options['width'])) {
throw new \RuntimeException('Invalid thumbnail width');
}
$filter .= ':'.$options['width'];
unset($options['width']);

if (isset($options['height'])) {
$filter .= ':'.(string) $options['height'];
if (! is_numeric($options['height'])) {
throw new \RuntimeException('Invalid thumbnail height');
}
$filter .= ':'.$options['height'];
unset($options['height']);
}
$filters[] = $filter;
}

foreach ($options as $name => $value) {
if (! \is_string($value)) {
throw new \RuntimeException('Invalid thumbnail option '.$name);
}
$filters[] = $name.':'.$value;
}
$filter .= '/';
$filter = implode('/', $filters).'/';
}
$relative = str_replace($GLOBALS['YASSG_BASEDIR'], '', $path);

Expand Down
2 changes: 2 additions & 0 deletions tests/functional/site/content/articles/images/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ This is a database lookup example: {{yassg_get('articles', '/hello-world.md').ti
This is an asset lookup: {{ yassg_thumbnail(item.image) }}

![Logo]({{ yassg_thumbnail(item.image, {width: 260, height: 480}) }})

![Logo]({{ yassg_thumbnail(item.image, {width: 400, height: 200, gravity: "no"}) }})
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ <h1>Images!</h1>
<p>This is a database lookup example: Hello World!</p>
<p>This is an asset lookup: /sub/dir/another/content/articles/images/image.509b6b4c.webp</p>
<p><img src="/sub/dir/another/content/articles/images/image.8514a44a.webp" alt="Logo" /></p>
<p><img src="/sub/dir/another/content/articles/images/image.ca8c5d95.webp" alt="Logo" /></p>

</div>
</div>
Expand Down

0 comments on commit 10f65a8

Please sign in to comment.