feat: avoid image upscaling during image processing

There's now a threshold for each scenario. Images less than x width will not be processed.
This commit is contained in:
Jimmy Cai 2023-10-21 18:24:52 +02:00
parent b9fb60f2ba
commit 47a57a2ad2
3 changed files with 37 additions and 14 deletions

View file

@ -157,5 +157,17 @@ imageProcessing:
- webp
cover:
enabled: true
small:
width: 800
threshold: 1000 # Only process images above this width
big:
width: 1600
threshold: 2000
content:
enabled: true
small:
width: 480
threshold: 600
big:
width: 1024
threshold: 1200

View file

@ -3,7 +3,7 @@
{{- $alt := .PlainText | safeHTML -}}
{{- $Width := 0 -}}
{{- $Height := 0 -}}
{{- $Srcset := "" -}}
{{- $Srcset := slice -}}
{{- $imageProcessing := .Page.Site.Params.imageProcessing.content.enabled -}}
{{- $allowedTypes := .Page.Site.Params.ImageProcessing.AllowedTypes -}}
{{- $resizableTypes := .Page.Site.Params.ImageProcessing.ResizableTypes -}}
@ -22,9 +22,16 @@
{{- $galleryImage = true -}}
{{- if $imageProcessing -}}
{{- $small := $image.Resize `480x` -}}
{{- $big := $image.Resize `1024x` -}}
{{- $Srcset = printf `%s 480w, %s 1024w` $small.RelPermalink $big.RelPermalink -}}
{{- $keys := slice "small" "big" -}}
{{- range $keys -}}
{{- with (index $.Page.Site.Params.ImageProcessing.Content .) -}}
{{- if gt $Width .Threshold -}}
{{- $resized := $image.Resize (printf "%dx" .Width) -}}
{{- $Srcset = $Srcset | append (printf `%s %dw` $resized.RelPermalink .Width) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $Srcset = $Srcset | append (printf `%s %dw` $Permalink $Width) -}}
{{- end -}}
{{- end -}}
{{- end -}}
@ -32,7 +39,7 @@
<img src="{{ $Permalink }}"
{{ with $Width }}width="{{ . }}"{{ end }}
{{ with $Height }}height="{{ . }}"{{ end }}
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
{{ with $Srcset }}srcset="{{ delimit . `, ` }}"{{ end }}
loading="lazy"
{{ with $alt }}
alt="{{ . }}"

View file

@ -7,19 +7,23 @@
{{- $Permalink := $image.resource.RelPermalink -}}
{{- $Width := $image.resource.Width -}}
{{- $Height := $image.resource.Height -}}
{{- $Srcset := "" -}}
{{- $Srcset := slice -}}
{{- if .Page.Site.Params.imageProcessing.cover.enabled -}}
{{- $thumbnail := $image.resource.Resize "800x" -}}
{{- $thumbnailRetina := $image.resource.Resize "1600x" -}}
{{- $Srcset = printf "%s 800w, %s 1600w" $thumbnail.RelPermalink $thumbnailRetina.RelPermalink -}}
{{- $Permalink = $thumbnail.RelPermalink -}}
{{- $Width = $thumbnail.Width -}}
{{- $Height = $thumbnail.Height -}}
{{- if .Page.Site.Params.ImageProcessing.Cover.Enabled -}}
{{- $keys := slice "small" "big" -}}
{{- range $keys -}}
{{- with (index $.Page.Site.Params.ImageProcessing.Cover .) -}}
{{- if gt $Width .Threshold -}}
{{- $resized := $image.resource.Resize (printf "%dx" .Width) -}}
{{- $Srcset = $Srcset | append (printf `%s %dw` $resized.RelPermalink .Width) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $Srcset = $Srcset | append (printf `%s %dw` $Permalink $Width) -}}
{{- end -}}
<img src="{{ $Permalink }}"
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
{{ with $Srcset }}srcset="{{ delimit . `, ` }}"{{ end }}
width="{{ $Width }}"
height="{{ $Height }}"
loading="lazy"