Omphaloskepsis-2/layouts/partials/images/get-image.html

63 lines
1.9 KiB
HTML
Raw Permalink Normal View History

2025-06-04 17:57:21 +02:00
<!--
Renders an image, resizing it if valid.
2025-06-04 17:57:21 +02:00
@params img The image.
@params size The resize target. Optional.
@params format Image conversation format. Optional.
-->
2025-06-04 17:57:21 +02:00
<!-- 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 -->
2025-06-04 17:57:21 +02:00
{{- if ( not ( isset . "img" ) ) -}}
{{- errorf "No img set" -}}
{{- end -}}
{{- if ne .img.MediaType.MainType "image" -}}
{{- errorf "Image '%q' is not an image" .img }}
{{- end -}}
2025-06-04 17:57:21 +02:00
{{- 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 -->
{{- if $isResizable -}}
2025-06-04 17:57:21 +02:00
{{- 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 }}
2025-06-04 17:57:21 +02:00
{{- $permalink = $img.RelPermalink -}}
{{- end -}}
2025-06-04 17:57:21 +02:00
{{- return $permalink -}}