Gallery: ratio="original" keeps the images' aspect ratio, use it on the home page
All checks were successful
/ build (push) Successful in 2m0s
/ accessibility (push) Successful in 3m55s

This commit is contained in:
Stefan Bethke 2026-11-16 00:05:25 +01:00
commit 11712cd501
3 changed files with 44 additions and 9 deletions

View file

@ -44,6 +44,17 @@
}
}
/* ratio="original": the images keep their own shape, and a single one spans the full width. */
.gallery--original {
.gallery__link img {
aspect-ratio: auto;
}
.gallery__item:only-child {
grid-column: 1 / -1;
}
}
@media (prefers-reduced-motion: no-preference) {
.gallery__link img {
transition: scale 0.2s;

View file

@ -19,17 +19,28 @@ One image in a gallery, see gallery.html. Params:
{{- else if or (ne $image.ResourceType "image") (eq $image.MediaType.SubType "svg") -}}
{{- 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. */ -}}
{{- /* Thumbnails at two sizes, never larger than the image itself: square, or uncropped with
ratio="original" on the gallery. */ -}}
{{- /* 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) -}}
{{- $small := "" -}}
{{- $large := "" -}}
{{- $sizes := "(min-width: 48rem) 34rem, 100vw" -}}
{{- if eq (.Parent.Get "ratio") "original" -}}
{{- $width := math.Min 1600 $oriented.Width | int -}}
{{- $small = $oriented.Resize (printf "%dx" (div $width 2)) -}}
{{- $large = $oriented.Resize (printf "%dx" $width) -}}
{{- $sizes = "(min-width: 48rem) 70rem, 100vw" -}}
{{- else -}}
{{- $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) -}}
{{- end -}}
<li class="gallery__item">
<figure class="gallery__figure">
<a class="gallery__link" href="{{ $image.RelPermalink }}" data-width="{{ $oriented.Width }}" data-height="{{ $oriented.Height }}" data-alt="{{ $alt }}"{{ with $credit }} data-credit="{{ . }}"{{ end }}>
<img src="{{ $large.RelPermalink }}" srcset="{{ $small.RelPermalink }} {{ $small.Width }}w, {{ $large.RelPermalink }} {{ $large.Width }}w" sizes="(min-width: 48rem) 34rem, 100vw" width="{{ $large.Width }}" height="{{ $large.Height }}" alt="{{ $alt }}" loading="lazy" decoding="async">
<img src="{{ $large.RelPermalink }}" srcset="{{ $small.RelPermalink }} {{ $small.Width }}w, {{ $large.RelPermalink }} {{ $large.Width }}w" sizes="{{ $sizes }}" width="{{ $large.Width }}" height="{{ $large.Height }}" alt="{{ $alt }}" loading="lazy" decoding="async">
<span class="visually-hidden"> ({{ i18n "gallery_enlarge" }})</span>
</a>
{{- with $credit }}

View file

@ -7,8 +7,15 @@ thumbnails link to the full image.
{{< gallery-image src="photo1.jpg" alt="What the photo shows" credit="© Photographer" >}}
{{< gallery-image src="photo2.jpg" alt="…" credit="…" >}}
{{< /gallery >}}
Param: ratio="original" keeps the images' own aspect ratio instead of square thumbnails; a
single image then spans the full width.
*/ -}}
<div class="gallery" data-gallery
{{- $ratio := .Get "ratio" | default "square" -}}
{{- if not (in (slice "square" "original") $ratio) -}}
{{- errorf "gallery: ratio must be \"square\" or \"original\", got %q: %s" $ratio .Position -}}
{{- end -}}
<div class="gallery{{ if eq $ratio "original" }} gallery--original{{ end }}" data-gallery
data-label-dialog="{{ i18n "gallery_dialog" }}"
data-label-close="{{ i18n "gallery_close" }}"
data-label-previous="{{ i18n "gallery_previous" }}"