Make image tag use thumb preset by default


I’m looking for a way to have images inserted using the (image: image.jpg) tag in the panel use a pre-defined thumb preset by default. Preferably, users should not have to specify any width or height manually for the image. I’d also like to make the tag output HTML with the srcset attribute.

Any tips as to where I should start looking to accomplish this? Thank you!

You may wanna have a look at this thread. And here’s the documentation for creating custom kirbytags.

Thank you! I was wondering if there was some alternative to overriding the image tag completely, but that seems to be the way to go.

It’ either overriding the image tag or creating a new one. Why don’t you want to override the image tag completely?

I understand it’s all or nothing with the image tag. I was wondering if there was solution which didn’t involve duplicating Kirby code, with related issues of maintainability and the like… something like a built-in option. But overriding the image tag is not a big deal. I replaced this code (from the image tag):

// use the file url if available and otherwise the given url
$url = $file ? $file->url() : url($url);

… with this:

if ($file) {
    if ($tag->attr('width') || $tag->attr('height')) {
        // Explicit width or height, use file URL
        $url = $file->url();
    } else {  
        // Use the thumb URL
        $url = $file->thumb('article')->url();
} else {
    // Use given URL
    $url = url($url);

… with a thumb preset called “article”, and it seems to do the trick with regards to using thumb presets for the images. I’ll get back to the srcset part later.

I wouldn’t worry about that, the image tag won’t see any changes in Kirby 2

1 Like