Reputation: 4129
I put some of my images in /assets/images
folder. Then in layouts/_default/single.html
, I created a hero image section for different screen sizes, shown in the code below. The problem is that Hugo says "*resources.genericResource is not an image" while the type checking confirms it's an image. More interesting, it works if I add the below changes while Hugo is running in on my machine. But I stop and restart, it shows the error and doesn't come up.
.Params.image: each post has the name of an image in the front matter like image: /images/bird.jpg
Some posts don't have a hero image because of that I skip this code if $img
is invalid.
Hugo installed via Choco:
Hugo Static Site Generator v0.78.2-959724F0 windows/amd64 BuildDate: 2020-11-13T10:08:15Z
<!-- layouts/_default/single.html-->
{{ $img := resources.GetMatch .Params.image }}
{{ if $img }}
{{ if eq $img.ResourceType "image" }}
{{$hero := $img.Fill "568x400"}}
{{$hero_sm := $img.Fill "768x400"}}
{{$hero_md := $img.Fill "1024x400 q50"}}
{{$hero_lg := $img.Resize "x400 q50"}}
<style>
.post__hero{
background-image: url('{{ $hero.Permalink }}');
}
@media (min-width: 568px) {
.post__hero{
background-image: url('{{ $hero_sm.Permalink }}');
}
}
@media (min-width: 768px) {
.post__hero{
background-image: url('{{ $hero_md.Permalink }}');
}
}
@media (min-width: 1024px) {
.post__hero{
background-image: url('{{ $hero_lg.Permalink }}');
}
}
</style>
<div class="single-image post__hero"></div>
{{end}}
{{end}}
Upvotes: 3
Views: 955
Reputation: 4129
Finally found it. The problem was that Hugo doesn't support image processing for WebP format. Some of my images were WebP. Probably because of that the two conditions:
{{ if $img }}
{{ if eq $img.ResourceType "image" }}
were passed for WebP images, but the functions Fill
and Resize
created the error. So, I had to replace them with JPEG format.
Upvotes: 2