reflect.IsImageResourceProcessable
Syntax
Returns
A processable image is an image file characterized by one of the following media types:
image/bmpimage/gifimage/jpegimage/pngimage/tiffimage/webp
Hugo can decode and encode these image formats, allowing you to use any of the resource methods applicable to images such as Width, Height, Crop, Fill, Fit, Resize, etc.
Usage
Use the reflect.IsImageResourceProcessable function to determine if a resource can be manipulated by Hugo’s imaging pipeline such as resizing, cropping, or filtering.
{{ with resources.Get "image.jpg" }}
{{ reflect.IsImageResourceProcessable . }} → true
{{ end }}Processable formats
The following table shows the result of reflect.IsImageResourceProcessable for various file types and their corresponding media types.
| Filename | Media Type | Processable |
|---|---|---|
| data.json | application/json | false |
| image.avif | image/avif | false |
| image.bmp | image/bmp | true |
| image.gif | image/gif | true |
| image.heic | image/heic | false |
| image.heif | image/heif | false |
| image.jpg | image/jpeg | true |
| image.png | image/png | true |
| image.svg | image/svg+xml | false |
| image.tiff | image/tiff | true |
| image.webp | image/webp | true |
Although AVIF, HEIC, and HEIF images are not processable, you can can determine their dimensions using the Width and Height methods.
{{ with resources.Get "image.avif" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">
{{ end }}Example
You can use this function to safely filter a collection of images before applying transformations, ensuring that the imaging pipeline only attempts to process supported formats.
{{ range resources.ByType "image" }}
{{ if reflect.IsImageResourceProcessable . }}
{{ with .Process "resize 600x webp" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
{{ end }}