rimg.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. {{/* Combination of code taken from: https://alexlakatos.com/web/2020/07/17/hugo-image-processing/ & https://dev.to/stereobooster/responsive-images-for-hugo-dn9}}
  2. {{/* get file that matches the filename as specified as src="" in shortcode */}}
  3. {{ $src := resources.GetMatch (.Get "src") }}
  4. {{ if in (.Get "src") "http" }}
  5. <img src="{{$src}}" {{ with .Get "alt" }}alt="{{.}}"{{ else }}alt=""{{ end }}>
  6. {{ else }}
  7. {{ if in (.Get "src") ".gif" }}
  8. <img src="{{$src.RelPermalink}}" {{ with .Get "alt" }}alt="{{.}}"{{ else }}alt=""{{ end }}>
  9. {{ else }}
  10. {{/* set image sizes, these are hardcoded for now */}}
  11. {{ $tinyw := default "500x" }}
  12. {{ $smallw := default "800x" }}
  13. {{ $mediumw := default "1200x" }}
  14. {{ $largew := default "1500x" }}
  15. {{/* resize the src image to the given sizes */}}
  16. {{ $tiny := $src.Resize $tinyw }}
  17. {{ $small := $src.Resize $smallw }}
  18. {{ $medium := $src.Resize $mediumw }}
  19. {{ $large := $src.Resize $largew }}
  20. {{/* add the processed images to the scratch */}}
  21. {{/* only use images smaller than or equal to the src (original) image size */}}
  22. <img
  23. {{ with .Get "sizes" }}sizes='{{.}}'{{ else }}{{ end }}
  24. srcset='
  25. {{ if ge $src.Width "500" }}
  26. {{ with $tiny.RelPermalink }}{{.}} 500w{{ end }}
  27. {{ end }}
  28. {{ if ge $src.Width "800" }}
  29. {{ with $small.RelPermalink }}, {{.}} 800w{{ end }}
  30. {{ end }}
  31. {{ if ge $src.Width "1200" }}
  32. {{ with $medium.RelPermalink }}, {{.}} 1200w{{ end }}
  33. {{ end }}
  34. {{ if ge $src.Width "1500" }}
  35. {{ with $large.RelPermalink }}, {{.}} 1500w {{ end }}
  36. {{ end }}'
  37. {{ if .Get (print $medium) }}
  38. src="{{ $medium.RelPermalink }}"
  39. {{ else }}
  40. src="{{ $src.RelPermalink }}"
  41. {{ end }}
  42. {{ with .Get "alt" }}alt='{{.}}'{{ end }}>
  43. {{ end }}
  44. {{ end }}