Image processing with missing image

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}}

It depends where /img/unknown.jpg is located. Is it under static, assets, or is it a page resource?

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

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}}