images.Process - is there a way to influence the name of the generated images

I’m generating several sizes for responsive images in <source> <srcset>... and would like to add a little hint to the file name. At the moment my images on the server look like this: “opengraph_hu_9495d926bfe3cefb.png”.
I would like to add -min, -mid, -max at the end of the hash. Is this directly possible with images.Process?

Following up on @chrillek’s guidance, with a page resource you can do something like this:

{{ with .Resources.GetMatch "**" }}
	{{ with .Process "resize 200x" }}
		{{ $oldBaseName := path.BaseName .RelPermalink }}
		{{ $newBaseName := printf "%s-min.%s" $oldBaseName .MediaType.FirstSuffix.Suffix }}
		{{ $publicationPath := urls.JoinPath $.RelPermalink $newBaseName }}
		{{ with resources.Copy $publicationPath . }}
			<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
		{{ end }}
	{{ end }}
{{ end }}

Obviously, this is a bit ugly.

Notes:

  • You will have to modify the above if you have set uglyURLs to true in your project configuration.
  • Calling .RelPermalink in line 3 triggers publication of the intermediate resource, which may or may not be a problem for you.
public/
├── posts/
│   ├── post-1/
│   │   ├── a.jpg                         <-- orignal resource
│   │   ├── a_hu_bcee031eb7234145-min.jpg <-- renamed image
│   │   ├── a_hu_bcee031eb7234145.jpg     <-- intermediate resource
│   │   └── index.html
│   └── index.html
├── favicon.ico
└── index.html

You can disable publication of the original resource in your front matter:

[build]
  publishResources = false

Or you can cascade that value down to all or a subset of pages from your project configuration. See configure casade in the documentation.

But note that you don’t want to disable publication of the original resource if you reference the images in Markdown, unless you have enabled the embedded image render hook (or something similar).

if

  • the page is backed by a file
  • you dont play with permalinks
  • you want to publish relative to the page

you may construct the target path based on .Path, which supresses the intermediate to get published.

maybe a .GetPermalink method would be nice to just get the info without publishing?

https://github.com/gohugoio/hugo/issues/12143