|
4 | 4 | {{ $className := .className | default "" }} |
5 | 5 | {{ $isAvatar := isset . "avatar" }} |
6 | 6 | {{ $image := resources.Get $sourcePath }} |
7 | | -{{ $image := $image | default (resources.Get "/images/placeholder.png") }} |
| 7 | +{{ $image = $image | default (resources.Get "/images/placeholder.png") }} |
8 | 8 |
|
9 | 9 | {{/* Set responsive image sizes based on avatar flag */}} |
10 | 10 | {{ $sizeSmall := cond $isAvatar "32x" "600x" }} |
|
13 | 13 | {{ $sizeFull := cond $isAvatar "128x" "1400x" }} |
14 | 14 |
|
15 | 15 | {{ with $image }} |
16 | | - {{/* Generate resized images in WebP and JPEG formats */}} |
| 16 | + {{/* Determine original image format (prefer resource MediaType, fall back to path extension) */}} |
| 17 | + {{ $extFromPath := lower (strings.TrimPrefix "." (path.Ext $sourcePath)) }} |
| 18 | + {{ $origFormat := (.MediaType.SubType | default $extFromPath) | lower }} |
| 19 | + {{ $isPNG := or (eq $origFormat "png") (eq $origFormat "x-png") }} |
| 20 | + |
| 21 | + {{/* Generate resized images in WebP and original/fallback formats */}} |
17 | 22 | {{ $webpSmall := .Resize (printf "%s webp q80" $sizeSmall) }} |
18 | 23 | {{ $webpMedium := .Resize (printf "%s webp q80" $sizeMedium) }} |
19 | 24 | {{ $webpLarge := .Resize (printf "%s webp q80" $sizeLarge) }} |
20 | | - {{ $jpegFull := .Resize (printf "%s jpg q90" $sizeFull) }} |
| 25 | + |
| 26 | + {{ $fallbackConfig := cond $isPNG (dict "format" "png" "mime" "image/png") (dict "format" "jpg q90" "mime" "image/jpeg") }} |
| 27 | + {{ $fallback := .Resize (printf "%s %s" $sizeFull (index $fallbackConfig "format")) }} |
| 28 | + {{ $mimeFallback := index $fallbackConfig "mime" }} |
| 29 | + {{ $sizesAttr := cond $isAvatar "(min-width: 1024px) 128px, (min-width: 800px) 64px, 32px" "(min-width: 1024px) 1400px, (min-width: 800px) 1000px, 600px" }} |
21 | 30 |
|
22 | 31 | <figure class="{{ $className }}"> |
23 | 32 | <picture> |
24 | | - {{/* JPEG fallback for large screens */}} |
25 | | - <source srcset="{{ $jpegFull.Permalink }}" type="image/jpeg" media="(min-width: 1024px)"> |
26 | | - {{/* WebP for large, medium, and small screens */}} |
| 33 | + {{/* WebP sources for modern browsers */}} |
27 | 34 | <source srcset="{{ $webpLarge.Permalink }}" type="image/webp" media="(min-width: 1024px)"> |
28 | 35 | <source srcset="{{ $webpMedium.Permalink }}" type="image/webp" media="(max-width: 1024px)"> |
29 | 36 | <source srcset="{{ $webpSmall.Permalink }}" type="image/webp" media="(max-width: 800px)"> |
| 37 | + |
| 38 | + {{/* Fallback for large screens in original format (PNG or JPEG) */}} |
| 39 | + <source srcset="{{ $fallback.Permalink }}" type="{{ $mimeFallback }}" media="(min-width: 1024px)"> |
| 40 | + |
30 | 41 | {{/* Fallback img tag with alt text and lazy loading */}} |
31 | 42 | <img |
32 | | - src="{{ $jpegFull.Permalink }}" |
| 43 | + src="{{ $fallback.Permalink }}" |
33 | 44 | alt="{{ $alt | plainify }}" |
34 | | - width="{{ $jpegFull.Width }}" |
35 | | - height="{{ $jpegFull.Height }}" |
| 45 | + width="{{ $fallback.Width }}" |
| 46 | + height="{{ $fallback.Height }}" |
| 47 | + sizes="{{ $sizesAttr }}" |
36 | 48 | loading="lazy" |
37 | 49 | decoding="async" |
38 | | - /> |
| 50 | + /> |
39 | 51 | </picture> |
| 52 | + |
40 | 53 | {{/* Show figcaption if alt is set and not an avatar */}} |
41 | 54 | {{ if and $alt (not $isAvatar) }} |
42 | 55 | <figcaption>CC: {{ $alt }}</figcaption> |
|
0 commit comments