diff --git a/layouts/partials/optimize-image.html b/layouts/partials/optimize-image.html
new file mode 100644
index 00000000..477b574c
--- /dev/null
+++ b/layouts/partials/optimize-image.html
@@ -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 -}}
diff --git a/layouts/shortcodes/image.amp.html b/layouts/shortcodes/image.amp.html
index c854e2cb..f323a1b5 100644
--- a/layouts/shortcodes/image.amp.html
+++ b/layouts/shortcodes/image.amp.html
@@ -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" . -}}
diff --git a/layouts/shortcodes/image.html b/layouts/shortcodes/image.html
index ef2cb11c..3937b0f7 100644
--- a/layouts/shortcodes/image.html
+++ b/layouts/shortcodes/image.html
@@ -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 }}
-
{{ $.Page.RenderString . | safeHTML }}
-{{ else }}
-

-{{ end }}
+{{- end }}
diff --git a/layouts/shortcodes/image.rss.html b/layouts/shortcodes/image.rss.html
index 6eceedde..21cbef80 100644
--- a/layouts/shortcodes/image.rss.html
+++ b/layouts/shortcodes/image.rss.html
@@ -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" . -}}
-
{{ with .Inner }}
{{ $.Page.RenderString . | safeHTML }}{{ end -}}