Commit de210c8

Daniel Axtens <dja@axtens.net>
2026-11-15 01:26:09
lossless processing of pngs, lanzcos sampling
1 parent da1e71b
Changed files (1)
blog
layouts
_default
blog/layouts/_default/_markup/render-image.html
@@ -6,19 +6,21 @@
 {{- $title := "" -}}
 {{- if .Title }}{{ $title = .Title }}{{ else }}{{ $title = .Text }}{{ end -}}
 {{- $src := .Destination -}}
-{{- $webpOpts := "webp q85"}}
-{{- $avifOpts := "avif q75"}}
-{{- $jpegOpts := "jpeg q75"}}
+{{- $original_ext := path.Ext .Destination -}}
+{{- $webpOpts := "webp q85 lanczos"}}
+{{- $avifOpts := "avif q75 lanczos"}}
+{{- $jpegOpts := "jpeg q75 lanczos"}}
 <picture>
 {{- if .Page.Resources.GetMatch $src -}}
   {{- with .Page.Resources.GetMatch $src -}}
     {{- $image := . -}}
-    {{- /* TODO: jxl */ -}}
+    {{- /* 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" -}}
     <source type="image/avif" srcset="
     {{- with $sizes -}}
       {{- range $i, $e := . -}}
         {{- if ge $image.Width . -}}
-            {{- $resized := $image.Resize (print . "x " $avifOpts ) -}}
+            {{- $resized := $image.Resize (print . "x " $avifOpts " lossless" ) -}}
             {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
         {{- end -}}
       {{- end -}}
@@ -28,28 +30,65 @@
     {{- with $sizes -}}
       {{- range $i, $e := . -}}
         {{- if ge $image.Width . -}}
-            {{- $resized := $image.Resize (print . "x " $webpOpts ) -}}
+            {{- $resized := $image.Resize (print . "x " $webpOpts " lossless" ) -}}
             {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
         {{- end -}}
       {{- end -}}
     {{- end -}}" sizes="720px" />
 
-    <source type="image/jpeg" srcset="
-    {{- with $sizes -}}
-      {{- range $i, $e := . -}}
-        {{- if ge $image.Width . -}}
-            {{- $resized := $image.Resize (print . "x " $jpegOpts ) -}}
-            {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
+    <source type="image/png" srcset="
+      {{- with $sizes -}}
+        {{- range $i, $e := . -}}
+          {{- if ge $image.Width . -}}
+              {{- $resized := $image.Resize (print . "x png lanczos" ) -}}
+              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
+          {{- end -}}
         {{- end -}}
-      {{- end -}}
-    {{- end -}}" sizes="720px" />
+      {{- end -}}" sizes="720px" />
 
+      {{- $fallback := $image.Resize "720x png lanczos" -}}
+      <img src="{{ $fallback.RelPermalink }}"
+        {{- if $alt -}} alt="{{ $alt }}" {{ end }}
+        {{- if $title -}} title="{{ $title }}" {{ end }}
+        width="{{ $fallback.Width }}" height="{{ $fallback.Height }}" />
+    {{- else -}}
+      {{- /* TODO: jxl */ -}}
+      <source type="image/avif" srcset="
+      {{- with $sizes -}}
+        {{- range $i, $e := . -}}
+          {{- if ge $image.Width . -}}
+              {{- $resized := $image.Resize (print . "x " $avifOpts ) -}}
+              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
+          {{- end -}}
+        {{- end -}}
+      {{- end -}}" sizes="720px" />
+
+      <source type="image/webp" srcset="
+      {{- with $sizes -}}
+        {{- range $i, $e := . -}}
+          {{- if ge $image.Width . -}}
+              {{- $resized := $image.Resize (print . "x " $webpOpts ) -}}
+              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
+          {{- end -}}
+        {{- end -}}
+      {{- end -}}" sizes="720px" />
+
+      <source type="image/jpeg" srcset="
+      {{- with $sizes -}}
+        {{- range $i, $e := . -}}
+          {{- if ge $image.Width . -}}
+              {{- $resized := $image.Resize (print . "x " $jpegOpts ) -}}
+              {{- if $i }},{{ end }} {{ $resized.RelPermalink }} {{ . }}w
+          {{- end -}}
+        {{- end -}}
+      {{- end -}}" sizes="720px" />
 
-    {{- $fallback := $image.Resize (print "720x " $jpegOpts ) -}}
-    <img src="{{ $fallback.RelPermalink }}"
-      {{- if $alt -}} alt="{{ $alt }}" {{ end }}
-      {{- if $title -}} title="{{ $title }}" {{ end }}
-      width="{{ $fallback.Width }}" height="{{ $fallback.Height }}" />
+      {{- $fallback := $image.Resize (print "720x " $jpegOpts ) -}}
+      <img src="{{ $fallback.RelPermalink }}"
+        {{- if $alt -}} alt="{{ $alt }}" {{ end }}
+        {{- if $title -}} title="{{ $title }}" {{ end }}
+        width="{{ $fallback.Width }}" height="{{ $fallback.Height }}" />
+    {{- end -}}
   {{- end -}}
 {{- else -}}
   {{- /* present unprocessed/remote images too */ -}}