Skip to main content

resizeImage

resizeImage is a JavaScript equivalent of Shopify's Liquid | img_url filter. It takes a Shopify CDN image URL and returns a new URL pointing at a resized (and optionally cropped/scaled) version of that image.

It accepts a single options object:

PropertyTypeDefaultDescription
srcstringThe Shopify CDN URL of the image.
sizestringThe size you want, e.g. '300x300'. Pass 'master' for the original.
cropstring \| nullnullOptional crop position, e.g. 'center', 'top', 'bottom'.
scalenumber \| nullnullOptional pixel density, e.g. 2 for a @2x image.
note

If size is not passed, the original src is returned unchanged. If the src doesn't look like a valid image URL, null is returned.

Usage examples

Basic

const url = window.BAO.utils.resizeImage({
src: 'https://cdn.shopify.com/s/files/1/0000/0000/products/shirt.jpg',
size: '300x300',
})
// https://cdn.shopify.com/s/files/1/0000/0000/products/shirt_300x300.jpg

With crop and scale

const url = window.BAO.utils.resizeImage({
src: 'https://cdn.shopify.com/s/files/1/0000/0000/products/shirt.jpg',
size: '600x600',
crop: 'center',
scale: 2,
})
// .../shirt_600x600_crop_center@2x.jpg