-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-handler.js
More file actions
353 lines (321 loc) · 15.2 KB
/
image-handler.js
File metadata and controls
353 lines (321 loc) · 15.2 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const sharp = require('sharp');
class ImageHandler {
constructor(s3, rekognition) {
this.s3 = s3;
this.rekognition = rekognition;
}
/**
* Main method for processing image requests and outputting modified images.
* @param {ImageRequest} request - An ImageRequest object.
*/
async process(request) {
let returnImage = '';
const originalImage = request.originalImage;
const edits = request.edits;
if (edits !== undefined && Object.keys(edits).length > 0) {
let image = null;
const keys = Object.keys(edits);
if (keys.includes('rotate') && edits.rotate === null) {
image = sharp(originalImage, { failOnError: false });
} else {
const metadata = await sharp(originalImage, { failOnError: false }).metadata();
if (metadata.orientation) {
image = sharp(originalImage, { failOnError: false }).withMetadata({ orientation: metadata.orientation });
} else {
image = sharp(originalImage, { failOnError: false }).withMetadata();
}
}
if (request.outputFormat === 'webp') {
const reductionEffortVal = process.env.WEBP_REDUCTION_EFFORT;
if (typeof reductionEffortVal !== "undefined") {
image = sharp(originalImage, { failOnError: false }).webp({reductionEffort: parseInt(reductionEffortVal, 10)});
}
}
const modifiedImage = await this.applyEdits(image, edits);
if (request.outputFormat !== undefined) {
modifiedImage.toFormat(request.outputFormat);
}
const bufferImage = await modifiedImage.toBuffer();
returnImage = bufferImage.toString('base64');
} else {
returnImage = originalImage.toString('base64');
}
// If the converted image is larger than Lambda's payload hard limit, throw an error.
const lambdaPayloadLimit = 6 * 1024 * 1024;
if (returnImage.length > lambdaPayloadLimit) {
throw {
status: '413',
code: 'TooLargeImageException',
message: 'The converted image is too large to return.'
};
}
return returnImage;
}
/**
* Applies image modifications to the original image based on edits
* specified in the ImageRequest.
* @param {Sharp} image - The original sharp image.
* @param {object} edits - The edits to be made to the original image.
*/
async applyEdits(image, edits) {
if (edits.resize === undefined) {
edits.resize = {};
edits.resize.fit = 'inside';
} else {
if (edits.resize.width) edits.resize.width = Math.round(Number(edits.resize.width));
if (edits.resize.height) edits.resize.height = Math.round(Number(edits.resize.height));
}
// Apply the image edits
for (const editKey in edits) {
const value = edits[editKey];
if (editKey === 'overlayWith') {
const metadata = await image.metadata();
let imageMetadata = metadata;
if (edits.resize) {
let imageBuffer = await image.toBuffer();
imageMetadata = await sharp(imageBuffer).resize({ edits: { resize: edits.resize }}).metadata();
}
const { bucket, key, wRatio, hRatio, alpha } = value;
const overlay = await this.getOverlayImage(bucket, key, wRatio, hRatio, alpha, imageMetadata);
const overlayMetadata = await sharp(overlay).metadata();
let { options } = value;
if (options) {
if (options.left !== undefined) {
let left = options.left;
if (isNaN(left) && left.endsWith('p')) {
left = parseInt(left.replace('p', ''));
if (left < 0) {
left = imageMetadata.width + (imageMetadata.width * left / 100) - overlayMetadata.width;
} else {
left = imageMetadata.width * left / 100;
}
} else {
left = parseInt(left);
if (left < 0) {
left = imageMetadata.width + left - overlayMetadata.width;
}
}
isNaN(left) ? delete options.left : options.left = left;
}
if (options.top !== undefined) {
let top = options.top;
if (isNaN(top) && top.endsWith('p')) {
top = parseInt(top.replace('p', ''));
if (top < 0) {
top = imageMetadata.height + (imageMetadata.height * top / 100) - overlayMetadata.height;
} else {
top = imageMetadata.height * top / 100;
}
} else {
top = parseInt(top);
if (top < 0) {
top = imageMetadata.height + top - overlayMetadata.height;
}
}
isNaN(top) ? delete options.top : options.top = top;
}
}
const params = [{ ...options, input: overlay }];
image.composite(params);
} else if (editKey === 'smartCrop') {
const options = value;
const imageBuffer = await image.toBuffer({resolveWithObject: true});
const boundingBox = await this.getBoundingBox(imageBuffer.data, options.faceIndex);
const cropArea = this.getCropArea(boundingBox, options, imageBuffer.info);
try {
image.extract(cropArea);
} catch (err) {
throw {
status: 400,
code: 'SmartCrop::PaddingOutOfBounds',
message: 'The padding value you provided exceeds the boundaries of the original image. Please try choosing a smaller value or applying padding via Sharp for greater specificity.'
};
}
} else if (editKey === 'roundCrop') {
const options = value;
const imageBuffer = await image.toBuffer({resolveWithObject: true});
let width = imageBuffer.info.width;
let height = imageBuffer.info.height;
//check for parameters, if not provided, set to defaults
const radiusX = options.rx && options.rx >= 0? options.rx : Math.min(width, height) / 2;
const radiusY = options.ry && options.ry >= 0? options.ry : Math.min(width, height) / 2;
const topOffset = options.top && options.top >= 0 ? options.top : height / 2;
const leftOffset = options.left && options.left >= 0 ? options.left : width / 2;
if(options)
{
const ellipse = Buffer.from(`<svg viewBox="0 0 ${width} ${height}"> <ellipse cx="${leftOffset}" cy="${topOffset}" rx="${radiusX}" ry="${radiusY}" /></svg>`);
const params = [{ input: ellipse, blend: 'dest-in' }];
let data = await image.composite(params).toBuffer();
image = sharp(data).withMetadata().trim();
}
} else if (editKey === 'contentModeration') {
const options = value;
const imageBuffer = await image.toBuffer({resolveWithObject: true});
const inappropriateContent = await this.detectInappropriateContent(imageBuffer.data, options);
const blur = options.hasOwnProperty('blur') ? Math.ceil(Number(options.blur)) : 50;
if(options && (blur >= 0.3 && blur <= 1000)) {
if(options.moderationLabels){
for(let item of inappropriateContent.ModerationLabels) {
if (options.moderationLabels.includes(item.Name)){
image.blur(blur);
break;
}
}
} else if(inappropriateContent.ModerationLabels.length) {
image.blur(blur);
}
}
} else {
image[editKey](value);
}
}
// Return the modified image
return image;
}
/**
* Gets an image to be used as an overlay to the primary image from an
* Amazon S3 bucket.
* @param {string} bucket - The name of the bucket containing the overlay.
* @param {string} key - The object keyname corresponding to the overlay.
* @param {number} wRatio - The width rate of the overlay image.
* @param {number} hRatio - The height rate of the overlay image.
* @param {number} alpha - The transparency alpha to the overlay.
* @param {object} sourceImageMetadata - The metadata of the source image.
*/
async getOverlayImage(bucket, key, wRatio, hRatio, alpha, sourceImageMetadata) {
const params = { Bucket: bucket, Key: key };
try {
const { width, height } = sourceImageMetadata;
const overlayImage = await this.s3.getObject(params).promise();
let resize = {
fit: 'inside'
}
// Set width and height of the watermark image based on the ratio
const zeroToHundred = /^(100|[1-9]?[0-9])$/;
if (zeroToHundred.test(wRatio)) {
resize['width'] = parseInt(width * wRatio / 100);
}
if (zeroToHundred.test(hRatio)) {
resize['height'] = parseInt(height * hRatio / 100);
}
// If alpha is not within 0-100, the default alpha is 0 (fully opaque).
if (zeroToHundred.test(alpha)) {
alpha = parseInt(alpha);
} else {
alpha = 0;
}
const convertedImage = await sharp(overlayImage.Body)
.resize(resize)
.composite([{
input: Buffer.from([255, 255, 255, 255 * (1 - alpha / 100)]),
raw: {
width: 1,
height: 1,
channels: 4
},
tile: true,
blend: 'dest-in'
}]).toBuffer();
return convertedImage;
} catch (err) {
throw {
status: err.statusCode ? err.statusCode : 500,
code: err.code,
message: err.message
};
}
}
/**
* Calculates the crop area for a smart-cropped image based on the bounding
* box data returned by Amazon Rekognition, as well as padding options and
* the image metadata.
* @param {Object} boundingBox - The boudning box of the detected face.
* @param {Object} options - Set of options for smart cropping.
* @param {Object} metadata - Sharp image metadata.
*/
getCropArea(boundingBox, options, metadata) {
const padding = (options.padding !== undefined) ? parseFloat(options.padding) : 0;
// Calculate the smart crop area
const cropArea = {
left : parseInt((boundingBox.Left * metadata.width) - padding),
top : parseInt((boundingBox.Top * metadata.height) - padding),
width : parseInt((boundingBox.Width * metadata.width) + (padding * 2)),
height : parseInt((boundingBox.Height * metadata.height) + (padding * 2)),
}
// Return the crop area
return cropArea;
}
/**
* Gets the bounding box of the specified face index within an image, if specified.
* @param {Sharp} imageBuffer - The original image.
* @param {Integer} faceIndex - The zero-based face index value, moving from 0 and up as
* confidence decreases for detected faces within the image.
*/
async getBoundingBox(imageBuffer, faceIndex) {
const params = { Image: { Bytes: imageBuffer }};
const faceIdx = (faceIndex !== undefined) ? faceIndex : 0;
try {
const response = await this.rekognition.detectFaces(params).promise();
if(response.FaceDetails.length <= 0) {
return {Height: 1, Left: 0, Top: 0, Width: 1};
}
let boundingBox = {};
//handle bounds > 1 and < 0
for (let bound in response.FaceDetails[faceIdx].BoundingBox)
{
if (response.FaceDetails[faceIdx].BoundingBox[bound] < 0 ) boundingBox[bound] = 0;
else if (response.FaceDetails[faceIdx].BoundingBox[bound] > 1) boundingBox[bound] = 1;
else boundingBox[bound] = response.FaceDetails[faceIdx].BoundingBox[bound];
}
//handle bounds greater than the size of the image
if (boundingBox.Left + boundingBox.Width > 1) {
boundingBox.Width = 1 - boundingBox.Left;
}
if (boundingBox.Top + boundingBox.Height > 1) {
boundingBox.Height = 1 - boundingBox.Top;
}
return boundingBox;
} catch (err) {
console.error(err);
if (err.message === "Cannot read property 'BoundingBox' of undefined") {
throw {
status: 400,
code: 'SmartCrop::FaceIndexOutOfRange',
message: 'You have provided a FaceIndex value that exceeds the length of the zero-based detectedFaces array. Please specify a value that is in-range.'
};
} else {
throw {
status: err.statusCode ? err.statusCode : 500,
code: err.code,
message: err.message
};
}
}
}
/**
* Detects inappropriate content in an image.
* @param {Sharp} imageBuffer - The original image.
* @param {Object} options - The options to pass to the dectectModerationLables Rekognition function
*/
async detectInappropriateContent(imageBuffer, options) {
const params = {
Image: {Bytes: imageBuffer},
MinConfidence: options.minConfidence ? parseFloat(options.minConfidence) : 75
}
try {
const response = await this.rekognition.detectModerationLabels(params).promise();
return response;
} catch(err) {
console.error(err)
throw {
status: err.statusCode ? err.statusCode : 500,
code: err.code,
message: err.message
}
}
}
}
// Exports
module.exports = ImageHandler;