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
765 B
HTML

{{/* Automatic resizing for HiDPI/retina images */}}
{{ $original := .Page.Resources.GetMatch (.Get "src") }}
{{ .Scratch.Set "image" $original }}
{{ $setWidth := .Site.Params.pageMaxWidth }}
{{ 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) }}
{{ $displayWidth := $setWidth }}
{{ $displayHeight := (math.Ceil (mul $origRatio $setWidth)) }}
{{ return (dict "Permalink" $image.Permalink "Width" $displayWidth "Height" $displayHeight) }}