# How to trim extention of image file?

**URL:** https://discourse.gohugo.io/t/how-to-trim-extention-of-image-file/38673
**Category:** support
**Created:** [May 18, 2022, 6:55pm UTC](https://discourse.gohugo.io/t/how-to-trim-extention-of-image-file/38673 "2022-05-18T18:55:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dugajimi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dugajimi/32/11698_2.png) [@dugajimi](https://discourse.gohugo.io/u/dugajimi)
#### Post date: [May 18, 2022, 6:55pm UTC](https://discourse.gohugo.io/t/how-to-trim-extention-of-image-file/38673/1 "2022-05-18T18:55:22Z")

</div>

Hi,  
with this code I grab an image from a page bundle.  
I link a resource from frontmatter.

```auto
     {{ $img := (.Resources.ByType "image").GetMatch "featured-image" }}
     {{ with $img }}
     {{ partial "tools/img" (dict "img" . "alt" $img.Title ) }}
     {{ end }}

```

Frontmatter

```auto
resources:
  - name: featured-image
    src: ImageName.jpg

```

I’m trying to cut the image file extension from: ImageName.jpg to ImageName to put it in the “alt” tag. Which approach do you recommend? Any advice?

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [May 18, 2022, 7:13pm UTC](https://discourse.gohugo.io/t/how-to-trim-extention-of-image-file/38673/2 "2022-05-18T19:13:59Z")

</div>

> [@dugajimi](#):
>
> Any advice?

You could try

{{ $imageName := “foo.jpg” }}  
{{ $imageName = $imageName | strings.TrimSuffix (path.Ext $imageName )}}

Or something…

---

<div class="post-metadata">

### Author: ![dugajimi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dugajimi/32/11698_2.png) [@dugajimi](https://discourse.gohugo.io/u/dugajimi)
#### Post date: [May 18, 2022, 7:21pm UTC](https://discourse.gohugo.io/t/how-to-trim-extention-of-image-file/38673/3 "2022-05-18T19:21:49Z")

</div>

Thanks @bep it works!

For clarity, I report the complete code:

```auto
            {{ $img := (.Resources.ByType "image").GetMatch "featured-image" }}
            {{ with $img }}
            {{ $imageName := $img.Title }}
            {{ $imageName = $imageName | strings.TrimSuffix (path.Ext $imageName )}}

            {{ partial "tools/img" (dict "img" . "alt" $imageName ) }}
            {{ end }}

```

---

<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: [May 20, 2022, 7:22pm UTC](https://discourse.gohugo.io/t/how-to-trim-extention-of-image-file/38673/4 "2022-05-20T19:22:23Z")

</div>

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