I have a partial to resize an image and also rename it to don’t have a long name after resize.
The problem is that I have two images: one for normal and one for retina. But $img1 and $img2 variables in fact contains the same size image with a different name. If from there I remove with Resources.Copy
then it works well. Two images with different sizes, but with ugly names. If I add Resources.Copy
then names are fine, but the size is the same.
Any ideas what’s the problem?
{{ $height := default "340" .height }}
{{ $heightX2 := mul (int $height) 2 }}
{{ $img1 := "" }}
{{ $img2 := "" }}
{{ $src := resources.Get $.src }}
{{ with $src }}
{{ $filename := path.Base $src }}
{{ $basename := replaceRE "\\..*$" "" $filename }}
{{ with .Resize (printf "x%s webp" $height) }}
{{ with . | resources.Copy (printf "%s.webp" $basename) }}
{{ $img1 = . }}
{{ end }}
{{ end }}
{{ with .Resize (printf "x%d webp" $heightX2) }}
{{ with . | resources.Copy (printf "%s@2x.webp" $basename) }}
{{ $img2 = . }}
{{ end }}
{{ end }}
<img src="{{ $img1.RelPermalink }}" srcset="{{ $img1.RelPermalink }} 1x, {{ $img2.RelPermalink }} 2x" loading="lazy" decoding="async"
{{ with $.width }}width="{{ $.width }}"{{else}}width="{{ $img1.Width }}"{{ end }}
{{ with $.height }}height="{{ $.height }}"{{else}}height="{{ $img1.Height }}"{{ end }}
{{ with $.class }}class="{{ . }}"{{ end }}
{{ with $.alt }}alt="{{ . }}"{{ end }}
{{ with $.id }}id="{{ . }}"{{ end }}
{{ with $.style }}style="{{ . }}"{{ end }}
{{ with $.title }}title="{{ . }}"{{ end }}
/>
{{ end }}
How I call that partial {{ partial "imgproc" (dict "class" "overflow-hidden w-[88px] h-12 rounded-md" "height" "48" "src" "/img/payment-method-1-min.jpg" "alt" "Visa" ) }}