The code I’m using where images are added through a template (site > templates > home.php
):
<div class='responsive-image'>
<div class='placeholder' style='padding-bottom: <?= number_format(100 / $page->image()->ratio(), 5, '.', '') ?>%'></div>
<img class='lazy lazyload' data-sizes='auto'
data-src='<?= $page->image()->url() ?>'
data-srcset='<?= $page->image()->srcset([400, 800, 1020, 2040]) ?>'
alt='<?= $page->title()->html() ?>'
width='100%' height='auto'/>
</div>
I would like to get about the same output as I would with the code above when I’m adding images with kirbytag (but probably instead of div class=‘responsive-image’
use a figure class=‘responsive-image’
). I’ve figured out how to generate an image but struggling with adding the placeholder. My code in plugins > custom-tags > index.html
:
<?php
$originalTag = Kirby\Text\KirbyTag::$types['image'];
Kirby::plugin('your/image', [
'tags' => [
'image' => [
'attr' => array_merge(
$originalTag['attr'],
[
'srcset',
'sizes',
]),
'html' => function($tag) use ($originalTag) {
$file = $tag->file($tag->value());
$srcset = option('srcsets.' . $tag->srcset);
$sizes = option('srcset-sizes.' . $tag->sizes);
$result = $originalTag['html']($tag);
if (! $file === true || is_null($srcset) === true || is_null($sizes) === true) {
return $result;
};
$pattern = '/<img.*?>/i';
$image = Html::img($tag->src, [
'data-src' => $tag->src,
'width' => '100%',
'height' => 'auto',
'class' => 'lazy lazyload',
'title' => $tag->title,
'alt' => $tag->alt ?? ' ',
'data-sizes' => 'auto',
'data-srcset' => $file->srcset($srcset),
]);
$result = preg_replace($pattern, $image , $result);
return $result;
},
],
],
]);
Help would be very appreciated