Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
@sanity/image-url
Advanced tools
@sanity/image-url is a JavaScript client for generating URLs for images stored in Sanity's content lake. It allows you to build image URLs with various transformations such as resizing, cropping, and format conversion.
Basic URL Generation
This feature allows you to generate a basic URL for an image stored in Sanity. You need to provide the project ID and dataset, and then you can generate the URL for a specific image asset.
const imageUrlBuilder = require('@sanity/image-url');
const client = { projectId: 'yourProjectId', dataset: 'yourDataset' };
const builder = imageUrlBuilder(client);
const url = builder.image('image-asset-id').url();
console.log(url);
Image Resizing
This feature allows you to resize an image by specifying the width and height. The generated URL will point to the resized image.
const url = builder.image('image-asset-id').width(300).height(200).url();
console.log(url);
Image Cropping
This feature allows you to crop an image. You can specify different cropping options such as 'center', 'top', 'bottom', etc.
const url = builder.image('image-asset-id').crop('center').url();
console.log(url);
Format Conversion
This feature allows you to convert the image format. For example, you can convert an image to 'webp' format.
const url = builder.image('image-asset-id').format('webp').url();
console.log(url);
Cloudinary is a cloud-based service that provides an end-to-end image and video management solution. It offers similar functionalities such as URL generation, image resizing, cropping, and format conversion. However, Cloudinary is a more comprehensive service with additional features like video management, advanced image transformations, and a robust API.
Imgix is a real-time image processing service that allows you to manipulate images on the fly by changing URL parameters. It offers functionalities like resizing, cropping, and format conversion similar to @sanity/image-url. Imgix is known for its performance and extensive set of image manipulation options.
Sharp is a high-performance Node.js image processing library. It allows you to resize, crop, and convert images, similar to @sanity/image-url. Unlike @sanity/image-url, Sharp processes images locally rather than generating URLs for remote images.
Quickly generate image urls from Sanity image records.
This helper will by default respect any crops/hotspots specified in the Sanity content provided to it. The most typical use case for this is to give it a sanity image and specify a width, height or both and get a nice, cropped and resized image according to the wishes of the content editor and the specifications of the front end developer.
In addition to the core use case, this library provides a handy builder to access the rich selection of processing options available in the Sanity image pipeline.
npm install --save @sanity/image-url
The most common way to use this library in your project is to configure it by passing it your configured sanityClient. That way it will automatically be preconfigured to your current project and dataset:
import React from 'react'
import myConfiguredSanityClient from './sanityClient'
import imageUrlBuilder from '@sanity/image-url'
const builder = imageUrlBuilder(myConfiguredSanityClient)
function urlFor(source) {
return builder.image(source)
}
Then you can use the handy builder syntax to generate your urls:
<img src={urlFor(author.image).width(200).url()} />
This will ensure that the author image is always 200 pixels wide, automatically applying any crop specified by the editor and cropping towards the hot-spot she drew. You can specify both width and height like this:
<img src={urlFor(movie.poster).width(500).height(300).url()}>
There are a large number of useful options you can specify, like e.g. blur:
<img src={urlFor(mysteryPerson.mugshot).width(200).height(200).blur(50).url()}>
Note that the url()
function needs to be the final one in order to output the url as a string.
image(source)
Specify the image to be rendered. Accepts either a Sanity image
record, an asset
record, or just the asset id as a string. In order for hotspot/crop processing to be applied, the image
record must be supplied, as well as both width and height.
dataset(dataset)
, projectId(projectId)
Usually you should preconfigure your builder with dataset and project id, but even when you did, these let you temporarily override them if you need to render assets from other projects or datasets.
width(pixels)
Specify the width of the rendered image in pixels.
height(pixels)
Specify the height of the rendered image in pixels.
size(width, height)
Specify width and height in one go.
focalPoint(x, y)
Specify a center point to focus on when cropping the image. Values from 0.0 to 1.0 in fractions of the image dimensions. When specified, overrides any crop or hotspot in the image record.
blur(amount)
, sharpen(amount)
, invert()
Apply image processing.
rect(left, top, width, height)
Specify the crop in pixels. Overrides any crop/hotspot in the image record.
format(name)
Specify the image format of the image. 'jpg', 'pjpg', 'png', 'webp'
auto(mode)
Specify transformations to automatically apply based on browser capabilities. Supported values:
format
- Automatically uses WebP if supportedorientation(angle)
Rotation in degrees. Acceptable values: 0, 90, 180, 270
quality(value)
Compression quality, where applicable. 0-100
forceDownload(defaultFileName)
Make this an url to download the image. Specify the file name that will be suggested to the user.
flipHorizontal()
, flipVertical()
Flips the image.
crop(mode)
Specifies how to crop the image. When specified, overrides any crop or hotspot in the image record. See the documentation for details.
fit(value)
Configures the fit mode. See the documentation for details.
dpr(value)
Specifies device pixel ratio scaling factor. From 1 to 3.
saturation(value)
Adjusts the saturation of the image. Currently the only supported value is -100
- meaning it grayscales the image.
ignoreImageParams()
Ignore any specifications from the image record (i.e. crop and hotspot).
url()
, toString()
Return the url as a string.
pad(value)
Specify the number of pixels to pad the image.
vanityName(fileName)
Specify a "vanity name" for the image. This is useful for SEO purposes and is added to the end of the URL. For example:
urlFor('image-928ac96d53b0c9049836c86ff25fd3c009039a16-200x200-png').vanityName('myImage.png')
// https://cdn.sanity.io/…/928ac96d53b0c9049836c86ff25fd3c009039a16-200x200.png/myImage.png
frame(value)
Specify the frame of an animated image to transform. Acceptable values:
1
- Returns the first frame of the animated image as a static preview of the image.minWidth(pixels)
, maxWidth(pixels)
, minHeight(pixels)
, maxHeight(pixels)
Specifies min/max dimensions when cropping.
Deprecated: You usually want to use width
/height
with a fit mode of max
or min
instead.
ℹ️ This feature is available to select Enterprise accounts. Get in touch with your sales executive to learn more.
You can specify a custom baseUrl
in the builder options in order to override the default (https://cdn.sanity.io
):
import imageUrlBuilder from '@sanity/image-url'
const builder = imageUrlBuilder({
baseUrl: 'https://my.custom.domain',
projectId: 'abc123',
dataset: 'production',
})
const urlFor = (source) => builder.image(source)
urlFor('image-928ac96d53b0c9049836c86ff25fd3c009039a16-200x200-png')
.auto('format')
.fit('max')
.width(720)
.toString()
// output: https://my.custom.domain/images/abc123/production/928ac96d53b0c9049836c86ff25fd3c009039a16-200x200.png?auto=format&fit=max&w=720
If you already have a configured client instance:
import imageUrlBuilder from '@sanity/image-url'
import myConfiguredClient from './mySanityClient'
const builder = imageUrlBuilder({
...myConfiguredClient.config(),
baseUrl: 'https://my.custom.domain',
})
MIT © Sanity.io
FAQs
Tools to generate image urls from Sanity content
The npm package @sanity/image-url receives a total of 100,962 weekly downloads. As such, @sanity/image-url popularity was classified as popular.
We found that @sanity/image-url demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.