HUGO
Menu
GitHub 87548 stars Mastodon

reflect.IsImageResource

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

Syntax

reflect.IsImageResource INPUT

Returns

bool
New in v0.154.0

processable image(可处理图像)是具有以下 media types(媒体类型)之一的图像文件:

  • image/gif
  • image/jpeg
  • image/png
  • image/tiff
  • image/webp

Hugo 可以解码和编码这些图像格式,允许您使用适用于图像的 resource methods(资源方法),如 WidthHeightCropFillFitResize

With this project structure:

project/
├── assets/
│   ├── a.json
│   ├── b.avif
│   └── c.jpg
└── content/
    └── example/
        ├── index.md
        ├── d.json
        ├── e.avif
        └── f.jpg

These are the values returned by the reflect.IsImageResource function:

layouts/page.html
{{ with resources.Get "a.json" }}
  {{ reflect.IsImageResource . }} → false
{{ end }}

{{ with resources.Get "b.avif" }}
  {{ reflect.IsImageResource . }} → false
{{ end }}

{{ with resources.Get "c.jpg" }}
  {{ reflect.IsImageResource . }} → true
{{ end }}

In the example above, the b.avif image is not a processable image because Hugo can neither decode nor encode the AVIF image format.

layouts/page.html
{{ with .Resources.Get "d.json" }}
  {{ reflect.IsImageResource . }} → false
{{ end }}

{{ with .Resources.Get "e.avif" }}
  {{ reflect.IsImageResource . }} → false
{{ end }}

{{ with .Resources.Get "f.jpg" }}
  {{ reflect.IsImageResource . }} → true
{{ end }}

In the example above, the e.avif image is not a processable image because Hugo can neither decode nor encode the AVIF image format.

layouts/page.html
{{ with site.GetPage "/example" }}
  {{ reflect.IsImageResource . }} → false
{{ end }}

Last updated: January 1, 0001
Improve this page