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
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/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" .
"resizer" (default "responsive" site.Params.lazyimg.resizer)
"renderer" (default "lqip" site.Params.lazyimg.renderer)
"imageBasePath" (default "/images/lazyimg" site.Params.lazyimg.imageBasePath)
)}}
{{ if reflect.IsMap . }}
{{ $userParams := dict }}
+4 -3
View File
@@ -1,8 +1,9 @@
{{/* Input: Input: dict {"responsive": [RESOURCE, ...]}. Output: srcset string */}}
{{/* Input: dict {"responsive": [RESOURCE, ...], "imageBasePath": "/images/lazyimg"} */}}
{{ $images := .responsive }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
{{ $srcset := slice }}
{{ 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 }}
{{ return (delimit $srcset ",\n") }}
+11 -9
View File
@@ -3,17 +3,19 @@
{{ $imgDict := .imgDict }}
{{ $imgDictFallback := .imgDictFallback }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
<picture>
<source type="image/webp"
{{- with $imgDict.lqip -}}
{{- $placeholderB64 := .Content | base64Encode -}}
{{- if gt (len $placeholderB64) 2048 }}
srcset="{{ .Permalink }}"
{{- else }}
{{ with $imgDict.lqip -}}
{{ $placeholderB64 := .Content | base64Encode }}
{{ if gt (len $placeholderB64) 2048 }}
{{ $lqipURL := path.Join $base (strings.TrimPrefix "/" .RelPermalink) }}
srcset="{{ $lqipURL }}"
{{ else }}
srcset="data:{{ .MediaType }};base64,{{ $placeholderB64 }}"
{{ end }}
{{- end }}
{{- end -}}
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}}>
{{ partial "renderer/lqip" (merge . (dict "imgDict" $imgDictFallback)) }}
{{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} data-sizes="auto" data-srcset="{{ . }}"{{ end }}{{ end }}>
{{ partial "renderer/lqip" (merge . (dict "imgDict" $imgDictFallback "imageBasePath" $base)) }}
</picture>
+18 -17
View File
@@ -2,24 +2,25 @@
{{ $alt := .alt }}
{{ $noscriptSupport := .noscript }}
{{ $imgDict := .imgDict }}
{{- if $noscriptSupport -}}
<noscript>
{{ partial "renderer/native" . }}
</noscript>
{{- end -}}
{{ if $noscriptSupport }}<noscript>{{ partial "renderer/native" . }}</noscript>{{ end }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
{{ $defaultURL := path.Join $base (strings.TrimPrefix "/" $imgDict.default.RelPermalink) }}
<img
{{- with $imgDict.lqip -}}
{{- $placeholderB64 := .Content | base64Encode -}}
{{- if gt (len $placeholderB64) 2048 }}
src="{{ .Permalink }}"
{{- else }}
{{ with $imgDict.lqip -}}
{{ $placeholderB64 := .Content | base64Encode }}
{{ if gt (len $placeholderB64) 2048 }}
{{ $lqipURL := path.Join $base (strings.TrimPrefix "/" .RelPermalink) }}
src="{{ $lqipURL }}"
{{ else }}
src="data:{{ .MediaType }};base64,{{ $placeholderB64 }}"
{{- end }}
data-src="{{ $imgDict.default.Permalink }}"
{{ end }}
data-src="{{ $defaultURL }}"
class="{{ with $class }}{{ . }} {{ end }}blur-up lazyload"
{{ else }}
src="{{ $imgDict.default.Permalink }}" class="{{ $class }} lazyload"
{{- end -}}
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}}
{{- with $alt }} alt="{{ . }}" {{- end }}>
src="{{ $defaultURL }}" class="{{ $class }} lazyload"
{{ end -}}
{{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} data-sizes="auto" data-srcset="{{ . }}"{{ end }}{{ end }}
{{ with $alt }} alt="{{ . }}" {{ end }}>
+8 -5
View File
@@ -2,8 +2,11 @@
{{ $alt := .alt }}
{{ $imgDict := .imgDict }}
<img src="{{ $imgDict.default.Permalink }}"
class="{{ $class }}" loading="lazy"
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}}
{{- if $imgDict.responsive -}} {{ with partial "renderer/_srcset" $imgDict }} srcset="{{ . }}"{{- end }} {{- end -}}
{{- with $alt }} alt="{{ . }}" {{- end }}>
{{ $base := default "/images/lazyimg" .imageBasePath }}
{{ $src := path.Join $base (strings.TrimPrefix "/" $imgDict.default.RelPermalink) }}
<img src="{{ $src }}"
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 }}
{{ $noscriptSupport := .noscript }}
{{ $imgDict := .imgDict }}
{{- if $noscriptSupport -}}
<noscript>
{{ partial "renderer/native" . }}
</noscript>
{{- end -}}
<img data-src="{{ $imgDict.default.Permalink }}" class="{{ $class }} lazyload"
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}}
{{- with $alt }} alt="{{ . }}" {{- end }}>
{{ if $noscriptSupport }}<noscript>{{ partial "renderer/native" . }}</noscript>{{ end }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
{{ $dataSrc := path.Join $base (strings.TrimPrefix "/" $imgDict.default.RelPermalink) }}
<img data-src="{{ $dataSrc }}" class="{{ $class }} lazyload"
{{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ end }}
{{ if $imgDict.responsive }}{{ with partial "renderer/_srcset" (merge $imgDict (dict "imageBasePath" $base)) }} data-sizes="auto" data-srcset="{{ . }}"{{ end }}{{ end }}
{{ with $alt }} alt="{{ . }}" {{ end }}>
+4 -3
View File
@@ -3,9 +3,10 @@
{{ $imgDict := .imgDict }}
{{ $imgDictFallback := .imgDictFallback }}
{{ $base := default "/images/lazyimg" .imageBasePath }}
<picture>
<source type="image/webp"
{{- with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{- end -}}
{{- if $imgDict.responsive -}}{{ with partial "renderer/_srcset" $imgDict }} data-sizes="auto" data-srcset="{{ . }}"{{- end }} {{- end -}}>
{{ partial "renderer/simple" (merge . (dict "imgDict" $imgDictFallback)) }}
{{ with $imgDict.default }} height="{{ .Height }}" width="{{ .Width }}" {{ 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 "imageBasePath" $base)) }}
</picture>