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
6 changed files with 44 additions and 9 deletions

View file

@ -280,7 +280,9 @@ In the GeoJSON, `coordinates` are already in `[lon, lat]` order, the colour and
| `alt` | yes | What the image shows. It is the thumbnail's alt text and the caption in the lightbox. | | `alt` | yes | What the image shows. It is the thumbnail's alt text and the caption in the lightbox. |
| `credit` | no | Photographer and licence, shown under the thumbnail and in the lightbox. | | `credit` | no | Photographer and licence, shown under the thumbnail and in the lightbox. |
Hugo crops the thumbnails to squares (keeping the most interesting part of the picture) at build time; the lightbox shows the original file. Without JavaScript, a thumbnail links to the original file. A missing image, a missing `alt` or a `gallery-image` outside a `gallery` stops the build with an error that names the page. `{{< gallery ratio="original" >}}` keeps each image's own aspect ratio instead of cropping it to a square; a gallery with a single image then spans the full width, as on the home page. Without `ratio` (or with `ratio="square"`), the thumbnails are square.
Hugo crops the square thumbnails (keeping the most interesting part of the picture) at build time; the lightbox shows the original file. Without JavaScript, a thumbnail links to the original file. A missing image, a missing `alt` or a `gallery-image` outside a `gallery` stops the build with an error that names the page.
## Shortcode: Event cards ## Shortcode: Event cards

View file

@ -26,7 +26,9 @@ Hackertours-Desk: DECT 8687 "TOUR", +49 40 573 0830 8687
[C3Nav-Direktlink](https://40c3.c3nav.de/l/hackertours-desk/) Ihr findet uns im Foyer Eingang Mitte, neben den Kassen. [C3Nav-Direktlink](https://40c3.c3nav.de/l/hackertours-desk/) Ihr findet uns im Foyer Eingang Mitte, neben den Kassen.
![Hackertours an der Messe. CC0 Amzi](hackertours_banner_messe.jpg) {{< gallery ratio="original" >}}
{{< gallery-image src=`hackertours_banner_messe.jpg` alt=`Hackertours am Eingang Mitte der Messe und Congress Hamburg` credit=`CC0 Amzi` >}}
{{< /gallery >}}
## Veranstaltungen ## Veranstaltungen

View file

@ -26,7 +26,9 @@ Hackertours Desk: DECT 8687 "TOUR", +49 40 573 0830 8687
[C3Nav Direct Link](https://40c3.c3nav.de/l/hackertours-desk/) You can find us at ground level of the Central Entrance foyer, next to the cash desk. [C3Nav Direct Link](https://40c3.c3nav.de/l/hackertours-desk/) You can find us at ground level of the Central Entrance foyer, next to the cash desk.
![Hackertours at the Messe. CC0 Amzi](hackertours_banner_messe.jpg) {{< gallery ratio="original" >}}
{{< gallery-image src=`hackertours_banner_messe.jpg` alt=`Hackertours at Central Entrance of Messe und Congress Hamburg` credit=`CC0 Amzi` >}}
{{< /gallery >}}
## Events ## Events

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) { @media (prefers-reduced-motion: no-preference) {
.gallery__link img { .gallery__link img {
transition: scale 0.2s; 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") -}} {{- 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 -}} {{- errorf "gallery-image: %q is not a photo Hugo can resize: %s" $src .Position -}}
{{- else -}} {{- 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 {{- /* 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. */ -}} that for the original, but Hugo doesn't when it resizes. */ -}}
{{- $oriented := $image.Filter images.AutoOrient -}} {{- $oriented := $image.Filter images.AutoOrient -}}
{{- $side := math.Min 800 $oriented.Width $oriented.Height | int -}} {{- $small := "" -}}
{{- $small := $oriented.Fill (printf "%dx%d Smart" (div $side 2) (div $side 2)) -}} {{- $large := "" -}}
{{- $large := $oriented.Fill (printf "%dx%d Smart" $side $side) -}} {{- $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"> <li class="gallery__item">
<figure class="gallery__figure"> <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 }}> <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> <span class="visually-hidden"> ({{ i18n "gallery_enlarge" }})</span>
</a> </a>
{{- with $credit }} {{- 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="photo1.jpg" alt="What the photo shows" credit="© Photographer" >}}
{{< gallery-image src="photo2.jpg" alt="…" credit="…" >}} {{< gallery-image src="photo2.jpg" alt="…" credit="…" >}}
{{< /gallery >}} {{< /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-dialog="{{ i18n "gallery_dialog" }}"
data-label-close="{{ i18n "gallery_close" }}" data-label-close="{{ i18n "gallery_close" }}"
data-label-previous="{{ i18n "gallery_previous" }}" data-label-previous="{{ i18n "gallery_previous" }}"