Can't evaluate field Resize in type resource.Resource


#1

I have a post with 6 or 7 images. Each image is added like this:

I have a custom shortcode which looks like this:

<figure>
  {{ $imgname := .Get "imgname" }}
  {{ $imgwidth := .Get "width" }}
  {{ $img := $.Page.Resources.GetByPrefix $imgname }}
  {{ $scaled := $img.Resize $imgwidth }}
  <p style="text-align:center">
    <a href="{{ $img.Permalink }}" style="text-decoration:none">
        <img src="{{ $scaled.Permalink }}" alt="{{ .Get "alt" }}" />
    </a>
  </p>
</figure>

This is included via this shortcode:
{{< figure imgname="imgname" width="500x" alt="altname" >}}

This works fine for all images except two. For these, the scaling process fails, and the error in this topic’s title is printed. The figure is included in the post, and the hyperlink is present - but the scaled image which should be the clickable hyperlink is not existing.

I’d like to know how to debug this issue. It cannot be a problem with hugo in general, since it works for all other images, but not for these two. They are in the middle of the post, there are several other images after them, in post order, but also in alphabetical order, so i don’t think that the scaling process crashes or something, and cannot recover, since it ran as expected on the other images.

The shortcode can’t also be the culprit, i copy&pasted it, and only changed the filename and the alt name. I think the images are bigger than 500px, and are not malformatted or something.

So maybe anyone has an idea?


#2

This is the Resource interface:

The error you get indicates that it is not detected as an image. We detect the type by its MIME type derived from it’s extension (e.g. “image/png” => an image!).

You can check this by printing (or adding a conditional) .ResourceType.

This is probably a filename issue.


#3

Thanks for pointing in the right direction. I printed out the .ResourceType by just adding {{ $img.ResourceType }}. The result is: All images were correctly identified as images - but i had a non-ASCII-char (a “ß”) in one of the filenames. This was the culprit, i changed it, and now the error is gone.


#4

I am running into the same type of issue while testing my site using 0.33 and I am having a hard time tracking down the issue. This is working fine in 0.32.4 (Link to post).

I verified that there are no non-ASCII characters in my filenames and also checked the MIME type on all files.

Here is an example of the error I am seeing:

ERROR 2018/01/18 20:03:29 error processing shortcode "shortcodes/imgproc.html" for page "post/using-actionable-notifications-in-home-assistant/index.md": template: shortcodes/imgproc.html:7:34: executing "shortcodes/imgproc.html" at <$original.Resize>: can't evaluate field Resize in type resource.Resource
ERROR 2018/01/18 20:03:29 error processing shortcode "shortcodes/imgproc.html" for page "post/using-actionable-notifications-in-home-assistant/index.md": template: shortcodes/imgproc.html:7:34: executing "shortcodes/imgproc.html" at <$original.Resize>: can't evaluate field Resize in type resource.Resource
ERROR 2018/01/18 20:03:29 error processing shortcode "shortcodes/imgproc.html" for page "post/using-actionable-notifications-in-home-assistant/index.md": template: shortcodes/imgproc.html:7:34: executing "shortcodes/imgproc.html" at <$original.Resize>: can't evaluate field Resize in type resource.Resource
ERROR 2018/01/18 20:03:29 error processing shortcode "shortcodes/imgproc.html" for page "post/using-actionable-notifications-in-home-assistant/index.md": template: shortcodes/imgproc.html:7:34: executing "shortcodes/imgproc.html" at <$original.Resize>: can't evaluate field Resize in type resource.Resource
ERROR 2018/01/18 20:03:29 error processing shortcode "shortcodes/imgproc.html" for page "post/using-actionable-notifications-in-home-assistant/index.md": template: shortcodes/imgproc.html:7:34: executing "shortcodes/imgproc.html" at <$original.Resize>: can't evaluate field Resize in type resource.Resource
ERROR 2018/01/18 20:03:29 error processing shortcode "shortcodes/imgproc.html" for page "post/using-actionable-notifications-in-home-assistant/index.md": template: shortcodes/imgproc.html:7:34: executing "shortcodes/imgproc.html" at <$original.Resize>: can't evaluate field Resize in type resource.Resource
ERROR 2018/01/18 20:03:29 error processing shortcode "shortcodes/imgproc.html" for page "post/using-actionable-notifications-in-home-assistant/index.md": template: shortcodes/imgproc.html:7:34: executing "shortcodes/imgproc.html" at <$original.Resize>: can't evaluate field Resize in type resource.Resource

Here is my shortcode imgproc.html (taken from the 0.32.4 documentation and slightly adapted):

{{ $original := .Page.Resources.GetByPrefix (.Get 0) }}
{{ $command := .Get 1 }}
{{ $options := .Get 2 }}
{{ if eq $command "Fit"}}
{{ .Scratch.Set "image" ($original.Fit $options) }}
{{ else if eq $command "Resize"}}
{{ .Scratch.Set "image" ($original.Resize $options) }}
{{ else if eq $command "Fill"}}
{{ .Scratch.Set "image" ($original.Fill $options) }}
{{ else }}
{{ errorf "Invalid image processing command: Must be one of Fit, Fill or Resize."}}
{{ end }}
{{ $image := .Scratch.Get "image" }}
<a href="{{ $original.Permalink }}"><img src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}"/></a>

Below is how I call one in my index.md:

{{< imgproc notification_while_locked Resize "400x" >}}

All of my code can be found on GitHub.

I am at a loss on what else to check. Any assistance would be greatly appreciated.

Thanks!

Dan W.


#5

What you can try to make your code more robust is to replace the above with something ala:

{{ $images := .Page.Resources.ByType "images" }}
{{ $original := $images.GetByPrefix (.Get 0) }}
{{ with $original }}
{{ end }}

There are several ways to get your error. My best guess is that it does not find the image.

If this is Hugo 0.33 and your image lives in a sub-dir, then this may be it:


#6

Thanks for this info!

I rewrote my shortcode to this:

{{ $images := .Page.Resources.ByType "image" }}
{{ $original := $images.GetByPrefix (.Get 0) }}
{{ $command := .Get 1 }}
{{ $options := .Get 2 }}
{{ with $original }}
{{ if eq $command "Fit"}}
{{ $.Scratch.Set "image" ($original.Fit $options) }}
{{ else if eq $command "Resize"}}
{{ $.Scratch.Set "image" ($original.Resize $options) }}
{{ else if eq $command "Fill"}}
{{ $.Scratch.Set "image" ($original.Fill $options) }}
{{ else }}
{{ errorf "Invalid image processing command: Must be one of Fit, Fill or Resize."}}
{{ end }}
{{ $image := $.Scratch.Get "image" }}
<a href="{{ $original.Permalink }}"><img src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}"/></a>
{{ end }}

And you were right, I am getting hit by the issue you mentioned. My images ar ein an images folder within the post. Once I added images/ to the beginning of the filename it started working in 0.33:

{{< imgproc "images/notification_while_locked" Resize "400x" >}}

I was looking through the issue and the linked forum post concerning this behaviour. Will this be rolled back to the old behaviour in the next update? I just wanted to understand the way forward.

Again, thanks for taking the time with this.

Dan