Skip to main content

getImageUrl

Returns the absolute URL of a resized copy of an image, served by the store's image endpoint /images. Use it where an image must have a fixed size or format and an absolute URL: emails, social sharing tags (og:image) and feeds.

The endpoint reads the image (from local storage, or from the full URL of a cloud storage), resizes it, converts it and caches the result.

A full URL is fetched only from an allowed host

For a full URL, the endpoint fetches only from a host on the image allowlist. The S3, Google Cloud Storage and Azure hosts of your storage settings are allowed automatically. Any other host (Cloudinary, a CDN, a storage extension) must be added with IMAGE_ALLOWED_HOSTS or registerAllowedImageHostsProvider, or the endpoint answers 404. See The /images proxy allowlist. To use the original file without the endpoint, use getAssetUrl.

Import​

import { getImageUrl } from '@evershop/evershop/lib/util/getImageUrl';

Syntax​

getImageUrl(
src: string,
options: {
width: number;
height?: number;
quality?: number;
format?: 'jpeg' | 'png' | 'webp' | 'avif';
}
): string

Parameters​

src

Type: string

The image's stored value, as it is: a site-relative path (local storage) or a full URL (any other storage, from an allowed host). Do not add the base URL to it.

options

OptionTypeDescription
widthnumberRequired. The width in pixels.
heightnumberOptional. With it, the image is scaled to fit inside width × height and padded to exactly that size: transparent for png, webp and avif, black for jpeg. Without it, only the width is set and there is no padding.
qualitynumberOptional, 1 to 100. The endpoint's default is 75.
formatstringOptional: jpeg, png, webp or avif. The endpoint's default is webp. Use png or jpeg in emails, because some email clients do not show WebP or AVIF.

Return Value​

The absolute URL, for example https://shop.com/images?src=%2Fassets%2Fcatalog%2Ftee.jpg&w=1200&q=80&h=675&f=png.

The parameters always come in the same order, so one image with the same options always gets the same URL. An option that is undefined or null is left out. The URL does not depend on the loaded routes, so it works in any process (the web app, subscribers, cron jobs, scripts).

Examples​

A social sharing image​

origin_image is the stored value of a row in the product_image table. With GraphQL data, use product.image.url.

import { getImageUrl } from '@evershop/evershop/lib/util/getImageUrl';

const image = getImageUrl(productImage.origin_image, {
width: 1200,
height: 675,
quality: 80,
format: 'png'
});

A logo in an email​

import { getImageUrl } from '@evershop/evershop/lib/util/getImageUrl';

const logoSrc = getImageUrl(logo, { width: 400, quality: 85, format: 'png' });

Notes​

  • In a React component, build the relative URL (/images?src=…), or use the Image component. getImageUrl reads the server configuration, so it cannot run in the browser.
  • For the original file without resizing, use getAssetUrl.

See Also​