*resources.genericResource is not an image, but its png

I want to resize an png like:

{{ $currentPage := . }}

...

{{/* Inside an range: */}}
{{ with $currentPage.Resources.GetMatch .image }}
  {{ printf "%#v" .MediaType }} <!-- Works -->
  {{ printf "%#v" .Permalink }} <!-- Works -->
  {{ printf "%#v" (.Resize "180x") }} <!-- Does not work -->
{{ end }}

Output (without .Resize):

media.Type{MainType:"image", SubType:"png", mimeSuffix:"", Delimiter:".", Suffixes:[]string{"png"}, fileSuffix:"png"}
"//localhost:8080/path-to-image/web.png"

Error (with .Resize):

at <.Resize>: error calling Resize: *resources.genericResource is not an image

What am I missing?


Edit:

This works but is not resized:

<img src="{{ .image }}" alt="Image">

Hi there,

Do you have your site code somewhere we can have a look at? It’s generally easier to help if we can replicate the error you are getting. Have a read here about Requesting Help .

I suspect what you see here is concurrency in play, i.e. the error message you see is not connected to the image you think is there.

I think you can make your code more robust by doing:

{{ if .image }}
{{ with $currentPage.Resources.GetMatch .image }}
  {{ printf "%#v" .MediaType }} <!-- Works -->
  {{ printf "%#v" .Permalink }} <!-- Works -->
  {{ printf "%#v" (.Resize "180x") }} <!-- Does not work -->
{{ end }}
{{ end }}

I am having a very similar error to this and can’t see how to get this to work.

Here is the code in my list.html default:

       {{- if  isset .Params "thumbnail" -}}
        
        {{- $imagePath := (printf "*%s*" .Params.thumbnail) -}}
        {{ with .Resources.GetMatch $imagePath }}
          {{ if eq .ResourceType "image"}}
            {{ $image := . }}
            {{ $image.Resize "600x" }}
          {{ end }}
        {{ end }}

        <img src="{{ .Permalink }}/{{ .Params.thumbnail }}" alt="">
        {{ end }}

And I get an error:

: execute of template failed: template: _default/list.html:18:21: executing "main" at <$image.Resize>: error calling Resize: *resources.genericResource is not an image

          {{ if eq .ResourceType "image"}}
            {{ $image := . }}
            {{ $image.Resize "600x" }}
          {{ end }}
        {{ end }}
hugo v0.92.0+extended darwin/amd64 BuildDate=unknown

Am I not checking the type correctly? How can it pass the if statement for ResourceType and then claim the resource not an image?

The img tag (which is a hold over from before I wanted to start image processing) works fine with the path provided.

Can you share the repo? Or a link to the PNG?

So as is often the case when I get around to posting to a forum, I found the main problem. Out of about 20 images, one was an svg. I took it out for now but now I am wondering if there is an effective way to check if an image is an svg before processing.

Check MediaType or MediaType.SubType

https://gohugo.io/content-management/page-resources/#properties

Thanks!

You can probably tighten up the code a bit with something like:

{{ with .Resources.GetMatch (printf "*%s*" (.Param "thumbnail")) }}
  {{ if in (slice "jpeg" "png" "webp") .MediaType.SubType }}
    {{ with .Resize "600x" }}
      <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ end }}
  {{ end }}
{{ end }}
1 Like