Hi,
I’m using browser-native lazy-loading, and I’m trying to use hugo’s markdown render hooks, render-image.html
here, to set the img
tag’s width
and height
attributes, so images don’t push text around when they get downloaded.
However, I keep getting the following error with many pictures:
error calling imageConfig: image: unknown format
The error occurs with both .png and .jpg images. Here’s the template code:
{{ $width := 0 }}
{{ $height := 0 }}
{{ $path := add .Page.RelPermalink (.Destination | safeURL) }}
{{ if not (fileExists $path) }}
{{ $path = add "/static" (.Destination | safeURL) }}
{{ end }}
{{ warnf "%q" $path }}
{{ if (fileExists $path) }}
{{ $fullPath := $path }}
{{ if not (findRE "^/?static" $path 1) }}
{{ $fullPath = add "/content" $fullPath }}
{{ end }}
{{ if ne $fullPath "image" }}
{{ warnf "%q" $fullPath }}
{{ with (imageConfig $fullPath) }}
{{ $width = .Width }}
{{ $height = .Height }}
{{ end }}
{{ end }}
{{ else }}
{{ warnf "<img/> render hook: %q not found" $path }}
{{ end }}
<img
src="{{ .Destination | safeURL }}"
alt="{{ .Text }}"
loading="lazy"
{{ if ne $width 0 }}width="{{ $width }}"{{ end }}
{{ if ne $height 0 }}height="{{ $height }}"{{ end }}
{{ with .Title}}title="{{ . }}"{{ end }}
/>
And the paths output by the warnf call:
WARN 2020/10/22 14:07:24 "/news/2017/10/godot-3-alpha-attack-tutorial/img/echo86/logo.png"
WARN 2020/10/22 14:07:24 "/content/news/2017/10/godot-3-alpha-attack-tutorial/img/echo86/logo.png"
WARN 2020/10/22 14:07:24 "/news/2017/10/godot-3-alpha-attack-tutorial/img/randa-meetings/konqi-randa.png"
WARN 2020/10/22 14:07:24 "/content/news/2017/10/godot-3-alpha-attack-tutorial/img/randa-meetings/konqi-randa.png"
Built in 615 ms
Error: Error building site: "/home/gdquest/Repositories/website/layouts/_default/_markup/render-image.html:18:13": execute of template failed: template: _default/_markup/render-image.html:18:13: executing "_default/_markup/render-image.html" at <imageConfig $fullPath>: error calling imageConfig: image: unknown format
I started this website (https://www.gdquest.com/) years ago, when hugo had way fewer features, and so I have hundreds of pictures. Back then, I would compress them before adding them to the website, as image processing features weren’t in yet.
As imageConfig doesn’t say which image is faulty, I’m unsure how to pinpoint the pictures that cause trouble and why.
Do you have an idea of how I could further troubleshoot this and get rid of the error?
Note I’d gladly pay someone to help tackle this task. You can find the corresponding issue here on GitHub: https://github.com/GDQuest/website/issues/228 (the website’s open-source). If you’re interested, you can contact me on twitter.
Thank you for your time,
Nathan