This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
image-size.ts
155 lines (132 loc) · 4.61 KB
/
image-size.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type {Image as ImageType} from './storefront-api-types.js';
import type {PartialDeep} from 'type-fest';
import type {ShopifyLoaderOptions, ShopifyLoaderParams} from './Image.js';
// TODO: Are there other CDNs missing from here?
const PRODUCTION_CDN_HOSTNAMES = [
'cdn.shopify.com',
'cdn.shopifycdn.net',
'shopify-assets.shopifycdn.com',
'shopify-assets.shopifycdn.net',
];
const LOCAL_CDN_HOSTNAMES = ['spin.dev'];
const ALL_CDN_HOSTNAMES = [...PRODUCTION_CDN_HOSTNAMES, ...LOCAL_CDN_HOSTNAMES];
// based on the default width sizes used by the Shopify liquid HTML tag img_tag plus a 2560 width to account for 2k resolutions
// reference: https://shopify.dev/api/liquid/filters/html-filters#image_tag
export const IMG_SRC_SET_SIZES = [352, 832, 1200, 1920, 2560];
/**
* Adds image size parameters to an image URL hosted by Shopify's CDN
*/
export function addImageSizeParametersToUrl({
src,
width,
height,
crop,
scale,
}: ShopifyLoaderParams) {
const newUrl = new URL(src);
const multipliedScale = scale ?? 1;
if (width) {
let finalWidth: string;
if (typeof width === 'string') {
finalWidth = (IMG_SRC_SET_SIZES[0] * multipliedScale).toString();
} else {
finalWidth = (Number(width) * multipliedScale).toString();
}
newUrl.searchParams.append('width', finalWidth);
}
if (height && typeof height === 'number') {
newUrl.searchParams.append('height', (height * multipliedScale).toString());
}
crop && newUrl.searchParams.append('crop', crop);
// for now we intentionally leave off the scale param, and instead multiple width & height by scale instead
// scale && newUrl.searchParams.append('scale', scale.toString());
return newUrl.toString();
}
export function shopifyImageLoader(params: ShopifyLoaderParams) {
const newSrc = new URL(params.src);
const isShopifyServedImage = ALL_CDN_HOSTNAMES.some((allowedHostname) =>
newSrc.hostname.endsWith(allowedHostname)
);
if (
!isShopifyServedImage ||
(!params.width && !params.height && !params.crop && !params.scale)
) {
return params.src;
}
return addImageSizeParametersToUrl(params);
}
type HtmlImageProps = React.ImgHTMLAttributes<HTMLImageElement>;
export type GetShopifyImageDimensionsProps = {
data: Pick<
PartialDeep<ImageType, {recurseIntoArrays: true}>,
'altText' | 'url' | 'id' | 'width' | 'height'
>;
loaderOptions?: ShopifyLoaderOptions;
elementProps?: {
width?: HtmlImageProps['width'];
height?: HtmlImageProps['height'];
};
};
type GetShopifyImageDimensionsPropsReturn = {
width: number | string | null;
height: number | string | null;
};
/**
* Width and height are determined using the followiing priority list:
* 1. `loaderOptions`'s width/height
* 2. `elementProps`'s width/height
* 3. `data`'s width/height
*
* If only one of `width` or `height` are defined, then the other will attempt to be calculated based on the Image's aspect ratio,
* provided that both `data.width` and `data.height` are available. If not, then the aspect ratio cannot be determined and the missing
* value will reamin as `null`
*/
export function getShopifyImageDimensions({
data: sfapiImage,
loaderOptions,
elementProps,
}: GetShopifyImageDimensionsProps): GetShopifyImageDimensionsPropsReturn {
let aspectRatio: number | null = null;
if (sfapiImage?.width && sfapiImage?.height) {
aspectRatio = sfapiImage?.width / sfapiImage?.height;
}
// * 1. `loaderOptions`'s width/height
if (loaderOptions?.width || loaderOptions?.height) {
return {
width:
loaderOptions?.width ??
(aspectRatio && typeof loaderOptions.height === 'number'
? Math.round(aspectRatio * loaderOptions.height)
: null),
height:
loaderOptions?.height ??
(aspectRatio && typeof loaderOptions.width === 'number'
? Math.round(aspectRatio * loaderOptions.width)
: null),
};
}
// * 2. `elementProps`'s width/height
if (elementProps?.width || elementProps?.height) {
return {
width:
elementProps?.width ??
(aspectRatio && typeof elementProps.height === 'number'
? Math.round(aspectRatio * elementProps.height)
: null),
height:
elementProps?.height ??
(aspectRatio && typeof elementProps.width === 'number'
? Math.round(aspectRatio * elementProps.width)
: null),
};
}
// * 3. `data`'s width/height
if (sfapiImage?.width || sfapiImage?.height) {
return {
// can't calculate the aspect ratio here
width: sfapiImage?.width ?? null,
height: sfapiImage?.height ?? null,
};
}
return {width: null, height: null};
}