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:
25
layouts/partials/optimize-image.html
Normal file
25
layouts/partials/optimize-image.html
Normal 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 -}}
|
Reference in New Issue
Block a user