-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcropping.ts
More file actions
278 lines (231 loc) · 8.06 KB
/
cropping.ts
File metadata and controls
278 lines (231 loc) · 8.06 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { normalizeNumberParameter } from "@cloudinary-util/util";
import type {
AspectRatio,
CropMode,
Gravity,
Height,
Width,
X,
Y,
Zoom,
} from "../constants/parameters.js";
import { plugin } from "../lib/plugin.js";
import { isArray } from "../lib/utils.js";
import type { PluginResults } from "../types/plugins.js";
const cropsAspectRatio = ["auto", "auto_pad", "crop", "fill", "lfill", "fill_pad", "thumb"];
const cropsGravityAuto = ["auto", "auto_pad", "crop", "fill", "lfill", "fill_pad", "thumb"];
const cropsWithZoom = ["crop", "thumb"];
const DEFAULT_CROP = "limit";
export declare namespace CroppingPlugin {
export interface Options {
aspectRatio?: AspectRatio;
crop?: CropMode | NestedOptions | ReadonlyArray<NestedOptions>;
gravity?: Gravity;
zoom?: Zoom;
/**
* @description Height of the given asset.
*/
height?: string | number;
/**
* @description Width of the given asset.
*/
width?: string | number;
}
export interface NestedOptions {
type: CropMode;
aspectRatio?: AspectRatio;
gravity?: Gravity;
height?: Height;
width?: Width;
x?: X;
y?: Y;
zoom?: Zoom;
source?: boolean;
}
}
export const CroppingPlugin = /* #__PURE__ */ plugin({
name: "Cropping",
supports: "all",
inferOwnOptions: {} as CroppingPlugin.Options,
props: {
aspectRatio: true,
crop: true,
gravity: true,
zoom: true,
height: true,
width: true,
},
// crop is applied even if the crop key is undefined
apply: (asset, opts) => {
let crops: Array<CroppingPlugin.NestedOptions> = [];
// Normalize the data that we're working with for simpler processing
if (typeof opts.crop === "string" || typeof opts.crop === "undefined") {
// If we have a type of string or we don't explicitly
// have a crop set (default is limit) we're using the
// default pattern of resizing/cropping after all
// of the transformations
crops.push({
aspectRatio: opts.aspectRatio,
height: opts.height,
gravity: opts.gravity,
type: opts.crop || DEFAULT_CROP,
width: opts.width,
zoom: opts.zoom,
});
} else if (typeof opts.crop === "object" && !isArray(opts.crop)) {
crops.push(opts.crop);
} else if (isArray(opts.crop)) {
crops = [...opts.crop];
}
// We always need a post-transformation to resize the image, whether that uses the
// default crop mode of "limit" or something user set, so if we dont have anything
// matching that criteria, push on a "default" using whatever data we can find
if (crops.length === 1 && crops[0].source === true) {
crops.push({
aspectRatio: opts.aspectRatio,
width: opts.width,
height: opts.height,
gravity: opts.gravity,
type: DEFAULT_CROP,
zoom: opts.zoom,
});
}
// Start working through the different crop options and determine whether they're
// pre-transformation (source) or post-transformation (final)
const finalTransformations: Array<Array<string>> = [];
const sourceTransformations: Array<Array<string>> = [];
for (const crop of crops) {
const cropDimensions = {
width: crop.width,
height: crop.height,
};
if (
typeof cropDimensions.width === "undefined" &&
typeof crop.aspectRatio === "undefined"
) {
cropDimensions.width = opts.width;
// We likely don't want to infer one dimension and not the other
// so only infer the height if we're already inferring the width
if (typeof cropDimensions.height === "undefined") {
cropDimensions.height = opts.height;
}
}
const transformations = collectTransformations({
aspectRatio: crop.aspectRatio,
gravity: crop.gravity,
type: crop.type || DEFAULT_CROP,
x: crop.x,
y: crop.y,
zoom: crop.zoom,
...cropDimensions,
});
// A source of true means we want to apply the transformations
// to the original source image
if (transformations.length > 0) {
if (crop.source === true) {
sourceTransformations.push(transformations);
} else {
finalTransformations.push(transformations);
}
}
}
// This stage provides the option for someone to crop and resize the image before any transformations
// are applied. this could be handy if you want a consistent size on different assets to share
// transformations for instance
sourceTransformations.forEach((transformation) => {
if (transformation.length > 0) {
asset.addTransformation(transformation.join(","));
}
});
// If we have any overrides, which are the the standard width/height options, apply
const results: PluginResults = {
options: {},
};
if (results.options && finalTransformations.length > 0) {
results.options.resize = finalTransformations
.map((transformation) => transformation.join(","))
.join("/");
}
return results;
},
});
/**
* CollectTransformations
* @description Given the avialable crop options, returns an array of transformation strings
*/
function collectTransformations(collectOptions: CroppingPlugin.NestedOptions) {
// Default the crop to "limit" to avoid upscaling
// This avoid further distorting the image since the browser will resize in that case.
// If caller wants actual resize, can explicitly pass in "scale".
const { aspectRatio, type: crop, x, y, zoom } = collectOptions;
// Normalize sizing parameters
let gravity = collectOptions.gravity;
const height = normalizeNumberParameter(collectOptions.height);
const width = normalizeNumberParameter(collectOptions.width);
const transformations = [];
const hasDefinedDimensions = height || width;
const hasValidAspectRatio = aspectRatio && cropsAspectRatio.includes(crop);
const hasXCoordinate = typeof x === "number" || typeof x === "string";
const hasYCoordinate = typeof y === "number" || typeof y === "string";
const hasDefinedCoordinates = hasXCoordinate || hasYCoordinate;
// Only apply a crop if we're defining some type of dimension attribute
// where the crop would make sense
if (
crop &&
(hasDefinedDimensions || hasValidAspectRatio || hasDefinedCoordinates)
) {
transformations.push(`c_${crop}`);
}
// Aspect Ratio requires a crop mode to be applied so we want to make
// sure a valid one is included
if (hasValidAspectRatio) {
transformations.push(`ar_${aspectRatio}`);
}
if (width) {
transformations.push(`w_${width}`);
}
// Skip explicit height when using aspect ratio (which determines height automatically)
// Otherwise, include explicit height when provided
if (typeof height === "number" && !hasValidAspectRatio) {
transformations.push(`h_${height}`);
}
if (hasXCoordinate) {
transformations.push(`x_${x}`);
}
if (hasYCoordinate) {
transformations.push(`y_${y}`);
}
// Gravity of auto only applies to certain crop types otherewise
// errors, so default to auto only when crop matches type.
// If the user is providing x or y coordinates, we also don't want
// to default to auto, as that will skew the intuitive results
if (!gravity && cropsGravityAuto.includes(crop) && !hasDefinedCoordinates) {
gravity = "auto";
}
// If we have gravity, apply it, but check that the gravity passed
// in doesn't conflict with the crop mode
if (gravity) {
if (gravity === "auto" && !cropsGravityAuto.includes(crop)) {
console.warn(
`Auto gravity can only be used with crop modes: ${cropsGravityAuto.join(
", "
)}. Not applying gravity.`
);
} else {
transformations.push(`g_${gravity}`);
}
}
// Some zoom types don't work with some crop types
if (zoom) {
if (zoom === "auto" && !cropsWithZoom.includes(crop)) {
console.warn(
`Zoom can only be used with crop modes: ${cropsWithZoom.join(
", "
)}. Not applying zoom.`
);
} else {
transformations.push(`z_${zoom}`);
}
}
return transformations;
}