But then a following problem occured. I use the hugo-universal-theme and this theme provides a way to show “recent posts” on the startpage. With the resizing and not publishing the original file, i have no clue how to put the “resized” image there.
First Try: Resize again. For that i changed the “recent_posts.html” in the theme but it somehow cant access the original path. Maybe because it works on the already processed images.
Second Thought: Put the filename of the resized image into the “banner” parameter at the top of a post, so that the template finds it. That works, but i dont have the name in the first place. So i had to run the resizing, find the name and have to put it there. Thats not very comfortable.
Third Thought: Rename the File after resizing to the original name or to the originalname with some suffix. Something like “example.jpg” to “example_minified.jpg”. But i dont get this working…
I hope you have a better idea or a solution for one of these ideas. Thanks!
using Frontmatter parameter Banner would need a hardcode argument. But if this is not set
it will use the image in the page buldle names img/preview.png
If you don’t mind duplicating this image, the simplest would be to publish fe the first image you resized twice using that hardcoded path.
{{- if $r is first first image resource}}
{{ with $r | resources.Copy "img/preview.png" }}
{{- $noop := .RelPermalink }}
{{ end }}
{{ end }}
Doing that you do not need to adjust the templates.
I tried to integrate your code snippet to generate a preview image that is included in the export, but that does not work. I had some success getting the image to the static/img/ folder, but i think i need one in each post-folder. And i am not smart enough to get this working…
Sorry, this is unfortunatly not possible. (big private repo…) but i can make a repo with only this issue. So you dont have to do this work! I will write here again if i have set it up. Thanks
root cause: the resource copied to the target page img/preview.jpg is unfortunately not added as a page resource. that’s why it’s not found - seems to be standard for hugo.
I changed the code to:
save the .RelPermalink of the first resized image to a the page.Store instead of creating a copy
imagegallery replaced lines 14-18 with this
{{ if not $index }}
{{ $.Page.Store.Set "banner" $img.RelPermalink }}
{{ end }}
retrieve the page.Store while rendering the recent posts (and make sure Store is fixed)
recent_posts - updated the div for image selection
<div class="image" style="overflow:hidden">
{{- /* make sure page is rendered to determinate Store */}}
{{ $noop := .WordCount }}
{{ $page := . }}
{{ with $page.Params.banner }}
{{ with $page.Resources.Get . }}
{{ warnf "[%s] Page Params: loaded from %s " $page.Path . }}
<img src="{{ .RelPermalink }}" class="img-responsive" alt="" />
{{ else }}
{{ warnf "[%s] params banner not found at : %s " $page.Path . }}
{{ end }}
{{ else with $page.Store.Get "banner" }}
{{ warnf "[%s] Page resource loaded from %s " $page.Path . }}
<img src="{{ . }}" class="img-responsive" alt="" />
{{ else }}
{{ warnf "[%s] Global fallback loaded from img/placeholder.png" $page.Path}}
<img src="{{ "img/placeholder.png" | relURL }}" class="img-responsive" alt="" />
{{ end }}
</div>
Benefit: no duplicate image published
Drawback: the implementation supports only ONE imagegallery per page! or we could say: the first image of the last imagegallery will be used. (could be changed though)
Thank you very very much!
If i could send you a coffee and cake i would do
It works great!
I had to change also the “list.html” in the theme → _default folder to have the images in the blog-post overview. That worked great.
Thank you, i would have never come to this solution.
PS: Danke und ebenfalls einen guten Rutsch ins neue Jahr