add base image location

This commit is contained in:
2025-08-23 22:54:08 -02:30
parent 72183e14a3
commit adb7e7ac3f
9 changed files with 69 additions and 58 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
module github.com/hugo-mods/lazyimg module git.agrajag.cloud/kova/lazyimg
go 1.13 go 1.13
require github.com/aFarkas/lazysizes v0.0.0-20210408151554-27c7ceb7d737 // indirect require github.com/aFarkas/lazysizes v0.0.0-20210517092641-1523a4ff4579 // indirect
+2
View File
@@ -1,2 +1,4 @@
github.com/aFarkas/lazysizes v0.0.0-20210408151554-27c7ceb7d737 h1:0rEJGkemFAVemGpxArHHkICg/XhP6parOV4vnnjT174= github.com/aFarkas/lazysizes v0.0.0-20210408151554-27c7ceb7d737 h1:0rEJGkemFAVemGpxArHHkICg/XhP6parOV4vnnjT174=
github.com/aFarkas/lazysizes v0.0.0-20210408151554-27c7ceb7d737/go.mod h1:xl3heUINAn0oPowV1biDQ2Pyw1w3x66I7KEbWkaxox0= github.com/aFarkas/lazysizes v0.0.0-20210408151554-27c7ceb7d737/go.mod h1:xl3heUINAn0oPowV1biDQ2Pyw1w3x66I7KEbWkaxox0=
github.com/aFarkas/lazysizes v0.0.0-20210517092641-1523a4ff4579 h1:K8ukOfj0MOBEWWsL8Mzd8GXIbAf7/RHnX1hipqbjeto=
github.com/aFarkas/lazysizes v0.0.0-20210517092641-1523a4ff4579/go.mod h1:xl3heUINAn0oPowV1biDQ2Pyw1w3x66I7KEbWkaxox0=
+1
View File
@@ -16,6 +16,7 @@
"img" . "img" .
"resizer" (default "responsive" site.Params.lazyimg.resizer) "resizer" (default "responsive" site.Params.lazyimg.resizer)
"renderer" (default "lqip" site.Params.lazyimg.renderer) "renderer" (default "lqip" site.Params.lazyimg.renderer)
"imageBasePath" (default "/images/lazyimg" site.Params.lazyimg.imageBasePath)
)}} )}}
{{ if reflect.IsMap . }} {{ if reflect.IsMap . }}
{{ $userParams := dict }} {{ $userParams := dict }}
+5 -4
View File
@@ -1,8 +1,9 @@
{{/* Input: Input: dict {"responsive": [RESOURCE, ...]}. Output: srcset string */}} {{/* Input: dict {"responsive": [RESOURCE, ...], "imageBasePath": "/images/lazyimg"} */}}
{{ $images := .responsive }} {{ $images := .responsive }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
{{ $srcset := slice }} {{ $srcset := slice }}
{{ range $images }} {{ range $images }}
{{ $srcset = append (printf "%s %dw" .Permalink .Width) $srcset }} {{ $u := path.Join $base (strings.TrimPrefix "/" .RelPermalink) }}
{{ $srcset = append (printf "%s %dw" $u .Width) $srcset }}
{{ end }} {{ end }}
{{ return (delimit $srcset ",\n") }} {{ return (delimit $srcset ",\n") }}
+15 -13
View File
@@ -3,17 +3,19 @@
{{ $imgDict := .imgDict }} {{ $imgDict := .imgDict }}
{{ $imgDictFallback := .imgDictFallback }} {{ $imgDictFallback := .imgDictFallback }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
<picture> <picture>
<source type="image/webp" <source type="image/webp"
{{- with $imgDict.lqip -}} {{ with $imgDict.lqip -}}
{{- $placeholderB64 := .Content | base64Encode -}} {{ $placeholderB64 := .Content | base64Encode }}
{{- if gt (len $placeholderB64) 2048 }} {{ if gt (len $placeholderB64) 2048 }}
srcset="{{ .Permalink }}" {{ $lqipURL := path.Join $base (strings.TrimPrefix "/" .RelPermalink) }}
{{- else }} srcset="{{ $lqipURL }}"
srcset="data:{{ .MediaType }};base64,{{ $placeholderB64 }}" {{ else }}
{{- end }} srcset="data:{{ .MediaType }};base64,{{ $placeholderB64 }}"
{{- end -}} {{ end }}
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}} {{- end }}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}}> {{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{ partial "renderer/lqip" (merge . (dict "imgDict" $imgDictFallback)) }} {{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} data-sizes="auto" data-srcset="{{ . }}"{{ end }}{{ end }}>
</picture> {{ partial "renderer/lqip" (merge . (dict "imgDict" $imgDictFallback "imageBasePath" $base)) }}
</picture>
+21 -20
View File
@@ -2,24 +2,25 @@
{{ $alt := .alt }} {{ $alt := .alt }}
{{ $noscriptSupport := .noscript }} {{ $noscriptSupport := .noscript }}
{{ $imgDict := .imgDict }} {{ $imgDict := .imgDict }}
{{- if $noscriptSupport -}} {{ if $noscriptSupport }}<noscript>{{ partial "renderer/native" . }}</noscript>{{ end }}
<noscript>
{{ partial "renderer/native" . }} {{ $base := default "/images/lazyimg" .imageBasePath }}
</noscript> {{ $defaultURL := path.Join $base (strings.TrimPrefix "/" $imgDict.default.RelPermalink) }}
{{- end -}}
<img <img
{{- with $imgDict.lqip -}} {{ with $imgDict.lqip -}}
{{- $placeholderB64 := .Content | base64Encode -}} {{ $placeholderB64 := .Content | base64Encode }}
{{- if gt (len $placeholderB64) 2048 }} {{ if gt (len $placeholderB64) 2048 }}
src="{{ .Permalink }}" {{ $lqipURL := path.Join $base (strings.TrimPrefix "/" .RelPermalink) }}
{{- else }} src="{{ $lqipURL }}"
src="data:{{ .MediaType }};base64,{{ $placeholderB64 }}" {{ else }}
{{- end }} src="data:{{ .MediaType }};base64,{{ $placeholderB64 }}"
data-src="{{ $imgDict.default.Permalink }}" {{ end }}
class="{{ with $class }}{{ . }} {{ end }}blur-up lazyload" data-src="{{ $defaultURL }}"
class="{{ with $class }}{{ . }} {{ end }}blur-up lazyload"
{{ else }} {{ else }}
src="{{ $imgDict.default.Permalink }}" class="{{ $class }} lazyload" src="{{ $defaultURL }}" class="{{ $class }} lazyload"
{{- end -}} {{ end -}}
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}} {{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}} {{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} data-sizes="auto" data-srcset="{{ . }}"{{ end }}{{ end }}
{{- with $alt }} alt="{{ . }}" {{- end }}> {{ with $alt }} alt="{{ . }}" {{ end }}>
+8 -5
View File
@@ -2,8 +2,11 @@
{{ $alt := .alt }} {{ $alt := .alt }}
{{ $imgDict := .imgDict }} {{ $imgDict := .imgDict }}
<img src="{{ $imgDict.default.Permalink }}" {{ $base := default "/images/lazyimg" .imageBasePath }}
class="{{ $class }}" loading="lazy" {{ $src := path.Join $base (strings.TrimPrefix "/" $imgDict.default.RelPermalink) }}
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}}
{{- if $imgDict.responsive -}} {{ with partial "renderer/_srcset" $imgDict }} srcset="{{ . }}"{{- end }} {{- end -}} <img src="{{ $src }}"
{{- with $alt }} alt="{{ . }}" {{- end }}> class="{{ $class }}" loading="lazy"
{{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} srcset="{{ . }}"{{ end }}{{ end }}
{{ with $alt }} alt="{{ . }}" {{ end }}>
+9 -9
View File
@@ -2,12 +2,12 @@
{{ $alt := .alt }} {{ $alt := .alt }}
{{ $noscriptSupport := .noscript }} {{ $noscriptSupport := .noscript }}
{{ $imgDict := .imgDict }} {{ $imgDict := .imgDict }}
{{- if $noscriptSupport -}} {{ if $noscriptSupport }}<noscript>{{ partial "renderer/native" . }}</noscript>{{ end }}
<noscript>
{{ partial "renderer/native" . }} {{ $base := default "/images/lazyimg" .imageBasePath }}
</noscript> {{ $dataSrc := path.Join $base (strings.TrimPrefix "/" $imgDict.default.RelPermalink) }}
{{- end -}}
<img data-src="{{ $imgDict.default.Permalink }}" class="{{ $class }} lazyload" <img data-src="{{ $dataSrc }}" class="{{ $class }} lazyload"
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}} {{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}} {{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} data-sizes="auto" data-srcset="{{ . }}"{{ end }}{{ end }}
{{- with $alt }} alt="{{ . }}" {{- end }}> {{ with $alt }} alt="{{ . }}" {{ end }}>
+6 -5
View File
@@ -3,9 +3,10 @@
{{ $imgDict := .imgDict }} {{ $imgDict := .imgDict }}
{{ $imgDictFallback := .imgDictFallback }} {{ $imgDictFallback := .imgDictFallback }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
<picture> <picture>
<source type="image/webp" <source type="image/webp"
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}} {{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}}> {{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} data-sizes="auto" data-srcset="{{ . }}"{{ end }}{{ end }}>
{{ partial "renderer/simple" (merge . (dict "imgDict" $imgDictFallback)) }} {{ partial "renderer/simple" (merge . (dict "imgDict" $imgDictFallback "imageBasePath" $base)) }}
</picture> </picture>