{{- $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) -}}