| 1 |
--- |
| 2 |
_comment: Do not remove front matter. |
| 3 |
--- |
| 4 |
|
| 5 |
## Image operations |
| 6 |
|
| 7 |
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. |
| 8 |
|
| 9 |
- [`reflect.IsImageResource`][]: {{% get-page-desc "/functions/reflect/isimageresource" %}} |
| 10 |
- [`reflect.IsImageResourceProcessable`][]: {{% get-page-desc "/functions/reflect/isimageresourceprocessable" %}} |
| 11 |
- [`reflect.IsImageResourceWithMeta`][]: {{% get-page-desc "/functions/reflect/isimageresourcewithmeta" %}} |
| 12 |
|
| 13 |
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. |
| 14 |
|
| 15 |
|Format|IsImageResource|IsImageResourceProcessable|IsImageResourceWithMeta| |
| 16 |
|:-----|:--------------|:-------------------------|:----------------------| |
| 17 |
|AVIF |true |true |true | |
| 18 |
|BMP |true |true |true | |
| 19 |
|GIF |true |true |true | |
| 20 |
|HEIC |true |**false** |true | |
| 21 |
|HEIF |true |**false** |true | |
| 22 |
|ICO |true |**false** |**false** | |
| 23 |
|JPEG |true |true |true | |
| 24 |
|PNG |true |true |true | |
| 25 |
|SVG |true |**false** |**false** | |
| 26 |
|TIFF |true |true |true | |
| 27 |
|WebP |true |true |true | |
| 28 |
|
| 29 |
This contrived example demonstrates how to iterate through resources and use these functions to apply the appropriate handling for each image format. |
| 30 |
|
| 31 |
```go-html-template |
| 32 |
{{ range resources.Match "**" }} |
| 33 |
{{ if reflect.IsImageResource . }} |
| 34 |
{{ if reflect.IsImageResourceProcessable . }} |
| 35 |
{{ with .Process "resize 300x webp" }} |
| 36 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 37 |
{{ end }} |
| 38 |
{{ else if reflect.IsImageResourceWithMeta . }} |
| 39 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 40 |
{{ else }} |
| 41 |
<img src="{{ .RelPermalink }}" alt=""> |
| 42 |
{{ end }} |
| 43 |
{{ end }} |
| 44 |
{{ end }} |
| 45 |
``` |
| 46 |
|
| 47 |
[`reflect.IsImageResourceProcessable`]: /functions/reflect/isimageresourceprocessable/ |
| 48 |
[`reflect.IsImageResourceWithMeta`]: /functions/reflect/isimageresourcewithmeta/ |
| 49 |
[`reflect.IsImageResource`]: /functions/reflect/isimageresource/ |