Generalize blogPhoto shortcode

Generate the resized images from simple config array + generate only
those, that are actualy smaller then the original.
This commit is contained in:
Vojtěch Káně 2020-04-20 13:54:08 +02:00
parent cc5e75e469
commit 315ec5b77b

View File

@ -20,10 +20,17 @@ or
{{- if (eq $imageName nil) }}{{ errorf "Missing image name in %s" .Position }}{{ end -}} {{- if (eq $imageName nil) }}{{ errorf "Missing image name in %s" .Position }}{{ end -}}
{{- if (eq $alt nil) }}{{ errorf "Missing alt for image %s in %s" $imageName .Position }}{{ end -}} {{- if (eq $alt nil) }}{{ errorf "Missing alt for image %s in %s" $imageName .Position }}{{ end -}}
{{- $img := .Page.Resources.GetMatch (printf "%s" $imageName) | default nil -}} {{- $img := .Page.Resources.GetMatch $imageName | default nil -}}
{{- if (eq $img nil) }}{{ errorf "Image %s not found in %s" $imageName .Position }}{{ end -}} {{- if (eq $img nil) }}{{ errorf "Image %s not found in %s" $imageName .Position }}{{ end -}}
{{- $resizedFullHD := $img.Fit "1920x1080 q50" -}} {{- $sizes := slice (slice 640 360 50) (slice 1280 720 50) (slice 1920 1080 50) -}}
{{- $resizedHD := $img.Fit "1280x720 q50" -}} {{- $srcset := slice -}}
{{- $resizedSD := $img.Fit "640x360 q50" -}} {{- $largest := index $sizes 0 -}}
{{- range $sizes -}}
{{- if (or (gt $img.Width (index . 0)) (gt $img.Height (index . 1))) -}}
{{- $resized := $img.Fit (printf "%dx%d q%d" (index . 0) (index . 1) (index . 2)) -}}
{{- $srcset = $srcset | append (printf "%s %dw" $resized.RelPermalink (index . 0)) -}}
{{- $largest = $resized -}}
{{- end -}}
{{- end -}}
<img src="{{ $resizedFullHD.RelPermalink }}" alt="{{ $alt }}" srcset="{{ $resizedFullHD.RelPermalink }} 1920w, {{ $resizedHD.RelPermalink }} 1280w, {{ $resizedSD.RelPermalink }} 640w" sizes="(orientation: portrait) 100vw, 50vw"> <img src="{{ $largest.RelPermalink }}" alt="{{ $alt }}" srcset="{{ delimit $srcset ", " }}" sizes="(orientation: portrait) 100vw, 50vw">