1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-19 15:45:33 -04:00

extract/reuse logic for optimizing images

This commit is contained in:
2019-12-23 11:52:30 -06:00
parent e25c98154e
commit a7b9f897f8
4 changed files with 37 additions and 93 deletions

View File

@@ -0,0 +1,25 @@
{{- /* 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 -}}