Access image width / height inside image render hook

Hi.

I am working on native lazy loading implementation for my website. To do so correctly I need to add a loading="lazy" attribute and width / height attribute. i really struggle to get a width / height of a resource. Could anyone help me please?

<img
  src="{{ .Destination | safeURL }}"
  alt="{{ .Text }}"
  loading="lazy"
  width="{{ I_NEED_YOUR_HELP_PLEASE }}"
  height="{{ I_NEED_YOUR_HELP_PLEASE }}"
/>

Thanks in advance.

You could pass the dimensions as the last word of the title, then parse the title.

![Alternate text](foo.jpg "This is the title =600x400")

Feels hacky to me. I want go to read dimensions for me, without being explicit.

Can you make use of imageConfig?

I use it in my Zen theme figure shortcode:

Thanks a lot @frjo.
I managed to implement what I need using your suggestion.

{{ $img := imageConfig (add “/static” (.Destination | safeURL)) }}

<img
  src="{{ .Destination | safeURL }}"
  alt="{{ .Text }}"
  loading="lazy"
  width="{{ $img.Width }}"
  height="{{ $img.Height }}"
/>

Thats all I needed.

1 Like