Image transformations

For the very common case of storing images, Xata offers a set of transformations that can be applied in real-time while retrieving images through access URLs. Transformations are specified in the URL and their result is cached by the CDN. Both public and signed URLs support transformations. In the URL the transformation specification is a comma separated list of individual transformations following the /transform/ segment.

Original image URL:

https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94

Image URL with transformations:

https://us-east-1.storage.xata.sh/transform/rotate=180,height=100,format=webp/4u1fh2o6p10blbutjnphcste94

Original

original

Transformed

transformed

When querying a file of file[] column from a record, you can make a direct transformation on any of the files it contains.

// Get a specific record from the "gallery" database
const record = await xata.db.gallery.read('record_id');
 
// Get a new url and signedUrl based on transformations applied to
// an image in the "imageColumnName"
//
// Also grab the related meta data (height, width...etc) of the new image
const { url, signedUrl, metadataUrl, signedMetadataUrl } = record.imageColumnName.transform({
  height: 100,
  rotate: 180,
  format: 'webp'
});

It is also possible to make transformations directly on any given image URL without knowledge of the record it is attached to.

For TypeScript, this is done client-side with the transformImage() helper in @xata.io/client. Performing client-side transformations can be useful for generating images conditionally based on media queries within a browser. With this method, only a url for an existing Xata image is needed.

client_component.tsx
'use client';
import { transformImage } from '@xata.io/client';
 
// Once you have a URL to a Xata image, you can transform it and return a new URL
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  height: 100,
  rotate: 180,
  format: 'webp'
});

Once a transformation occurs, it is common to need the dimensions of the new image. The format=json option returns an JSON object containing dimensions for the transformed and original versions of the image.

TypeScript additionally provides metadataUrl and signedMetadataUrl (if using signed URLs). The below code contains common patterns for that method.

// Get a specific record from the "gallery" database
const record = await xata.db.gallery.read('record_id');
 
// Also grab the related meta data (height, width...etc) of the new image
const { url, metadataUrl } = record.imageColumnName.transform({
  height: 100,
  rotate: 180,
  format: 'webp'
});
 
const metadata = await fetchMetadata(metadataUrl);
 
console.log(metadata.height); // 100px
 
// Below is a TypeScript function useful for fetching metadata
 
interface OriginalImageInfo {
  file_size: number;
  width: number;
  height: number;
  format: string;
}
 
interface ImageMetadata {
  width: number;
  height: number;
  original: OriginalImageInfo;
}
 
export const fetchMetadata = async (metadataUrl: string): Promise<ImageMetadata | null> => {
  try {
    const response = await fetch(metadataUrl);
    if (!response.ok) {
      throw new Error('Failed to fetch metadata');
    }
    const data: ImageMetadata = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching metadata:', error);
    return null;
  }
};

If using the URL based transformations, you can still get post-transformation metadata by utilizing the format=json option, which you will need to do as a second call.

The following transformations are available:

Use to remove animation frames from input files. Default is set to true. Setting it to false reduces animations to still images. You will also need to pass at least one other transformation, like format: 'auto', for it to take effect.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/ai4u0uoq297518gmuqboschtcs', {
  anim: false,
  format: 'auto'
});

Original

original

Transformed

transformed

Use this to introduce a background color beneath the image. Applies to images with transparency. Accepts any CSS color, such as #RRGGBB and rgba(…).

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/a3r97ge71h3od25glq8ook1vg8', {
  background: 'pink'
});

Original

original

Transformed

transformed

Adds a blur radius ranging from 1 (slight blur) to 250 (maximum).

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  blur: 20
});

Original

original

Transformed

transformed

Increase brightness by a factor. A value of 1.0 equals no change, a value of 0.5 equals half brightness, and a value of 2.0 equals twice as bright. 0 is ignored.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  brightness: 0.5
});

Original

original

Transformed

transformed

This marginally reduces latency in case of a cache miss, by selecting the fastest-compressing file format. However, this comes at the expense of larger file sizes and diminished image quality. The only option is fast. It will usually override the format option and choose JPEG over WebP or AVIF.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  compression: 'fast'
});

Original

original

Transformed

transformed

Increase contrast by a factor. A value of 1.0 equals no change, a value of 0.5 equals low contrast, and a value of 2.0 equals high contrast. 0 is ignored.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  contrast: 0.5
});

Original

original

Transformed

transformed

Depending on the file type, some browsers preview the file instead of downloading it. Setting the download property will make the browser to always download the file. The value allows you to assign a name, and optionally an extension to the file that will be downloaded.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  download: 'xata-cat.jpg',
  format: 'jpeg'
});

This refers to the Device Pixel Ratio (DPR). The default is 1. This is a multiplier (similar to a flat scale) for width/height that makes it easier to specify higher-DPI sizes in img srcset. dpr needs to be used in tandem with a dimension transform to take effect.

// Note that the rendered example is actually 400px even though the original was 200px
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  dpr: 2,
  height: 200,
  fit: 'contain'
});

Original

original

Transformed

transformed

Affects interpretation of width and height. All resizing modes preserve aspect ratio.

Image will be resized (shrunk or enlarged) to be as large as possible within the given width or height while preserving the aspect ratio. If you only provide a single dimension (for example, only width), the image will be shrunk or enlarged to exactly match that dimension.

// Note that the rendered example retains the original aspect-ratio
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 100,
  height: 100,
  fit: 'contain'
});

Original

original

Transformed

transformed
// Note that the rendered example retains the original aspect-ratio, but doesn't grow beyond the original size
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 250,
  height: 250,
  fit: 'contain'
});

Original

original

Transformed

transformed

Similar to contain, but the image is never enlarged. If the image is larger than given width or height, it will be resized. Otherwise its original size will be kept.

// Notice that the rendered example scales down properly because the source is larger
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 100,
  height: 100,
  fit: 'scale-down'
});

Original

original

Transformed

transformed
// Notice that the rendered example does not grow beyond it's original size
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 600,
  height: 400,
  fit: 'scale-down'
});

Original

original

Transformed

transformed

Resizes (shrinks or enlarges) to fill the entire area of width and height. If the image has an aspect ratio different from the ratio of width and height, it will be cropped to

// Notice that the rendered example is a perfect square
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 100,
  height: 100,
  fit: 'cover'
});

Original

original

Transformed

transformed
// Notice that the rendered example is a perfect square that enlarges past its original size
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 250,
  height: 250,
  fit: 'cover'
});

Original

original

Transformed

transformed

Image will be shrunk and cropped to fit within the area specified by width and height. The image will not be enlarged. For images smaller than the given dimensions, it is the same as scale-down. For images larger than the given dimensions, it is the same as cover.

// Notice that the rendered example crops to a square because it is below the original size
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 100,
  height: 100,
  fit: 'crop'
});

Original

original

Transformed

transformed
// Notice that the rendered example does not become a square, and does not grow above the original size
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 500,
  height: 500,
  fit: 'crop'
});

Original

original

Transformed

transformed

Resizes to the maximum size that fits within the given width and height, and then fills the remaining area with a background color (white by default). This mode is not recommended, since you can achieve the same effect more efficiently with the contain mode and the CSS object-fit contain property.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 250,
  height: 250,
  fit: 'pad',
  background: 'pink'
});

Original

original

Transformed

transformed

Converts the format of the retrieved image.

  • auto Will serve the WebP or AVIF format to browsers that support it. If this option is not specified, a standard format like JPEG or PNG will be used.
  • avif Generate images in AVIF format if possible (with WebP as a fallback).
  • webp Generate images in Google WebP format. Set the quality to 100 to get the WebP lossless format.
  • json Instead of generating an image, outputs information about the image in JSON format. The JSON object will contain data such as image size (before and after resizing), source image’s MIME type, and file size.
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  format: 'webp'
});

Original

original

Transformed

transformed

Here is an example of outputting JSON, which will give you dimensions of the transformed image along with details from the original.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  height: 100,
  format: 'json'
});

Check the resulting JSON at https://us-east-1.storage.xata.sh/transform/format=json,height=100/4u1fh2o6p10blbutjnphcste94

{
  "width": 125,
  "height": 100,
  "original": { "file_size": 22684, "width": 250, "height": 200, "format": "image/jpeg" }
}

Increases exposure by a factor. A value of 1.0 equals no change, a value of 0.5 darkens the image, while a value of 2.0 lightens the image. 0 is ignored.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  gamma: 0.5
});

Original

original

Transformed

transformed

When cropping with fit: "cover" and fit: "crop", this parameter defines the side or point that should not be cropped. Available options are:

  • auto Selects focal point based on saliency detection (using maximum symmetric surround algorithm).
  • side A side ("left", "right", "top", "bottom") or coordinates specified on a scale from 0.0 (top or left) to 1.0 (bottom or right), 0.5 being the center. The X and Y coordinates are separated by lowercase x in the URL format. For example, 0x1 means left and bottom, 0.5x0.5 is the center, 0.5x0.33 is a point in the top third of the image.
// Crop from right
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 100,
  height: 100,
  fit: 'cover',
  gravity: 'right'
});

Original

original

Transformed

transformed
// Crop from left and bottom
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 100,
  height: 100,
  fit: 'cover',
  gravity: {
    x: 0,
    y: 1
  }
});

Original

original

Transformed

transformed

Specifies maximum height of the image in pixels. Exact behavior depends on the fit mode.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  height: 100
});

Original

original

Transformed

transformed

Controls amount of invisible metadata (EXIF data) that should be preserved. Color profiles and EXIF rotation are applied to the image even if the metadata is discarded. Note that if the Polish feature is enabled, all metadata may have been removed already and this option will have no effect. Options are:

  • keep Preserves most of EXIF metadata, including GPS location if present.
  • copyright Discard all metadata except EXIF copyright tag. This is the default behavior for JPEG images.
  • none Discard all invisible EXIF metadata. Currently, WebP and PNG output formats always discard metadata.
const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  metadata: 'none'
});

Specifies quality for images in JPEG, WebP, and AVIF formats. The quality is in a 1-100 scale, but useful values are between 50 (low quality, small file size) and 90 (high quality, large file size). 85 is the default.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  quality: 50
});

Original

original

Transformed

transformed

Specifies number of degrees (90, 180, or 270) to rotate the image by. Width and height options refer to axes after rotation.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  rotate: 90
});

Original

original

Transformed

transformed

Specifies strength of sharpening filter to apply to the image. The value is a floating-point number between 0 (no sharpening, default) and 10 (maximum). 1 is a recommended value for downscaled images.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  sharpen: 2
});

Original

original

Transformed

transformed

Specifies the number of pixels to cut off on each side. Allows removal of borders or cutting out a specific fragment of an image. Trimming is performed before resizing or rotation. Takes dpr into account.

const transformedUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  trim: {
    top: 20,
    right: 30,
    bottom: 20,
    left: 0
  }
});

Original

original

Transformed

transformed

Specifies maximum width of the image in pixels. Exact behavior depends on the fit mode.

const transformedImageUrl = transformImage('https://us-east-1.storage.xata.sh/4u1fh2o6p10blbutjnphcste94', {
  width: 100
});

Original

original

Transformed

transformed