pancake
February 27, 2019, 9:55pm
1
I’d like to use image processing to resize my images. There are instances where there is no corresponding image for a markdown file so I then use a placeholder.
The following code gives all images that match a markdown file and resizes them.
{{range .Pages}}
<h1>{{.Title}}</h1>
{{$img := (site.GetPage "/img").Resources.GetMatch (printf "%s.*" (.Params.company))}}
{{with $img}}
{{$resize := $img.Fill "200x200"}}
<img src="{{$resize.RelPermalink}}">
{{end}}
{{end}}
I’ve tried doing the following so that if $img
does not exist, it shows /img/unknown.jpg
but I don’t know how to resize this image.
{{range .Pages}}
<h1>{{.Title}}</h1>
{{$img := (site.GetPage "/img").Resources.GetMatch (printf "%s.*" (.Params.company))}}
<img src="{{with $img}}{{$resize := $img.Fill "200x200"}}{{$img.RelPermalink}}{{else}}/img/unknown.jpg{{end}}"/>
{{end}}
zwbetz
February 27, 2019, 9:57pm
2
It depends where /img/unknown.jpg
is located. Is it under static
, assets
, or is it a page resource?
pancake
February 27, 2019, 9:59pm
3
It’s a page resource. It’s located in the same folder as all the other images.
Content
|---- companies
|------ img
|----- index.md (headless = true)
|----- unknown.jpg
pancake
February 27, 2019, 10:14pm
4
I’ve managed to get it to work. But it’s quite an ugly solution. I’ve broken up the code so it’s easier to read.
{{range .Pages}}
<h1>{{.Title}}</h1>
{{$img := (site.GetPage "/img").Resources.GetMatch (printf "%s.*" (.Params.company))}}
{{$unknown := (site.GetPage "/img").Resources.GetMatch "unknown.jpg"}}
<img src="
{{with $img}}
{{$resize := $img.Fill "200x200"}}
{{$resize.RelPermalink}}
{{else}}
{{$resize := $unknown.Fill "200x200"}}
{{$resize.RelPermalink}}
{{end}}"/>
{{end}}