1

Images sociales

This commit is contained in:
2026-03-14 21:27:49 +01:00
parent 32fdcc201f
commit 7676fe3e22
3 changed files with 86 additions and 14 deletions

View File

@@ -0,0 +1,54 @@
{{- $image := .image -}}
{{- if not $image -}}
{{- errorf "head/social-image-variant.html: missing image resource" -}}
{{- end -}}
{{- $targetWidth := .width -}}
{{- if not $targetWidth -}}
{{- errorf "head/social-image-variant.html: missing target width for %q" $image.Name -}}
{{- end -}}
{{- $targetHeight := .height -}}
{{- if not $targetHeight -}}
{{- errorf "head/social-image-variant.html: missing target height for %q" $image.Name -}}
{{- end -}}
{{- $anchor := .anchor -}}
{{- if not $anchor -}}
{{- errorf "head/social-image-variant.html: missing crop anchor for %q" $image.Name -}}
{{- end -}}
{{- $subType := lower $image.MediaType.SubType -}}
{{- if eq $subType "svg" -}}
{{- errorf "head/social-image-variant.html: unsupported svg image %q" $image.Name -}}
{{- end -}}
{{- $width := $targetWidth -}}
{{- $height := $targetHeight -}}
{{- if ge (mul $image.Width $targetHeight) (mul $image.Height $targetWidth) -}}
{{- if lt $image.Height $height -}}
{{- $height = $image.Height -}}
{{- end -}}
{{- $width = div (mul $height $targetWidth) $targetHeight -}}
{{- else -}}
{{- if lt $image.Width $width -}}
{{- $width = $image.Width -}}
{{- end -}}
{{- $height = div (mul $width $targetHeight) $targetWidth -}}
{{- end -}}
{{- $format := "jpg q85" -}}
{{- if eq $subType "png" -}}
{{- $format = "png" -}}
{{- end -}}
{{- $variant := $image.Fill (printf "%dx%d %s %s" $width $height $anchor $format) -}}
{{- if .blur -}}
{{- $variant = $variant.Filter (images.GaussianBlur 6) -}}
{{- end -}}
{{- return dict
"url" $variant.Permalink
"width" $variant.Width
"height" $variant.Height
"alt" (.alt | strings.TrimSpace | truncate 420)
-}}