HUGO

Retrieving image size

I can somehow get the size of the image height and weight when rendering from md (/layouts/_default/_markup/render-image.html)?

Hi! You could do something like this:

{{ $image := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) }}
{{ $height := $image.Height }}
{{ $width := $image.Width }}

Two ways. If the image is a page resource (in the same folder as your markdown file) take the way @grob described.

If the file is in static, try the following:

{{ with (imageConfig "favicon.ico") }}
favicon.ico: {{.Width}} x {{.Height}}
{{ end }}

@Grob and @davidsneighbour thank you very much

There is a question what to do if I do not know the name of the picture in advance. Now your decision is falling due to the fact that I use pictures from other sites. You can somehow protect yourself from this.

This is what I use:

{{ if $image }}
  {{/* for images as page resources */}}
  {{/* image code with .Width, .Height, resizing … */}}
{{ else }}
  {{/* for ‘static’ and external images */}}
  <figure>
    <img src="{{ .Destination | safeURL }}" loading="lazy" alt="{{ with $alt }}{{ . }}{{ else }}{{ $caption | .Page.RenderString | plainify | default "&nbsp;" }}{{ end }}">
    {{ with $caption }}
      <figcaption>{{ . | $.Page.RenderString }}</figcaption>
    {{ end }}
  </figure>
{{ end }}

Hope it helps for your use case.

Another rough idea is to check your variable for https://:

{{ if in $yourVariable "https://" }}
  {{/* use .Width and .Height here */}}
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.