Back to Hugo

Image Reflection Functions

docs/content/en/_common/functions/reflect/image-reflection-functions.md

0.161.12.6 KB
Original Source

Image operations

Use these functions to determine which operations Hugo supports for a given resource. While Hugo classifies a variety of file types as image resources, its ability to process them or extract metadata varies by format.

The table below shows the values these functions return for various file formats. Use it to determine which checks are required before calling specific methods in your templates.

FormatIsImageResourceIsImageResourceProcessableIsImageResourceWithMeta
AVIFtruefalsetrue
BMPtruetruetrue
GIFtruetruetrue
HEICtruefalsetrue
HEIFtruefalsetrue
ICOtruefalsefalse
JPEGtruetruetrue
PNGtruetruetrue
SVGtruefalsefalse
TIFFtruetruetrue
WebPtruetruetrue

This contrived example demonstrates how to iterate through resources and use these functions to apply the appropriate handling for each image format.

go-html-template
{{ range resources.Match "**" }}
  {{ if reflect.IsImageResource . }}
    {{ if reflect.IsImageResourceProcessable . }}
      {{ with .Process "resize 300x webp" }}
        
      {{ end }}
    {{ else if reflect.IsImageResourceWithMeta . }}
      
    {{ else }}
      
    {{ end }}
  {{ end }}
{{ end }}