From 311a3b13706d816d38ae59589b5e576189c66666 Mon Sep 17 00:00:00 2001 From: Stefan Bethke Date: Sat, 17 Oct 2026 10:55:54 +0200 Subject: [PATCH] Fix orientation of thumbnails (cherry picked from commit f6a698a64b97c9487dc1c5e00e47a1763b2e5585) --- layouts/_markup/render-image.html | 7 +++++-- layouts/_partials/head/extra.html | 4 +++- layouts/_shortcodes/gallery-image.html | 11 +++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/layouts/_markup/render-image.html b/layouts/_markup/render-image.html index 9c7d95f..56396a4 100644 --- a/layouts/_markup/render-image.html +++ b/layouts/_markup/render-image.html @@ -9,8 +9,11 @@ An image on its own line becomes a figure; images sharing a paragraph get an inl {{- with or (.PageInner.Resources.Get (strings.TrimPrefix "./" .Destination)) (resources.Get .Destination) -}} {{- $src = .RelPermalink -}} {{- if and (eq .ResourceType "image") (ne .MediaType.SubType "svg") -}} - {{- $width = .Width -}} - {{- $height = .Height -}} + {{- /* Width and height as the browser shows the image, i.e. turned as its EXIF orientation + says (only JPEG and TIFF carry one). */ -}} + {{- $shown := cond (in (slice "jpeg" "tiff") .MediaType.SubType) (.Filter images.AutoOrient) . -}} + {{- $width = $shown.Width -}} + {{- $height = $shown.Height -}} {{- end -}} {{- end -}} {{- /* A title that only repeats the alt text is not a credit. */ -}} diff --git a/layouts/_partials/head/extra.html b/layouts/_partials/head/extra.html index e61670b..9bc15d9 100644 --- a/layouts/_partials/head/extra.html +++ b/layouts/_partials/head/extra.html @@ -27,7 +27,9 @@ og:image:alt. {{- with $resource -}} {{- $image := . -}} {{- if and (eq .ResourceType "image") (ne .MediaType.SubType "svg") -}} - {{- if gt .Width 1200 }}{{ $image = .Resize "1200x" }}{{ end -}} + {{- /* Turned as its EXIF orientation says; Hugo doesn't do that when it resizes. */ -}} + {{- $image = .Filter images.AutoOrient -}} + {{- if gt $image.Width 1200 }}{{ $image = $image.Resize "1200x" }}{{ end -}} {{- $width = $image.Width -}} {{- $height = $image.Height -}} {{- end -}} diff --git a/layouts/_shortcodes/gallery-image.html b/layouts/_shortcodes/gallery-image.html index 2b69906..89b88f3 100644 --- a/layouts/_shortcodes/gallery-image.html +++ b/layouts/_shortcodes/gallery-image.html @@ -20,12 +20,15 @@ One image in a gallery, see gallery.html. Params: {{- errorf "gallery-image: %q is not a photo Hugo can resize: %s" $src .Position -}} {{- else -}} {{- /* Square thumbnails at two sizes, never larger than the image itself. */ -}} -{{- $side := math.Min 800 $image.Width $image.Height | int -}} -{{- $small := $image.Fill (printf "%dx%d Smart" (div $side 2) (div $side 2)) -}} -{{- $large := $image.Fill (printf "%dx%d Smart" $side $side) -}} +{{- /* Turned as its EXIF orientation says (phone photos are often stored sideways); browsers do +that for the original, but Hugo doesn't when it resizes. */ -}} +{{- $oriented := $image.Filter images.AutoOrient -}} +{{- $side := math.Min 800 $oriented.Width $oriented.Height | int -}} +{{- $small := $oriented.Fill (printf "%dx%d Smart" (div $side 2) (div $side 2)) -}} +{{- $large := $oriented.Fill (printf "%dx%d Smart" $side $side) -}}