-
Notifications
You must be signed in to change notification settings - Fork 17
/
buildImageUrl.js
46 lines (34 loc) · 1.01 KB
/
buildImageUrl.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
37
38
39
40
41
42
43
44
45
46
import { STORYBLOK_BASE_URL } from '../defaults'
import { applyFilters } from './helpers'
function buildImageUrl(originalPath, image) {
let { width, height, smartCrop, quality, format, fill } = image
let [, extension] = originalPath.split('.')
let url = STORYBLOK_BASE_URL
if (width && height) {
url += `/${width}x${height}`
}
if (smartCrop) {
url += `/smart`
}
let filters = [
...[quality && `quality(${quality})`],
...[format && format !== extension && `format(${format})`],
...[fill && `fill(${fill})`]
]
// remove falsy elements
filters = filters.filter(element => Boolean(element) === true)
if (filters.length > 0) {
url += applyFilters(filters)
}
// add original path at the end
url += `/${originalPath}`
return url
}
export function buildLowFiUrl(originalPath, {width, height, aspectRatio }) {
return buildImageUrl(originalPath, {
width: (width / 3).toFixed(0),
height: (height / 3).toFixed(0),
quality: 10
})
}
export default buildImageUrl