-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathimage-helpers.js
36 lines (31 loc) · 1.24 KB
/
image-helpers.js
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
/**
* @namespace ImageHelpers
*/
export default {
/**
* Generates the image src for a resized image with maximum dimensions `maxWidth` and `maxHeight`.
* Images do not scale up.
*
* @example
* const url = client.image.helpers.imageForSize(product.variants[0].image, {maxWidth: 50, maxHeight: 50});
*
* @memberof ImageHelpers
* @method imageForSize
* @param {Object} image The original image model to generate the image src for.
* @param {Object} options An options object containing:
* @param {Integer} options.maxWidth The maximum width for the image.
* @param {Integer} options.maxHeight The maximum height for the image.
* @return {String} The image src for the resized image.
*/
imageForSize(image, {maxWidth, maxHeight}) {
const splitUrl = image.src.split('?');
const notQuery = splitUrl[0];
const query = splitUrl[1] ? `?${splitUrl[1]}` : '';
// Use the section before the query
const imageTokens = notQuery.split('.');
// Take the token before the file extension and append the dimensions
const imagePathIndex = imageTokens.length - 2;
imageTokens[imagePathIndex] = `${imageTokens[imagePathIndex]}_${maxWidth}x${maxHeight}`;
return `${imageTokens.join('.')}${query}`;
}
};