hugo icon indicating copy to clipboard operation
hugo copied to clipboard

Add resources.IsProcessableImage

Open bep opened this issue 1 year ago • 4 comments

This is slightly related to https://github.com/gohugoio/hugo/pull/11688

Note that I'm not totally sure about this ... There may be an alternative fix lurking around for this somewhere.

  • Currently $resources.ByType means filter by MIME main type.
  • The problem with this for type image is that most people expect the images returned to have a .Width and to be resizeable etc.

We have a few options:

  • We could make $resources.ByType 'image' return only images that's processable.
  • We could add some $resource.IsProcessableImage` ... or something less ugly.

Not sure.

bep avatar Dec 04 '23 11:12 bep

I'll vote for the $resource.IsProcessableImage, I think $resources.ByType 'image' should return all images regardless if it's processable, such as SVG, beside this approach probably bring breaking changes.

razonyang avatar Dec 04 '23 15:12 razonyang

I agree with @razonyang. I think this is fine:

{{ range resources.ByType "image" }}
  {{ with and .IsProcessableImage (.Process "resize 200x webp") }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

Using a method as shown above seems a little cleaner to me. The alternative is:

{{ range resources.ByType "image" }}
  {{ with and (resources.IsProcessableImage .) (.Process "resize 200x webp") }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

jmooring avatar Dec 04 '23 21:12 jmooring

I have been thinking about this, and on the implementation level this would be much easier/cleaner if we make it a function instead of a method -- then it would just be a matter of checking if the type implements an interface, which is a pattern we could extend on if needed, e.g:

  • resources.IsProcessableImage
  • resources.HasWidth

...

Also, having $page.IsProcessableImage always return false isnt't great when we want to document this down the line and then gets $page.IsProcessableVideo etc..

bep avatar Dec 05 '23 08:12 bep

I upvote @razonyang's suggestion.

hugo-sid avatar Dec 08 '23 01:12 hugo-sid