> ## Documentation Index
> Fetch the complete documentation index at: https://docs.prisme.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Image Module

> Read the dimensions of an image and downscale it in automations

Runtime modules are the built-in capabilities you call from automations on the foundation, and the **image** module is one of them. It reads the dimensions of a base64 image and downscales it under a size limit, inside the runtime: the image never leaves it, and no Custom Code function is needed. Typical use: bringing an image under a model provider's pixel limit before sending it to the model.

It reads **png**, **jpeg**, **gif** and **webp** images. Any other format (svg, tiff, heif, pdf…) is refused before being decoded.

## dimensions

Read the format and dimensions of an image from its header only, without decoding the image.

```yaml theme={null}
- run:
    module: image
    function: dimensions
    parameters:
      data: "{{file.base64}}"
    output: dims
# dims → { format: "png", width: 1440, height: 9200 }
```

| Parameter | Type | Required | Default | Description |
| - | - | - | - | - |
| data | string | yes | | The image in base64, bare or as a `data:` URI. Line breaks are ignored. |

Returns `{ format, width, height }`, or `null` when the data is empty, not one of the four formats, or its header is unreadable. `width` and `height` are the stored dimensions: a phone photo shot in portrait can be stored in landscape with an EXIF orientation, and reports its stored sides here. The longest side is the same either way.

## fit

Downscale an image so that neither side exceeds `maxSide` pixels, and optionally its encoded size `maxBytes`. The aspect ratio is kept, and the image is never enlarged.

```yaml theme={null}
- run:
    module: image
    function: fit
    parameters:
      data: "{{file.base64}}"
      maxSide: 2048
      maxBytes: 3670016
    output: fitted
- conditions:
    '{{fitted.error}}':
      - comment: "The image could not be fitted: tell the user"
    default:
      - set:
          name: imageUrl
          value: "data:{{fitted.mediaType}};base64,{{fitted.data}}"
```

| Parameter | Type | Required | Default | Description |
| - | - | - | - | - |
| data | string | yes | | The image in base64, bare or as a `data:` URI. Line breaks are ignored. |
| maxSide | number | yes | | Maximum width and height of the result, in pixels. A positive integer. |
| format | string | no | the input's | Output format: `png`, `jpeg` or `webp`. Without it, the image keeps its format, and a gif that has to be rewritten becomes a png (first frame). Transparent areas turn white in a `jpeg`. An empty value counts as absent. |
| maxBytes | number | no | | Maximum size of the result, in bytes, such as a model provider's per-image limit. A heavier result is re-encoded as `jpeg`, unless `format` is set; still too heavy, `fit` returns `output_too_heavy`. A positive integer; an empty value, such as an unset variable, counts as absent. |

The EXIF orientation is applied, so a phone photo comes out the right way up, without an orientation tag.

When the image already fits, keeps its format, has no orientation to apply and is within `maxBytes`, it is returned unchanged with `resized: false`. Only its header is read then: a corrupt image can come back unchanged. Otherwise it is returned rewritten:

| Field | Description |
| - | - |
| data | The image in bare base64 (no `data:` prefix). |
| mediaType | Its media type, such as `image/png`. |
| width, height | Its dimensions, as displayed. |
| originalWidth, originalHeight | The dimensions of the input, as displayed. |
| resized | `true` when the image was downscaled, converted or turned. |

An image that cannot be fitted does not stop the automation. `fit` returns `{ error }` instead, with a `message` for some of them (`unreadable`, `timeout`, `too_costly`, `busy`, `output_too_heavy`):

| Error | Cause |
| - | - |
| `invalid_max_side` | `maxSide` is missing, or not a positive integer. |
| `invalid_max_bytes` | `maxBytes` is not a positive integer. |
| `invalid_format` | `format` is not `png`, `jpeg` or `webp`. |
| `empty_image` | `data` is empty. |
| `unsupported_format` | The image is not a png, jpeg, gif or webp. |
| `too_heavy` | The decoded image is larger than the runtime allows (25 MB by default). Refused before decoding. |
| `too_many_pixels` | The image has more pixels than the runtime allows (50 megapixels by default). Refused before decoding. |
| `too_costly` | Fitting the image would take more memory than the runtime's whole image budget (450 MB by default), estimated from its size, its format, `maxSide` and the output format. Returned at once, without waiting. A smaller `maxSide`, or a `jpeg` or `png` output, costs less. |
| `busy` | The image could not get its share of memory within 10 seconds (default): too many images are being fitted at once, or the runtime container is short of free memory. Try again in a moment. |
| `aborted` | The execution was cancelled while the image was waiting its turn. |
| `timeout` | Processing took longer than the runtime allows (10 s by default). |
| `output_too_large` | The result exceeds what the output format can hold (16383 px per side for `webp`). |
| `output_too_heavy` | The result is still larger than `maxBytes`, even re-encoded as `jpeg`. |
| `unreadable` | The image is corrupt or truncated. |

The memory and time a fit takes grow with the output size: a `maxSide` close to what the consumer of the image actually uses keeps them low. Model providers keep no more than 1568 to 3072 px per side, so a larger `maxSide` only makes the image heavier.

A runtime instance shares a memory budget between the images it fits. Before decoding, each image reserves what it takes, from its header: about 100 MB for a baseline JPEG or a plain PNG (phone photos, screenshots) and 300 MB for a progressive JPEG, an interlaced PNG, a webp or a gif, which are decoded whole, both up to 2048 px in the image's own format; more for a larger `maxSide` or a `webp` output. An image already within `maxSide` only reserves the reading of its header, and is decoded only if its orientation must be applied. An image waits while the budget is full, at most three are fitted at once, and the reservations never exceed the memory the runtime container has left. The first image kept waiting for lack of memory goes before the ones that arrive after it.

> [!NOTE]
>
> On a self-hosted platform, the limits of `fit` are set on the runtime: `IMAGE_MAX_INPUT_BYTES`, `IMAGE_MAX_INPUT_PIXELS`, `IMAGE_FIT_MEMORY_BUDGET`, `IMAGE_MEMORY_PRESSURE_PERCENT`, `IMAGE_MAX_CONCURRENT_FITS`, `IMAGE_FIT_QUEUE_TIMEOUT_SECONDS`, `IMAGE_FIT_TIMEOUT_SECONDS` and `IMAGE_LIBVIPS_THREADS`. See [Environment variables](https://docs.prisme.ai/self-hosting/kubernetes/environment-variables.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.