use get-image partial

This commit is contained in:
Ben Goldsworthy 2025-06-04 17:57:21 +02:00
parent ed9e769b2d
commit edba5d203e
No known key found for this signature in database
8 changed files with 94 additions and 47 deletions

View file

@ -1,25 +1,62 @@
{{- /*
Renders an image, resizing it if valid.
<!--
Renders an image, resizing it if valid.
@params img The image.
*/
-}}
@params img The image.
@params size The resize target. Optional.
@params format Image conversation format. Optional.
-->
<!-- Variable assignment -->
{{- $img := .img -}}
{{- $hint := default ( index $img.Params "hint" ) "" }}
{{- $size := cond ( isset . "size" ) .size "" }}
{{- $isResizable := not ( in ( slice "avif" "svg" "gif" ) $img.MediaType.SubType ) -}}
{{- $format := cond ( isset . "format" ) .format "webp" }}
{{- $permalink := "" -}}
<!-- Validation -->
{{- if ( not ( isset . "img" ) ) -}}
{{- errorf "No img set" -}}
{{- end -}}
{{- if ne .img.MediaType.MainType "image" -}}
{{- errorf "Image '%q' is not an image" .img }}
{{- end -}}
{{- if ( isset . "size" ) -}}
{{- if $isResizable -}}
{{- $width := ( first 1 ( split .size "x" ) ) -}}
{{- if gt $width $img.Width -}}
{{- warnf "Requested size %s is more than image width %d (%q)" $width $img.Width $img.File.Path -}}
{{- end -}}
{{- else -}}
{{- warnf "Size set on non-resizable image of type %s; ignoring" $img.MediaType.SubType -}}
{{- end -}}
{{- end -}}
<!-- Rendering -->
{{- $isResizable := not ( in ( slice "avif" "svg" "gif" ) .img.MediaType.SubType ) -}}
{{- if $isResizable -}}
{{- with .img.Resize "1200x webp" -}}
{{- .RelPermalink -}}
{{- if $size -}}
{{- if eq $size "same" -}}
{{- $permalink = ( $img.Process ( printf "%s %s" $format $hint ) ).RelPermalink -}}
{{- else -}}
{{- $permalink = ( $img.Resize ( printf "%s %s %s" $size $format $hint ) ).RelPermalink -}}
{{- end -}}
{{- else -}}
{{- if gt $img.Width 1200 -}}
{{- $permalink = ( $img.Resize ( printf "1200x %s %s" $format $hint ) ).RelPermalink -}}
{{- else if gt $img.Width 800 -}}
{{- $permalink = ( $img.Resize ( printf "800x %s %s" $format $hint ) ).RelPermalink -}}
{{- else -}}
{{- $permalink = ( $img.Process ( printf "%s %s" $format $hint ) ).RelPermalink -}}
{{- end -}}
{{- end -}}
{{- else }}
{{- .img.RelPermalink -}}
{{- $permalink = $img.RelPermalink -}}
{{- end -}}
{{- return $permalink -}}