# How to publish unmodified image with hash in filename

**URL:** <https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573>\
**Category:** support\
**Created:** [February 17, 2025, 8:36am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573 "2025-02-17T08:36:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asg7](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Asg7](https://discourse.gohugo.io/u/Asg7)\
**Post date:** [February 17, 2025, 8:36am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/1 "2025-02-17T08:36:27Z")

</div>

Hello all,

I need a hashed filename for unmodified asset images like the ones modified get (xxhash). An example with the main question in the _else_ branch.

```auto
{{- with resources.GetMatch (printf "portraits/%s.jpg" .) -}}
	{{- $image := . -}}
	{{- if gt $image.Height 40 }}
		{{- $image = $image.Fit "40x40" -}}
	{{- else -}}
		<!-- What should go here to get a hashed filename for the unmodified image? -->
	{{- end -}}
	<img src="{{ $image.RelPermalink }}">
{{- end -}}

```

So if the image is greater that 40px it gets a filename in the public folder with an xxhash in the name. But if it’s 40 or smaller the filename will be the same as in the assets folder. I need both with a hash in the name.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Asg7](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Asg7](https://discourse.gohugo.io/u/Asg7)\
**Post date:** [February 17, 2025, 8:39am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/2 "2025-02-17T08:39:48Z")

</div>

The following does the job in the else branch, but I don’t think this is a good solution as I don’t know what that does with the image and definitely changes the file:

```auto
{{- $image = $image.Process "" -}}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 17, 2025, 8:58am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/3 "2025-02-17T08:58:39Z")

</div>

```plaintext
{{ with $i := resources.Get "a.jpg" }}
  {{ if gt .Height 40 }}
    {{ $i = .Fit "40x40" -}}
  {{ else }}
    {{ $newFileName := printf "%s.%s" (hash.XxHash .Name) (.MediaType.FirstSuffix.Suffix) }}
    {{ $i = resources.Copy $newFileName . }}
  {{ end }}
  <img src="{{ $i.RelPermalink }}">
{{ end }}

```

[https://gohugo.io/functions/resources/copy/](https://gohugo.io/functions/resources/copy/)

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 17, 2025, 9:46am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/4 "2025-02-17T09:46:26Z")

</div>

Upon reflection, it would be better to hash the content.

```plaintext
{{ with $i := resources.Get "a.jpg" }}
  {{ if gt .Height 500 }}
    {{ $i = .Fit "40x40" -}}
  {{ else }}
    {{ $newFileName := printf "%s.%s" (hash.XxHash .Content) (.MediaType.FirstSuffix.Suffix) }}
    {{ $i = resources.Copy $newFileName . }}
  {{ end }}
  <img src="{{ $i.RelPermalink }}">
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![Asg7](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Asg7](https://discourse.gohugo.io/u/Asg7)\
**Post date:** [February 17, 2025, 10:07am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/5 "2025-02-17T10:07:24Z")

</div>

Thank you! It is quite close. But the built in “renamer” puts the file into the correct subfolder within public and adds language code to to the original filename. So it generates `[filename]_[lang]_[hash].[extension]`

I understand now that it can be programmed into this example. But isn’t there some config variable or method to use this filename creation template to all files copied/created into the public folder from assets?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 17, 2025, 10:08am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/6 "2025-02-17T10:08:44Z")

</div>

> [@Asg7](#):
>
> isn’t there

No, there is not.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 17, 2025, 10:13am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/7 "2025-02-17T10:13:48Z")

</div>

> [@Asg7](#):
>
> [filename]_[lang]_[hash].[extension]

The “hu” stands for “Hugo” not " Hungarian"… language isn’t part of the equation here. Just a coincidence.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 19, 2025, 10:13am UTC](https://discourse.gohugo.io/t/how-to-publish-unmodified-image-with-hash-in-filename/53573/8 "2025-02-19T10:13:55Z")

</div>

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