Regarding resources.Copy, image processing and page bundles

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.

Out of curiosity, why?

I worded it poorly, as I don’t really care about the processed image title, what I actually want is for the unprocessed image to not be exported to the public build. The reason for that is I don’t want unused images occupying unnecessary space.

I managed to solve the short code issue with:

{{- $filters := slice
    (images.Process "resize 400x")
    (images.Process "jpg")
-}}
<section class="media-cards">
    {{- range .Page.Resources.Match "images/*" -}}
        {{- $title := .Key | path.BaseName | humanize  | title -}}
        {{- $title := replaceRE " Original" "$1" $title -}}
        {{- with  . | images.Filter $filters -}}
            <figure class="media-card">
                <img
                    src="{{ .Permalink }}"
                    aria-hidden="true"
                    alt=""
                    width="{{ .Width }}"
                    height="{{ .Height }}" />
                <figcaption>
                    <span>{{ $title }}</span>
                </figcaption>
            </figure>
        {{- end -}}
    {{- end -}}
</section>

The render hook I still haven’t figured out.

You can use build options to prevent publishing resources:
https://gohugo.io/content-management/build-options/

You might cascade the value down from your site configuration.

Within an image render hook or shortcode, the image will be published when you invoke any of the following methods: Permalink, RelPermalink, or Publish. So if you’re going to use resources.Copy, make sure you don’t call any of those methods before you copy.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.