This repository was archived by the owner on Jan 25, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
238 lines (210 loc) · 8.93 KB
/
Copy pathindex.js
File metadata and controls
238 lines (210 loc) · 8.93 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
var sharp = require('sharp'),
AWS = require('aws-sdk'),
path = require('path'),
isAnimated = require('animated-gif-detector'),
smartcrop = require('smartcrop-sharp');
var regions = {};
module.exports = {};
module.exports.s3 = function(config, key, args, callback) {
AWS.config.region = config.region;
var s3config = {};
if (config.endpoint) {
s3config.endpoint = config.endpoint;
s3config.s3ForcePathStyle = true;
}
if (!regions[config.region]) {
regions[config.region] = new AWS.S3(
Object.assign({ region: config.region }, s3config)
);
}
var s3 = regions[config.region];
return s3.makeUnauthenticatedRequest(
'getObject',
{ Bucket: config.bucket, Key: key },
function(err, data) {
if (err) {
return callback(err);
}
args.key = key;
return module.exports.resizeBuffer(data.Body, args, callback);
}
);
};
var getDimArray = function( dims, zoom ) {
var dimArr = typeof dims === 'string' ? dims.split(',') : dims;
zoom = zoom || 1;
return dimArr.map(function(v) {
return (Number(v) * zoom) || null;
});
}
var clamp = function( val, min, max ) {
return Math.min( Math.max( Number( val ), min ), max );
}
module.exports.resizeBuffer = function(buffer, args, callback) {
return new Promise(function(resolve, reject) {
try {
var image = sharp(buffer).withMetadata();
var metadata = function(crop) {
return image.metadata(function(err, metadata) {
if (err) {
reject(err);
if (callback) {
callback(err);
}
return;
}
// auto rotate based on orientation exif data
image.rotate();
// convert gifs to pngs unless animated
if (
args.key &&
path.extname(args.key).toLowerCase() === '.gif'
) {
if (isAnimated(buffer)) {
return callback(new Error('fallback-to-original'));
} else {
image.png();
}
}
// crop (assumes crop data from original)
if (args.crop) {
var cropValues =
typeof args.crop === 'string'
? args.crop.split(',')
: args.crop;
// convert percentages to px values
cropValues = cropValues.map(function(value, index) {
if (value.indexOf('px') > -1) {
return Number(value.substr(0, value.length - 2));
} else {
return Number(
Number(
metadata[index % 2 ? 'height' : 'width'] *
(value / 100)
).toFixed(0)
);
}
});
// avoid fatal error if there are not 4 cropValues that are valid numbers
numericCrops = function( value ) { return !isNaN( value ) };
if ( 4 === cropValues.filter( numericCrops ).length ) {
console.log(cropValues);
image.extract({
left: cropValues[0],
top: cropValues[1],
width: cropValues[2],
height: cropValues[3],
});
}
}
// get zoom value
var zoom = parseFloat( args.zoom ) || 1;
// resize
if (args.resize) {
args.resize = getDimArray( args.resize, zoom );
// apply cropping strategies
if ( args.gravity ) {
image.crop( args.gravity );
}
if ( args.crop_strategy === 'attention' ) {
image.crop( sharp.strategy.attention );
}
if ( args.crop_strategy === 'entropy' ) {
image.crop( sharp.strategy.entropy );
}
if ( args.crop_strategy === 'smart' && crop ) {
image.extract({
left: crop.x,
top: crop.y,
width: crop.width,
height: crop.height,
});
}
// apply the resize
image.resize.apply(
image,
args.resize
);
} else if (args.fit) {
args.fit = getDimArray( args.fit, zoom );
image.resize.apply(
image,
args.fit
);
image.max();
} else if (args.lb) {
args.lb = getDimArray( args.lb, zoom );
image.resize.apply(
image,
args.lb
);
// default to a black background to replicate Photon API behaviour
// when no background colour specified
if (!args.background) {
args.background = 'black';
}
image.background(args.background);
image.embed();
} else if (args.w || args.h) {
image.resize(
(Number(args.w) * zoom) || null,
(Number(args.h) * zoom) || null
);
if (!args.crop) {
image.max();
}
}
// return a default compression value based on a logarithmic scale
// defaultValue = 100, zoom = 2; = 65
// defaultValue = 80, zoom = 2; = 50
// defaultValue = 100, zoom = 1.5; = 86
// defaultValue = 80, zoom = 1.5; = 68
var applyZoomCompression = function( defaultValue, zoom ) {
return clamp( Math.round( defaultValue - ( (Math.log(zoom) / Math.log(defaultValue / zoom)) * (defaultValue * zoom) ) ), Math.round(defaultValue / zoom), defaultValue );
}
// set default quality slightly higher than sharp's default
if ( ! args.quality ) {
args.quality = applyZoomCompression( 82, zoom );
}
// allow override of compression quality
if (args.webp) {
image.webp({
quality: Math.round( clamp( args.quality, 0, 100 ) ),
});
} else if (metadata.format === 'jpeg') {
image.jpeg({
quality: Math.round( clamp( args.quality, 0, 100 ) ),
});
}
// send image
return image.toBuffer(function(err, _data, info) {
if (err) {
reject(err);
if (callback) {
callback(err);
}
return;
}
resolve({ data: _data, info: info });
if (callback) {
callback(err, _data, info);
}
return;
});
});
}
// handle smartcrop promise
if ( args.crop_strategy === 'smart' && args.resize ) {
args.resize = getDimArray( args.resize );
return smartcrop.crop(buffer, { width: args.resize[0], height: args.resize[1] })
.then(function(result) {
return metadata(result.topCrop);
});
}
return metadata();
} catch (err) {
reject(err);
callback(err);
}
});
};