{{- /* Heavily modified from https://runtimeterror.dev/hugo-image-optimization-render-hook/ , which according to the footer there makes this code MIT licensed */ -}} {{- /* Also: https://www.brycewray.com/posts/2022/06/responsive-optimized-images-hugo/ */ -}} {{- /* theme uses 720px box, so use that as our base, do 0.5x(?), 1x, 1.5x, 2x, 3x. TODO: doesn't handle small images! */ -}} {{- $sizes := slice "360" "720" "1080" "1440" "2160" -}} {{- $alt := .Text -}} {{- $title := "" -}} {{- if .Title }}{{ $title = .Title }}{{ else }}{{ $title = .Text }}{{ end -}} {{- $src := .Destination -}} {{- $original_ext := path.Ext .Destination -}} {{- $webpOpts := "webp q85 lanczos"}} {{- $avifOpts := "avif q75 lanczos"}} {{- $jpegOpts := "jpeg q75 lanczos"}} {{- if .Page.Resources.GetMatch $src -}} {{- with .Page.Resources.GetMatch $src -}} {{- $image := . -}} {{- /* we want to keep PNGs lossless, so supply them in lossless webp/avif/png. everything else can get lossily converted */ -}} {{- if eq $original_ext ".png" -}} {{- $fallback := $image.Resize "720x png lanczos" -}} {{ $alt }} {{- else -}} {{- /* TODO: jxl */ -}} {{- $fallback := $image.Resize (print "720x " $jpegOpts ) -}} {{ $alt }} {{- end -}} {{- end -}} {{- else -}} {{- /* present unprocessed/remote images too */ -}} {{ $alt }} {{- end -}}