1{{- /* Heavily modified from https://runtimeterror.dev/hugo-image-optimization-render-hook/ , which according to the footer there makes this code MIT licensed */ -}}
 2{{- /* Also: https://www.brycewray.com/posts/2022/06/responsive-optimized-images-hugo/ */ -}}
 3{{- /* theme uses 720px box, so use that as our base, do 0.5x(?), 1x, 1.5x, 2x, 3x. TODO: doesn't handle small images! */ -}}
 4{{- $sizes := slice "360" "720" "1080" "1440" "2160" -}}
 5{{- $alt := .Text -}}
 6{{- $title := "" -}}
 7{{- if .Title }}{{ $title = .Title }}{{ else }}{{ $title = .Text }}{{ end -}}
 8{{- $src := .Destination -}}
 9{{- $original_ext := path.Ext .Destination -}}
10{{- $webpOpts := "webp q85 lanczos"}}
11{{- $avifOpts := "avif q75 lanczos"}}
12{{- $jpegOpts := "jpeg q75 lanczos"}}
13<picture>
14{{- if .Page.Resources.GetMatch $src -}}
15  {{- with .Page.Resources.GetMatch $src -}}
16    {{- $image := . -}}
17    {{- /* we want to keep PNGs lossless, so supply them in lossless webp/avif/png. everything else can get lossily converted */ -}}
18    {{- if eq $original_ext ".png" -}}
19    <source type="image/avif" srcset="
20    {{- with $sizes -}}
21      {{- range $i, $e := . -}}
22        {{- if ge $image.Width . -}}
23            {{- $resized := $image.Resize (print . "x " $avifOpts " lossless" ) -}}
24            {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
25        {{- end -}}
26      {{- end -}}
27    {{- end -}}" sizes="720px" />
28
29    <source type="image/webp" srcset="
30    {{- with $sizes -}}
31      {{- range $i, $e := . -}}
32        {{- if ge $image.Width . -}}
33            {{- $resized := $image.Resize (print . "x " $webpOpts " lossless" ) -}}
34            {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
35        {{- end -}}
36      {{- end -}}
37    {{- end -}}" sizes="720px" />
38
39    <source type="image/png" srcset="
40      {{- with $sizes -}}
41        {{- range $i, $e := . -}}
42          {{- if ge $image.Width . -}}
43              {{- $resized := $image.Resize (print . "x png lanczos" ) -}}
44              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
45          {{- end -}}
46        {{- end -}}
47      {{- end -}}" sizes="720px" />
48
49      {{- $fallback := $image.Resize "720x png lanczos" -}}
50      <img src="{{ $fallback.RelPermalink }}"
51        {{- if $alt -}} alt="{{ $alt }}" {{ end }}
52        {{- if $title -}} title="{{ $title }}" {{ end }}
53        width="{{ $fallback.Width }}" height="{{ $fallback.Height }}" />
54    {{- else -}}
55      {{- /* TODO: jxl */ -}}
56      <source type="image/avif" srcset="
57      {{- with $sizes -}}
58        {{- range $i, $e := . -}}
59          {{- if ge $image.Width . -}}
60              {{- $resized := $image.Resize (print . "x " $avifOpts ) -}}
61              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
62          {{- end -}}
63        {{- end -}}
64      {{- end -}}" sizes="720px" />
65
66      <source type="image/webp" srcset="
67      {{- with $sizes -}}
68        {{- range $i, $e := . -}}
69          {{- if ge $image.Width . -}}
70              {{- $resized := $image.Resize (print . "x " $webpOpts ) -}}
71              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
72          {{- end -}}
73        {{- end -}}
74      {{- end -}}" sizes="720px" />
75
76      <source type="image/jpeg" srcset="
77      {{- with $sizes -}}
78        {{- range $i, $e := . -}}
79          {{- if ge $image.Width . -}}
80              {{- $resized := $image.Resize (print . "x " $jpegOpts ) -}}
81              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
82          {{- end -}}
83        {{- end -}}
84      {{- end -}}" sizes="720px" />
85
86      {{- $fallback := $image.Resize (print "720x " $jpegOpts ) -}}
87      <img src="{{ $fallback.RelPermalink }}"
88        {{- if $alt -}} alt="{{ $alt }}" {{ end }}
89        {{- if $title -}} title="{{ $title }}" {{ end }}
90        width="{{ $fallback.Width }}" height="{{ $fallback.Height }}" />
91    {{- end -}}
92  {{- end -}}
93{{- else -}}
94  {{- /* present unprocessed/remote images too */ -}}
95  <img src="{{ .Destination | safeURL }}" alt="{{ $alt }}" title="{{ $alt }}" />
96{{- end -}}
97</picture>