imageConfig doesn't work with module's images, but fileExists does

I have an image saved as static/images/example.jpg inside a module, but I found that the imageConfig can’t resolve it from a module, but the fileExists function does, not sure if should it be considered as a bug.

common/             - The common module.
  static/
    images/
      example.jpg

main/                     - The site imports the common module.
{{- warnf "%s: %v" $filepath (fileExists $filepath) }}
{{- if fileExists $filepath }}
  {{- with imageConfig $filepath }}
    {{- $naturalHeight = printf "%dpx" .Height }}
    {{- $naturalWidth = printf "%dpx" .Width }}
  {{- end }}
{{- end }}
WARN  static/images/example.jpg: true
...executing "partials/images/image.html" at <imageConfig $filepath>: error calling imageConfig: open /home/razonyang/Projects/hugomods/site/main/static/images/example.jpg: no such file or directory

imageConfig currently reads in the project working dir (only) – which is a limitation/bug, and should be fixed, but these “file funcs” precedes Hugo’s image/resource API, which should have less surprises in this area. I welcome fixes to these, but it’s non-trivial to do without breaking stuff, which is probably why It’s not on my priority list. I’m a pretty active Hugo user myself, and it’s many years since I used imageConfig, fileExists etc.

I suggest that you mount/put the images that you need to know dimensions from inside /assets.

1 Like

I see, I created a GitHub issue for tracking this, `imageConfig` not working with module's images. · Issue #11205 · gohugoio/hugo · GitHub.

I solved it by mounting it to assets, hope it helps.

common/hugo.yaml
module:
  mounts:
    - source: content
      target: content
    - source: static
      target: static
    - source: layouts
      target: layouts
    - source: data
      target: data
    - source: assets
      target: assets
    - source: i18n
      target: i18n
    - source: archetypes
      target: archetypes
    - source: static/images/example.jpg
      target: assets/images/example.jpg

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