1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 13:56:22 -04:00

extract/reuse logic for optimizing images

This commit is contained in:
Jake Jarvis 2019-12-23 11:52:30 -06:00
parent e25c98154e
commit a7b9f897f8
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
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 -}}

View File

@ -1,39 +1,12 @@
{{- /* Automatic resizing for HiDPI/retina images */ -}}
{{- $original := .Page.Resources.GetMatch (.Get "src") -}}
{{- .Scratch.Set "image" $original -}}
{{- $maxWidth := 910 -}}
{{- $setWidth := 0 -}}
{{- if .Get "width" -}}
{{- $setWidth = (int (.Get "width")) -}}
{{- $retinaWidth := (mul $setWidth 2) -}}
{{- if gt $original.Width $retinaWidth -}}
{{- $finalWidth := (printf "%dx" $retinaWidth) -}}
{{- .Scratch.Set "image" ($original.Resize $finalWidth) -}}
{{- end -}}
{{- else -}}
{{- $setWidth = $maxWidth -}}
{{- if gt $original.Width 1820 -}}
{{- .Scratch.Set "image" ($original.Resize "1820x") -}}
{{- end -}}
{{- end -}}
{{- $image := .Scratch.Get "image" -}}
{{- $origRatio := (div (float $image.Height) $image.Width) -}}
{{- $displayWidth := $setWidth -}}
{{- $displayHeight := (math.Ceil (mul $origRatio $setWidth)) -}}
{{- $optimized := partial "optimize-image" . -}}
<p class="image">
<amp-img
{{- with .Get "alt" }} alt="{{ . | safeHTMLAttr }}" title="{{ . | safeHTMLAttr }}"{{ end }}
{{- with .Inner }} alt="{{ . | markdownify | plainify | safeHTMLAttr }}" title="{{ . | markdownify | plainify | safeHTMLAttr }}"{{ end }}
src="{{ $image.Permalink }}"
width="{{ $displayWidth }}"
height="{{ $displayHeight }}"
src="{{ $optimized.Permalink }}"
width="{{ .Scratch.Get "displayWidth" }}"
height="{{ .Scratch.Get "displayHeight" }}"
layout="intrinsic">
</amp-img>
</p>

View File

@ -1,41 +1,14 @@
{{- /* Automatic resizing for HiDPI/retina images */ -}}
{{- $optimized := partial "optimize-image" . -}}
{{- $original := .Page.Resources.GetMatch (.Get "src") -}}
{{- .Scratch.Set "image" $original -}}
{{- $maxWidth := 910 -}}
{{- $setWidth := 0 -}}
{{- if .Get "width" -}}
{{- $setWidth = (int (.Get "width")) -}}
{{- $retinaWidth := (mul $setWidth 2) -}}
{{- if gt $original.Width $retinaWidth -}}
{{- $finalWidth := (printf "%dx" $retinaWidth) -}}
{{- .Scratch.Set "image" ($original.Resize $finalWidth) -}}
{{- end -}}
{{- else -}}
{{- $setWidth = $maxWidth -}}
{{- if gt $original.Width 1820 -}}
{{- .Scratch.Set "image" ($original.Resize "1820x") -}}
{{- end -}}
{{- end -}}
{{- $image := .Scratch.Get "image" -}}
{{- $origRatio := (div (float $image.Height) $image.Width) -}}
{{- $displayWidth := $setWidth -}}
{{- $displayHeight := (math.Ceil (mul $origRatio $setWidth)) -}}
{{ with .Inner }}
{{- with .Inner }}
<figure>
<picture>
<img src="{{ $image.Permalink }}" width="{{ $displayWidth }}" height="{{ $displayHeight }}"
<img src="{{ $optimized.Permalink }}" width="{{ $.Scratch.Get "displayWidth" }}" height="{{ $.Scratch.Get "displayHeight" }}"
alt="{{ . | markdownify | plainify | safeHTMLAttr }}" title="{{ . | markdownify | plainify | safeHTMLAttr }}">
</picture>
<figcaption>{{ $.Page.RenderString . | safeHTML }}</figcaption>
</figure>
{{ else }}
<p><img src="{{ $image.Permalink }}" width="{{ $displayWidth }}" height="{{ $displayHeight }}"
{{- else -}}
<p><img src="{{ $optimized.Permalink }}" width="{{ $.Scratch.Get "displayWidth" }}" height="{{ $.Scratch.Get "displayHeight" }}"
{{- with .Get "alt" }} alt="{{ . | safeHTMLAttr }}" title="{{ . | safeHTMLAttr }}"{{ end }}></p>
{{ end }}
{{- end }}

View File

@ -1,34 +1,7 @@
{{- /* Automatic resizing for HiDPI/retina images */ -}}
{{- $original := .Page.Resources.GetMatch (.Get "src") -}}
{{- .Scratch.Set "image" $original -}}
{{- $maxWidth := 910 -}}
{{- $setWidth := 0 -}}
{{- if .Get "width" -}}
{{- $setWidth = (int (.Get "width")) -}}
{{- $retinaWidth := (mul $setWidth 2) -}}
{{- if gt $original.Width $retinaWidth -}}
{{- $finalWidth := (printf "%dx" $retinaWidth) -}}
{{- .Scratch.Set "image" ($original.Resize $finalWidth) -}}
{{- end -}}
{{- else -}}
{{- $setWidth = $maxWidth -}}
{{- if gt $original.Width 1820 -}}
{{- .Scratch.Set "image" ($original.Resize "1820x") -}}
{{- end -}}
{{- end -}}
{{- $image := .Scratch.Get "image" -}}
{{- $origRatio := (div (float $image.Height) $image.Width) -}}
{{- $displayWidth := $setWidth -}}
{{- $displayHeight := (math.Ceil (mul $origRatio $setWidth)) -}}
{{- $optimized := partial "optimize-image" . -}}
<p style="text-align: center;">
<img src="{{ $image.Permalink }}" width="{{ $displayWidth }}" height="{{ $displayHeight }}"
<img src="{{ $optimized.Permalink }}" width="{{ .Scratch.Get "displayWidth" }}" height="{{ .Scratch.Get "displayHeight" }}"
{{- with .Get "alt" }} alt="{{ . | safeHTMLAttr }}" title="{{ . | safeHTMLAttr }}"{{ end }}
{{- with .Inner }} alt="{{ . | markdownify | plainify | safeHTMLAttr }}" title="{{ . | markdownify | plainify | safeHTMLAttr }}"{{ end }}>
{{ with .Inner }}<br>{{ $.Page.RenderString . | safeHTML }}{{ end -}}