feat: add responsive image support (#1283)
* feat: create responsive-image helper * feat: new responsive-image helper * feat: Implement responsive image rendering in page content using a new partial and configure image processing widths. * feat: add `sizes` attribute for enhanced image responsiveness * feat: add responsive image to page header too * Check for `.Enabled` before processing image * style: remove wrong article-list--tile width and height * feat: add `helper/thumbnail-image` for responsive thumbnails * feat: enable content image processing and update responsive image `sizes` attributes for improved display. * fix: get src from the attributes so external image can work correctly * fix: add missing data-title-escaped * Update layouts/_partials/article/components/header.html Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * feat: remove default `true` for thumbnail resize parameter. * Update layouts/_markup/render-image.html Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update layouts/_partials/helper/thumbnail-image.html Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add original width and height to thumbnail pictures, for case that resize is not enabled * Update layouts/_partials/helper/thumbnail-image.html Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -207,8 +207,8 @@
|
|||||||
border-radius: var(--card-border-radius);
|
border-radius: var(--card-border-radius);
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
position: relative;
|
position: relative;
|
||||||
height: 350px;
|
|
||||||
width: 250px;
|
width: 250px;
|
||||||
|
height: 150px;
|
||||||
box-shadow: var(--shadow-l1);
|
box-shadow: var(--shadow-l1);
|
||||||
transition: box-shadow 0.3s ease;
|
transition: box-shadow 0.3s ease;
|
||||||
background-color: var(--card-background);
|
background-color: var(--card-background);
|
||||||
|
|||||||
@@ -242,8 +242,6 @@
|
|||||||
margin-right: 15px;
|
margin-right: 15px;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
width: 250px;
|
|
||||||
height: 150px;
|
|
||||||
|
|
||||||
.article-title {
|
.article-title {
|
||||||
font-size: 1.8rem;
|
font-size: 1.8rem;
|
||||||
|
|||||||
@@ -56,8 +56,6 @@
|
|||||||
width: max-content;
|
width: max-content;
|
||||||
|
|
||||||
article {
|
article {
|
||||||
width: 250px;
|
|
||||||
height: 150px;
|
|
||||||
margin-right: 20px;
|
margin-right: 20px;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
|
|
||||||
|
|||||||
@@ -75,10 +75,12 @@ SortBy = "default"
|
|||||||
toggle = true
|
toggle = true
|
||||||
default = "auto"
|
default = "auto"
|
||||||
|
|
||||||
[imageProcessing.cover]
|
[imageProcessing]
|
||||||
|
[imageProcessing.content]
|
||||||
|
widths = [800, 1600, 2400]
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|
||||||
[imageProcessing.content]
|
[imageProcessing.thumbnail]
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|
||||||
# GDPR Cookie Consent Configuration
|
# GDPR Cookie Consent Configuration
|
||||||
|
|||||||
@@ -2,23 +2,29 @@
|
|||||||
{{- $alt := .PlainText | safeHTML -}}
|
{{- $alt := .PlainText | safeHTML -}}
|
||||||
{{- $title := .Title | markdownify -}}
|
{{- $title := .Title | markdownify -}}
|
||||||
|
|
||||||
{{/* SVG and external images won't work with gallery layout, because their width and height attributes are unknown */}}
|
{{/* There are some types of images that don't have width and height attributes, such as SVG */}}
|
||||||
{{- $galleryImage := and $image.Height $image.Width -}}
|
{{- $galleryImage := and $image.Height $image.Width -}}
|
||||||
|
|
||||||
<img src="{{ $image.Permalink }}"
|
{{- $attributes := dict -}}
|
||||||
{{ with $image.Width }}width="{{ . }}"{{ end }}
|
{{- if $galleryImage -}}
|
||||||
{{ with $image.Height }}height="{{ . }}"{{ end }}
|
{{- $attributes = (dict
|
||||||
loading="lazy"
|
"data-flex-grow" (div (mul $image.Width 100) $image.Height)
|
||||||
{{ with $alt }}
|
"data-flex-basis" (printf "%dpx" (div (mul $image.Width 240) $image.Height))
|
||||||
alt="{{ . }}"
|
) -}}
|
||||||
{{ end }}
|
{{- end -}}
|
||||||
{{ with $title }}
|
|
||||||
title="{{ . }}"
|
{{ partial "helper/responsive-image" (dict
|
||||||
data-title-escaped="{{ . | htmlEscape }}"
|
"Resource" $image.Resource
|
||||||
{{ end }}
|
"Widths" (cond .Page.Site.Params.ImageProcessing.Content.Enabled .Page.Site.Params.ImageProcessing.Content.Widths nil)
|
||||||
{{ if $galleryImage }}
|
"Attributes" (merge $attributes (dict
|
||||||
class="gallery-image"
|
"src" $image.Permalink
|
||||||
data-flex-grow="{{ div (mul $image.Width 100) $image.Height }}"
|
"width" $image.Width
|
||||||
data-flex-basis="{{ div (mul $image.Width 240) $image.Height }}px"
|
"height" $image.Height
|
||||||
{{ end }}
|
"alt" $alt
|
||||||
>
|
"title" $title
|
||||||
|
"data-title-escaped" ($title | htmlEscape)
|
||||||
|
"class" (cond $galleryImage "gallery-image" "")
|
||||||
|
"loading" "lazy"
|
||||||
|
"sizes" "(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px"
|
||||||
|
))
|
||||||
|
) }}
|
||||||
@@ -14,8 +14,19 @@
|
|||||||
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
||||||
{{ if $image }}
|
{{ if $image }}
|
||||||
<div class="article-image">
|
<div class="article-image">
|
||||||
<img src="{{ $image.Permalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" alt="{{ .Title }}"
|
{{ partial "helper/thumbnail-image" (dict
|
||||||
loading="lazy">
|
"Resource" $image.Resource
|
||||||
|
"Width" 60
|
||||||
|
"Height" 60
|
||||||
|
"Resize" .Site.Params.ImageProcessing.Thumbnail.Enabled
|
||||||
|
"Attributes" (dict
|
||||||
|
"src" $image.Permalink
|
||||||
|
"alt" .Title
|
||||||
|
"loading" "lazy"
|
||||||
|
"width" $image.Width
|
||||||
|
"height" $image.Height
|
||||||
|
)
|
||||||
|
) }}
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
@@ -3,8 +3,19 @@
|
|||||||
<a href="{{ .context.RelPermalink }}">
|
<a href="{{ .context.RelPermalink }}">
|
||||||
{{ if $image }}
|
{{ if $image }}
|
||||||
<div class="article-image">
|
<div class="article-image">
|
||||||
<img src="{{ $image.Permalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" loading="lazy"
|
{{ partial "helper/thumbnail-image" (dict
|
||||||
alt="Featured image of post {{ .context.Title }}">
|
"Resource" $image.Resource
|
||||||
|
"Width" 250
|
||||||
|
"Height" 150
|
||||||
|
"Resize" .context.Site.Params.ImageProcessing.Thumbnail.Enabled
|
||||||
|
"Attributes" (dict
|
||||||
|
"src" $image.Permalink
|
||||||
|
"alt" (printf "Featured image of post %s" .context.Title)
|
||||||
|
"loading" "lazy"
|
||||||
|
"width" $image.Width
|
||||||
|
"height" $image.Height
|
||||||
|
)
|
||||||
|
) }}
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,18 @@
|
|||||||
{{ if $image }}
|
{{ if $image }}
|
||||||
<div class="article-image">
|
<div class="article-image">
|
||||||
<a href="{{ $Page.RelPermalink }}">
|
<a href="{{ $Page.RelPermalink }}">
|
||||||
<img src="{{ $image.Permalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" loading="lazy"
|
{{ partial "helper/responsive-image" (dict
|
||||||
alt="Featured image of post {{ $Page.Title }}" />
|
"Resource" $image.Resource
|
||||||
|
"Widths" (cond $Page.Site.Params.ImageProcessing.Content.Enabled $Page.Site.Params.ImageProcessing.Content.Widths nil)
|
||||||
|
"Attributes" (dict
|
||||||
|
"src" $image.Permalink
|
||||||
|
"width" $image.Width
|
||||||
|
"height" $image.Height
|
||||||
|
"alt" (printf "Featured image of post %s" $Page.Title)
|
||||||
|
"loading" "lazy"
|
||||||
|
"sizes" "(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px"
|
||||||
|
)
|
||||||
|
) }}
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
{{- /*
|
||||||
|
Params:
|
||||||
|
Resource: Hugo image resource object (Optional, for processing)
|
||||||
|
Widths: Slice of widths to generate for srcset
|
||||||
|
Attributes: Map of HTML attributes for the <img> tag
|
||||||
|
*/ -}}
|
||||||
|
|
||||||
|
{{- $resource := .Resource -}}
|
||||||
|
{{- $widths := .Widths -}}
|
||||||
|
{{- $attributes := .Attributes | default dict -}}
|
||||||
|
|
||||||
|
{{/* Generate srcset if Resource and Widths are provided */}}
|
||||||
|
{{- if and $widths $resource (reflect.IsImageResourceProcessable $resource) -}}
|
||||||
|
{{- $srcset := slice -}}
|
||||||
|
{{- range $widths -}}
|
||||||
|
{{- if lt . $resource.Width -}}
|
||||||
|
{{- $resized := $resource.Resize (printf "%dx" .) -}}
|
||||||
|
{{- $srcset = $srcset | append (printf "%s %dw" $resized.RelPermalink .) -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
{{- if gt (len $srcset) 0 -}}
|
||||||
|
{{- $permalink := default $resource.RelPermalink (index $attributes "src") -}}
|
||||||
|
{{- $srcset = $srcset | append (printf "%s %dw" $permalink $resource.Width) -}}
|
||||||
|
{{- $attributes = merge $attributes (dict "srcset" (delimit $srcset ", ")) -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
<img {{- range $k, $v := $attributes -}}
|
||||||
|
{{- if $v -}}
|
||||||
|
{{- printf " %s=%q" (lower $k) (printf "%v" $v) | safeHTMLAttr -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}>
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
{{- /*
|
||||||
|
Params:
|
||||||
|
Resource: Hugo image resource object (Optional, for processing)
|
||||||
|
Width: Image width (Required)
|
||||||
|
Height: Image height (Required)
|
||||||
|
Resize: Whether to perform resize (Optional, default: false)
|
||||||
|
Attributes: Map of HTML attributes for the <img> tag
|
||||||
|
*/ -}}
|
||||||
|
|
||||||
|
{{- $resource := .Resource -}}
|
||||||
|
{{- $width := .Width -}}
|
||||||
|
{{- $height := .Height -}}
|
||||||
|
{{- $resize := .Resize -}}
|
||||||
|
{{- $attributes := .Attributes | default dict -}}
|
||||||
|
|
||||||
|
{{- if and $resize $resource (reflect.IsImageResourceProcessable $resource) $width $height -}}
|
||||||
|
{{/* Create thumbnail with 1x/2x descriptors */}}
|
||||||
|
{{- $srcset := slice -}}
|
||||||
|
{{- range (slice 1 2) -}}
|
||||||
|
{{- $w := mul $width . -}}
|
||||||
|
{{- $h := mul $height . -}}
|
||||||
|
{{- if and (le $w $resource.Width) (le $h $resource.Height) -}}
|
||||||
|
{{- $resized := $resource.Fill (printf "%dx%d" $w $h) -}}
|
||||||
|
{{- $srcset = $srcset | append (printf "%s %dx" $resized.RelPermalink .) -}}
|
||||||
|
|
||||||
|
{{- if eq . 1 -}}
|
||||||
|
{{- $attributes = merge $attributes (dict "src" $resized.RelPermalink) -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
{{- if gt (len $srcset) 0 -}}
|
||||||
|
{{- $attributes = merge $attributes (dict "width" $width "height" $height "srcset" (delimit $srcset ", ")) -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
<img {{- range $k, $v := $attributes -}}
|
||||||
|
{{- if $v -}}
|
||||||
|
{{- printf " %s=%q" (lower $k) (printf "%v" $v) | safeHTMLAttr -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- end -}}>
|
||||||
Reference in New Issue
Block a user