1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 18:50:29 -04:00
jarv.is/layouts/partials/functions/optimize-image.html

26 lines
725 B
HTML

{{- /* Automatic resizing for HiDPI/retina images */ -}}
{{- $original := .Page.Resources.GetMatch (.Get "src") -}}
{{- .Scratch.Set "image" $original -}}
{{- $setWidth := 910 -}}
{{- if .Get "width" -}}
{{- $setWidth = (int (.Get "width")) -}}
{{- end -}}
{{- $retinaWidth := (mul $setWidth 2) -}}
{{- if gt $original.Width $retinaWidth -}}
{{- $finalWidth := (printf "%dx" $retinaWidth) -}}
{{- .Scratch.Set "image" ($original.Resize $finalWidth) -}}
{{- end -}}
{{- $image := .Scratch.Get "image" -}}
{{- $origRatio := (div (float $image.Height) $image.Width) -}}
{{- .Scratch.Set "displayWidth" $setWidth -}}
{{- .Scratch.Set "displayHeight" (math.Ceil (mul $origRatio $setWidth)) -}}
{{- return $image -}}