Hugo Partial for controlled output of image sizes depending on monitor resolution and viewport size.
In my blog post I describe the source code, the corresponding SCSS and give tips for testing the Hugo localhost browser output at @1 and @2 monitor resolutions in a network.
{{ with (.context.Resources.GetMatch "article-img") }}
{{ $images := newScratch }}
{{ $images.Set "css-class" $.cssclass }}
{{ $images.Set "img@2" ($.context.Resources.GetMatch "article-img") }}
{{ $width := ($images.Get "img@2").Width }}
{{ $width := printf "%dx" (div $width 2) }}
{{ $images.Set "img@1" (($images.Get "img@2").Resize $width) }}
{{ $images.Set "img768@2" (($images.Get "img@2").Resize "1536x") }}
{{ $images.Set "img768@1" (($images.Get "img@1").Resize "768x") }}
{{ $images.Set "img576@2" (($images.Get "img@2").Resize "1152x") }}
{{ $images.Set "img576@1" (($images.Get "img@1").Resize "576x") }}
{{ $images.Set "img400@2" (($images.Get "img@2").Resize "800x") }}
{{ $images.Set "img400@1" (($images.Get "img@1").Resize "400x") }}
{{ $images.Set "img320@2" (($images.Get "img@2").Resize "640x") }}
{{ $images.Set "img320@1" (($images.Get "img@1").Resize "320x") }}
{{ $images.Set "imgx150@2" (($images.Get "img@2").Resize "x300") }}
{{ $images.Set "imgx150@1" (($images.Get "img@1").Resize "x150") }}
{{ if eq ($images.Get "css-class") "img-articleimg" }}
<picture>
<source media="(min-width: 992px)"
srcset='{{($images.Get "img@1").RelPermalink}} 1x,
{{($images.Get "img@2").RelPermalink}} 2x'>
<source media="(min-width: 768px)"
srcset='{{($images.Get "img768@1").RelPermalink}} 1x,
{{($images.Get "img768@2").RelPermalink}} 2x'>
<source media="(min-width: 576px)"
srcset='{{($images.Get "img576@1").RelPermalink}} 1x,
{{($images.Get "img576@2").RelPermalink}} 2x'>
<source media="(min-width: 400px)"
srcset='{{($images.Get "img400@1").RelPermalink}} 1x,
{{($images.Get "img400@2").RelPermalink}} 2x'>
<source media="(min-width: 320px)"
srcset='{{($images.Get "img320@1").RelPermalink}} 1x,
{{($images.Get "img320@2").RelPermalink}} 2x'>
<img src='{{- ($images.Get "img@1").RelPermalink -}}' class='{{- $images.Get "css-class" -}}' alt="{{ $.context.Title }}" />
</picture>
{{ else if eq ($images.Get "css-class") "img-teaserimg" }}
<picture>
<source media="(min-width: 992px)"
srcset='{{($images.Get "imgx150@1").RelPermalink}} 1x,
{{($images.Get "imgx150@2").RelPermalink}} 2x'>
<source media="(min-width: 768px)"
srcset='{{($images.Get "img768@1").RelPermalink}} 1x,
{{($images.Get "img768@2").RelPermalink}} 2x'>
<source media="(min-width: 576px)"
srcset='{{($images.Get "img576@1").RelPermalink}} 1x,
{{($images.Get "img576@2").RelPermalink}} 2x'>
<source media="(min-width: 400px)"
srcset='{{($images.Get "img400@1").RelPermalink}} 1x,
{{($images.Get "img400@2").RelPermalink}} 2x'>
<source media="(min-width: 320px)"
srcset='{{($images.Get "img320@1").RelPermalink}} 1x,
{{($images.Get "img320@2").RelPermalink}} 2x'>
<img src='{{- ($images.Get "imgx150@1").RelPermalink -}}' class='{{- $images.Get "css-class" -}}' alt="{{ $.context.Title }}" />
</picture>
{{ end }}
{{ end }}
The partial is called as follows:
{{ partial "img-post" (dict "context" . "cssclass" "img-articleimg") }}