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:
parent
cc5e75e469
commit
315ec5b77b
@ -20,10 +20,17 @@ or
|
||||
{{- 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 -}}
|
||||
|
||||
{{- $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 -}}
|
||||
{{- $resizedFullHD := $img.Fit "1920x1080 q50" -}}
|
||||
{{- $resizedHD := $img.Fit "1280x720 q50" -}}
|
||||
{{- $resizedSD := $img.Fit "640x360 q50" -}}
|
||||
{{- $sizes := slice (slice 640 360 50) (slice 1280 720 50) (slice 1920 1080 50) -}}
|
||||
{{- $srcset := slice -}}
|
||||
{{- $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">
|
||||
|
Loading…
Reference in New Issue
Block a user