HUGO
Menu
GitHub 86778 stars Mastodon

reflect.IsImageResourceWithMeta

Reports whether the given value is a Resource object representing an image with metadata.

Syntax

reflect.IsImageResourceWithMeta INPUT

Returns

bool
New in v0.157.0

An image with metadata is an image format from which Hugo can extract dimensions and, if present, Exif, IPTC, and XMP data.

Usage

Use the reflect.IsImageResourceWithMeta function to determine if a resource is an image format from which metadata can be extracted.

{{ with resources.Get "image.avif" }}
  {{ reflect.IsImageResourceWithMeta . }} → true
{{ end }}

Formats with metadata

The following table shows the result of reflect.IsImageResourceWithMeta for various file types and their corresponding media types.

FilenameMedia TypeWith Meta
data.jsonapplication/jsonfalse
image.avifimage/aviftrue
image.bmpimage/bmpfalse
image.gifimage/giffalse
image.heicimage/heictrue
image.heifimage/heiftrue
image.jpgimage/jpegtrue
image.pngimage/pngtrue
image.svgimage/svg+xmlfalse
image.tiffimage/tifftrue
image.webpimage/webptrue

Example

You can use this function to safely extract and display metadata from a collection of images, ensuring you only call metadata methods on supported formats.

{{ range resources.ByType "image" }}
  {{ if reflect.IsImageResourceWithMeta . }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">
    {{ with .Meta }}
      <p>Taken on: {{ .Date }}</p>
    {{ end }}
  {{ end }}
{{ end }}