HUGO
Menu
GitHub 86778 stars Mastodon

reflect.IsImageResourceProcessable

Reports whether the given value is a Resource object representing a processable image.

Syntax

reflect.IsImageResourceProcessable INPUT

Returns

bool
New in v0.157.0

A processable image is an image file characterized by one of the following media types:

  • image/bmp
  • image/gif
  • image/jpeg
  • image/png
  • image/tiff
  • image/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.

FilenameMedia TypeProcessable
data.jsonapplication/jsonfalse
image.avifimage/aviffalse
image.bmpimage/bmptrue
image.gifimage/giftrue
image.heicimage/heicfalse
image.heifimage/heiffalse
image.jpgimage/jpegtrue
image.pngimage/pngtrue
image.svgimage/svg+xmlfalse
image.tiffimage/tifftrue
image.webpimage/webptrue

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 }}