{{< image src="images/example.png" width="750" alt="Mouseover text" caption="Optional caption text below image" >}}
Created
October 30, 2019 01:00
-
-
Save jakejarvis/71c0d6de7462420572397640913fc846 to your computer and use it in GitHub Desktop.
Hugo shortcode for resizing retina images with max-width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Automatic resizing for HiDPI/retina images --> | |
<!-- https://gohugo.io/content-management/image-processing/ --> | |
{{- $original := .Page.Resources.GetMatch (.Get "src") -}} | |
{{- .Scratch.Set "image" $original -}} | |
{{- $maxWidth := 910 -}} | |
{{- if .Get "width" }} | |
{{- $inputWidth := (int (.Get "width")) -}} | |
{{- $retinaWidth := (mul $inputWidth 2) -}} | |
{{- if gt $original.Width $retinaWidth -}} | |
{{- $finalWidth := (printf "%dx" $retinaWidth) -}} | |
{{- .Scratch.Set "image" ($original.Resize $finalWidth) -}} | |
{{- end -}} | |
{{- else -}} | |
{{- $inputWidth := $maxWidth -}} | |
{{- if gt $original.Width 1820 -}} | |
{{- .Scratch.Set "image" ($original.Resize "1820x") -}} | |
{{- end -}} | |
{{- end -}} | |
{{- $image := .Scratch.Get "image" -}} | |
{{- if .Get "caption" -}} | |
<figure> | |
<picture> | |
{{- else -}} | |
<p> | |
{{- end -}} | |
<img src="{{ $image.Permalink }}"{{ if .Get "alt" }} alt="{{ .Get "alt" }}"{{ end }}{{ if .Get "width" }} width="{{ .Get "width" }}"{{ end }}{{ if .Get "caption" }} title="{{ .Get "caption"}}"{{ end }}> | |
{{- if .Get "caption" -}} | |
</picture> | |
<figcaption>{{ .Get "caption" }}</figcaption> | |
</figure> | |
{{- else -}} | |
</p> | |
{{- end -}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment