APIs and SDKs

Presenting Images

Presenting images through the Sanity image pipeline

Sanity provides a powerful image pipeline that gives you access to a globally-distributed asset CDN. This allows you to request images in various sizes, crops, and formats on demand, with assets automatically cached close to your users for optimal performance.

This guide explains how to effectively present and transform images from your Sanity Content Lake in your front-end applications. For information about how assets are stored or uploaded, see the Assets documentation.

Prerequisites:

  • A Sanity project with images uploaded to the Content Lake.
  • Understanding of your front-end framework of choice.
  • Familiarity with your schema.

Understanding image representation in Sanity

Before implementing images in your front-end, it's important to understand how Sanity handles images. In Sanity:

  • The image type represents an image used as a field in a document or embedded in block text.
  • Images may contain additional fields like caption, credits, and specific crop and hotspot information.
  • Each image references an asset which contains the actual image data.
  • One asset can be shared by multiple image instances, allowing editors to reuse assets with different crops, captions, etc.

Media Library

Get the image URL

The simplest way to display images from Sanity is to get the base URL of the image from the asset, then transform it. Here's how to query for an image URL:

Gotcha

With the base URL, you can now append options in order to constrain size, crop the image, blur it or perform other operations on it. Here are some examples:

Gotcha

See the image url reference documentation for the full list of parameters.

Use the image URL builder

For JavaScript projects, Sanity provides the @sanity/image-url package that generates image URLs while respecting crop and hotspot settings.

image-url returns a URL

It uses a method-chaining style syntax that lets you define image transformations.

Install the package

Configure the builder

Next, configure the builder by passing a configured @sanity/client to the imageUrlBuilder. This example exports a reusable urlFor function. Check the ImageComponent example to see how you

The builder accepts an image source, an asset source, or a string containing the asset's ID. If you supply the image or asset source, the builder will automatically default to respecting any crop and hot-spot settings.

Learn more about the @sanity/image-url in the library documentation.

The crop and hot-spot

Sanity Studio allows editors, if enabled in the schema, to define crop regions and hotspots for images. The crop describes which part of the image the editor wants to allow to be used, while the hot-spot specifies what area to preserve when the image needs to be cropped additionally in a front end.

Hotspot and crop UI

When an editor sets these, the image record might look like this:

The editor expects your front end to respect these settings. You can manually build the URL using the crop and hotspot data along with the pipeline's transformation options, but the @sanity/image-url library handles this automatically for you.

For example, using the urlFor helper from the earlier code example, you can render an image in two sizes.

import {urlFor} from './sanityImageUrl'

function ProfileImage({person}) {
  // The image builder automatically applies crop and hotspot settings
  return (
    <img 
      src={urlFor(person.image)
        .width(800)
        .height(600)
        .url()}
      alt={person.image.alt || `Portrait of ${person.name}`}
    />
  )
}

// For square thumbnails
function AvatarImage({person}) {
  return (
    <img 
      src={urlFor(person.image)
        .width(200)
        .height(200)
        .url()}
      alt={person.image.alt || `Avatar of ${person.name}`}
      className="avatar"
    />
  )
}

The URL builder applies the editor's crop settings, then uses the hotspot to determine a focal point if additional cropping is needed based on the requested dimensions.

Creating downloadable image links

Sometimes you want to change the headers on the URL so that when clicked, the file will download instead of displaying in the browser. You can do this with both the urlFor helper from the previous examples and with URL parameters.

// Using the URL builder
const downloadUrl = urlFor(image)
  .width(1200)
  .url() + '&dl=filename.jpg'

// Raw URL equivalent
const baseUrl = 'https://cdn.sanity.io/images/zp7mbokg/production/G3i4emG6B8JnTmGoN0UjgAp8-300x450.jpg'
const downloadUrlRaw = `${baseUrl}?w=1200&dl=filename.jpg`;

// Use the default filename by omitting a filename
const defaultFilename = `${baseUrl}?dl=`

Using dl= without a filename will use the original filename from time of upload, assuming storeOriginalFilename wasn't disabled in the schema. Otherwise, it will use the asset ID.

Vanity filenames

Sometimes you want to display an image normally, but adjust the pre-set filename that users see when they download or copy an image. You can append /your-new-filename.jpg with the text and extension of your choice to update the name. For example:

https://cdn.sanity.io/images/y856rro4/production/6005c6a1da9e27b033589ef439f8bb8f38420933-5152x7728.jpg/vanity-filename.jpg

Performance considerations

To maximize performance, reuse image transformations across your front end. This ensures that cached assets are reused effectively.

Pair image transformations with responsive images. For example:

Additional resources

Was this page helpful?