-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathconstructUrl.js
161 lines (132 loc) · 3.33 KB
/
constructUrl.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
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
156
157
158
159
160
161
/*
Copyright © 2015 by Coursera
Licensed under the Apache License 2.0, seen https://github.com/coursera/react-imgix/blob/master/LICENSE
Minor syntax modifications have been made
*/
var Base64 = require("js-base64").Base64;
const PACKAGE_VERSION = require("../package.json").version;
import extractQueryParams from "./extractQueryParams";
// @see https://www.imgix.com/docs/reference
var PARAM_EXPANSION = Object.freeze({
// Adjustment
brightness: "bri",
contrast: "con",
exposure: "exp",
gamma: "gam",
highlights: "high",
hue: "hue",
invert: "invert",
saturation: "sat",
shaddows: "shad",
sharpness: "sharp",
"unsharp-mask": "usm",
"unsharp-radius": "usmrad",
vibrance: "vib",
// Automatic
"auto-features": "auto",
// Background
"background-color": "bg",
// Blend
blend: "blend",
"blend-mode": "bm",
"blend-align": "ba",
"blend-alpha": "balph",
"blend-padding": "bp",
"blend-width": "bw",
"blend-height": "bh",
"blend-fit": "bf",
"blend-crop": "bc",
"blend-size": "bs",
"blend-x": "bx",
"blend-y": "by",
// Border & Padding
border: "border",
padding: "pad",
// Face Detection
"face-index": "faceindex",
"face-padding": "facepad",
faces: "faces",
// Format
"chroma-subsampling": "chromasub",
"color-quantization": "colorquant",
download: "dl",
DPI: "dpi",
format: "fm",
"lossless-compression": "lossless",
quality: "q",
// Mask
"mask-image": "mask",
// Mask
"noise-blur": "nr",
"noise-sharpen": "nrs",
// Palette n/a
// PDF n/a
// Pixel Density n/a
// Rotation
"flip-direction": "flip",
orientation: "or",
"rotation-angle": "rot",
// Size
"crop-mode": "crop",
"fit-mode": "fit",
"image-height": "h",
"image-width": "w",
// Stylize
blurring: "blur",
halftone: "htn",
monotone: "mono",
pixelate: "px",
"sepia-tone": "sepia",
// Trim TODO
// Watermark TODO
// Extra
height: "h",
width: "w",
});
var DEFAULT_OPTIONS = Object.freeze({
auto: "format", // http://www.imgix.com/docs/reference/automatic#param-auto
});
/**
* Construct a URL for an image with an Imgix proxy, expanding image options
* per the [API reference docs](https://www.imgix.com/docs/reference).
* @param {String} src src of raw image
* @param {Object} longOptions map of image API options, in long or short form per expansion rules
* @return {String} URL of image src transformed by Imgix
*/
function constructUrl(src, longOptions) {
if (!src) {
return "";
}
const keys = Object.keys(longOptions);
const keysLength = keys.length;
let url = src + "?";
for (let i = 0; i < keysLength; i++) {
let key = keys[i];
let val = longOptions[key];
if (PARAM_EXPANSION[key]) {
key = PARAM_EXPANSION[key];
} else {
key = encodeURIComponent(key);
}
if (key.substr(-2) === "64") {
val = Base64.encodeURI(val);
}
url += key + "=" + encodeURIComponent(val) + "&";
}
return url.slice(0, -1);
}
function buildURLPublic(src, imgixParams = {}, options = {}) {
const { disableLibraryParam } = options;
const [rawSrc, params] = extractQueryParams(src);
return constructUrl(
rawSrc,
Object.assign(
{},
params,
imgixParams,
disableLibraryParam ? {} : { ixlib: `react-${PACKAGE_VERSION}` }
)
);
}
export default constructUrl;
export { buildURLPublic };