Skip to content

Commit

Permalink
(image: ) tag: width: auto
Browse files Browse the repository at this point in the history
Fixes #5064
  • Loading branch information
distantnative committed Aug 11, 2024
1 parent f14c731 commit 96e8a86
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
17 changes: 15 additions & 2 deletions config/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
'width'
],
'html' => function (KirbyTag $tag): string {
$kirby = $tag->kirby();

$tag->width ??= $kirby->option('kirbytext.image.width');
$tag->height ??= $kirby->option('kirbytext.image.height');

if ($tag->file = $tag->file($tag->value)) {
$tag->src = $tag->file->url();
$tag->alt ??= $tag->file->alt()->or('')->value();
Expand All @@ -129,6 +134,14 @@

$tag->srcset = $tag->file->srcset($srcset);
}

if ($tag->width === 'auto') {
$tag->width = $tag->file->width();
}
if ($tag->height === 'auto') {
$tag->height = $tag->file->height();
}

} else {
$tag->src = Url::to($tag->value);
}
Expand Down Expand Up @@ -157,14 +170,14 @@
'alt' => $tag->alt ?? ''
]);

if ($tag->kirby()->option('kirbytext.image.figure', true) === false) {
if ($kirby->option('kirbytext.image.figure', true) === false) {
return $link($image);
}

// render KirbyText in caption
if ($tag->caption) {
$options = ['markdown' => ['inline' => true]];
$caption = $tag->kirby()->kirbytext($tag->caption, $options);
$caption = $kirby->kirbytext($tag->caption, $options);
$tag->caption = [$caption];
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Text/ImageKirbyTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,43 @@ public function testWithParent()
'parent' => $page,
]));
}

public function testWithWidthHeightAuto()
{
$app = $this->app->clone([
'options' => [
'kirbytext' => [
'image' => [
'width' => 'auto',
'height' => 'auto'
]
]
],
'roots' => [
'index' => '/dev/null',
],
'site' => [
'children' => [
[
'slug' => 'a',
'root' => __DIR__ . '/fixtures/kirbytext/image-auto',
'files' => [
[
'filename' => 'cat.jpg',
]
]
]
]
]
]);

$page = $app->page('a');
$image = $page->image('cat.jpg');
$expected = '<figure><img alt="" height="500" src="/media/pages/a/' . $image->mediaHash() . '/cat.jpg" width="500"></figure>';

$this->assertSame($expected, $app->kirbytag('image', 'cat.jpg', [], [
'parent' => $page,
]));
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 96e8a86

Please sign in to comment.