Image shortcodes now resize images to reduce file size

This commit is contained in:
Joe Hutchinson
2019-07-20 22:03:39 -04:00
parent 0a27447f4b
commit b31a96f27a
2 changed files with 46 additions and 18 deletions

View File

@@ -1,7 +1,14 @@
<picture>
{{ if (fileExists (printf "%s/%s" "content" (replace (index .Params "src") (path.Ext (index .Params "src")) ".webp"))) -}}
<source srcset="{{ replace (index .Params "src") (path.Ext (index .Params "src")) ".webp" | absURL }}" type="image/webp">
{{ if (fileExists (printf "%s/%s" "content" (replace (.Get "src") (path.Ext (.Get "src")) ".webp"))) -}}
<source srcset="{{ replace (.Get "src") (path.Ext (.Get "src")) ".webp" | absURL }}" type="image/webp">
{{- end }}
<source srcset="{{ index .Params "src" | absURL }}">
<img src="{{ index .Params "src" | absURL }}" {{ if isset .Params "alt" -}} alt="{{ index .Params "alt"}}" {{- end }}>
{{ $image := $.Page.Resources.GetMatch (.Get "src") }}
{{ if gt $image.Width 800 -}}
{{ $resized := $image.Resize "800x Lanczos" }}
<source srcset="{{ $resized.Permalink }}">
<img src="{{ $resized.Permalink }}" {{ if isset .Params "alt" -}} alt="{{ .Get "alt"}}" {{- end }}>
{{ else }}
<source srcset="{{ $image.Permalink }}">
<img src="{{ $image.Permalink }}" {{ if isset .Params "alt" -}} alt="{{ .Get "alt"}}" {{- end }}>
{{- end}}
</picture>