I’ve been working on a gallery shortcode that will range through all images in the page bundle, process them and give them a title by parsing the file name. I also want the processed images to be exported with the original image’s file name. I’ve managed to get the desired path but the resulting images are not being processed at all.
Here’s what I have so far:
{{- $filters := slice
(images.Process "resize 400x")
(images.Process "jpg")
-}}
<section class="media-cards">
{{- $permalink := .Page.RelPermalink -}}
{{- range .Page.Resources.Match "images/*" -}}
{{- $path := urls.JoinPath $permalink .Name -}}
{{- $title := .Key | path.BaseName | humanize | title -}}
{{- with . | images.Filter $filters | resources.Copy $path -}}
<figure class="media-card">
<img
src="{{ .Permalink }}"
alt="{{ $title }}"
width="{{ .Width }}"
height="{{ .Height }}" />
<figcaption>
<span>{{ $title }}</span>
</figcaption>
</figure>
{{- end -}}
{{- end -}}
</section>
The main motivation behind using resources.Copy is that I don’t want both images (processed and unprocessed) to be pushed to the build directory. I know I could use the assets folder but I’d also like to do something similar to the image render hook:
{{- $filters := slice
(images.Process "resize 800x")
(images.Process "jpg")
-}}
{{- $imgTitle := .Title -}}
{{- $altText := .Text -}}
{{- $permalink := .Page.RelPermalink -}}
{{- $path :=
urls.JoinPath $permalink .Destination | urlize | safeHTML
-}}
{{- with .Page.Resources.Get .Destination -}}
{{- with . | images.Filter $filters -}}
{{- with . | resources.Copy $path -}}
{{- if $imgTitle -}}
<figure>
<img
src="{{ .Permalink }}"
alt="{{ $altText }}"
width="{{ .Width }}"
height="{{ .Height }}" />
<figcaption>
<p>{{ $imgTitle }}</p>
</figcaption>
</figure>
{{- else -}}
<img
src="{{ .Permalink }}"
alt="{{ $altText }}"
width="{{ .Width }}"
height="{{ .Height }}" />
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
I’m not quite sure what I’m doing wrong or if there’s a less clunky way of doing it, so I’d appreciate any help.