| 1 |
--- |
| 2 |
title: Fit |
| 3 |
description: Applicable to images, returns a new image resource downscaled to fit according to the given processing specification. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: images.ImageResource |
| 9 |
signatures: [RESOURCE.Fit SPECIFICATION] |
| 10 |
--- |
| 11 |
|
| 12 |
{{% include "/_common/methods/resource/global-page-remote-resources.md" %}} |
| 13 |
|
| 14 |
The `Fit` method returns a new resource from a [processable image](g) according to the given [processing specification](#processing-specification). |
| 15 |
|
| 16 |
> [!NOTE] |
| 17 |
> Use the [`reflect.IsImageResourceProcessable`][] function to verify that an image can be processed. |
| 18 |
|
| 19 |
## Usage |
| 20 |
|
| 21 |
When fitting, you must provide both width and height (such as `300x175`) within the specification. `Fit` maintains the original aspect ratio by downscaling the image until it fits within the specified dimensions. Unlike [`Fill`][] or [`Resize`][], this method will never upscale an image; if the source image is smaller than the target dimensions, the dimensions of the resulting image are the same as the original. |
| 22 |
|
| 23 |
```go-html-template |
| 24 |
{{ with resources.Get "images/original.jpg" }} |
| 25 |
{{ with .Fit "300x175" }} |
| 26 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 27 |
{{ end }} |
| 28 |
{{ end }} |
| 29 |
``` |
| 30 |
|
| 31 |
In the example above, `"300x175"` is the processing specification. |
| 32 |
|
| 33 |
{{% include "/_common/methods/resource/processing-spec.md" %}} |
| 34 |
|
| 35 |
## Example |
| 36 |
|
| 37 |
```go-html-template |
| 38 |
{{ with resources.Get "images/original.jpg" }} |
| 39 |
{{ with .Fit "300x175" }} |
| 40 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 41 |
{{ end }} |
| 42 |
{{ end }} |
| 43 |
``` |
| 44 |
|
| 45 |
{{< img |
| 46 |
src="images/examples/zion-national-park.jpg" |
| 47 |
alt="Zion National Park" |
| 48 |
filter="Process" |
| 49 |
filterArgs="fit 300x175" |
| 50 |
example=true |
| 51 |
>}} |
| 52 |
|
| 53 |
[`Fill`]: /methods/resource/fill/ |
| 54 |
[`Resize`]: /methods/resource/resize/ |
| 55 |
[`reflect.IsImageResourceProcessable`]: /functions/reflect/isimageresourceprocessable/ |