Render Images, get .webp, .avif or jpg / png

<figure>
  <picture>
      {{- $img := "" -}}
{{ if hasPrefix .Destination "http" }}
{{ $img = resources.GetRemote .Destination }}
{{ else }}
{{ $img = resources.Get .Destination }}
{{end}}
    {{- $destination := .Get "src" -}}
    {{- $extension := path.Ext $destination -}}
    {{- $resources := .Page.Resources -}}
  
    {{- $avifPath:= replace $destination $extension ".avif" -}}
    {{- $webpPath:= replace $destination $extension ".webp" -}}
    {{- $altText := .Get "alt" -}}

    {{- with $resources.GetMatch $avifPath -}}
    <source srcset="{{- $avifPath -}}" type="image/avif">
    {{- end -}}
    {{- with $resources.GetMatch $webpPath -}}
    <source srcset="{{- $webpPath -}}" type="image/webp">
    {{- end -}}
  
    {{- with $resources.GetMatch $destination -}}
      <img
        src="{{- .Permalink -}}"
        alt="{{- $altText -}}"
        loading="lazy"
        decoding="async"
        width="{{- .Width -}}"
        height="{{- .Height -}}"
      />
    {{- end -}}
  </picture>
  
  <figcaption>{{ .Get "caption" }}</figcaption>
</figure>

the error is:
/layouts/_default/_markup/render-image.html:9:24": execute of template failed: template: _default/_markup/render-image.html:9:24: executing "_default/_markup/render-image.html" at <.Get>: can't evaluate field Get in type goldmark.linkContext

my intention is, if there are webp or avif, get it

Thank u very much

This doesn’t make any sense in a render hook.

And unless you know what you’re doing, rendering an image as a figure element generates invalid HTML.

Obviously, I don’t know what I’m doing as there are hundreds of ways of doing things, but I think you get what I’m trying to do …, thanks!