-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (49 loc) · 1.47 KB
/
index.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
var smartcrop = require('smartcrop');
var lwip = require('lwip');
function convertBuffer(width, height, buffer) {
var bufferLength = buffer.length;
var numPixels = bufferLength / 4;
var output = new Buffer(bufferLength);
for (var i = 0; i < numPixels; i++) {
output[i * 4] = buffer[i];
output[(i * 4) + 1] = buffer[i + numPixels];
output[(i * 4) + 2] = buffer[i + (2 * numPixels)];
output[(i * 4) + 3] = 255;
}
return output;
}
var iop = {
open: function(src, options) {
return new Promise(function(resolve, reject) {
lwip.open(src, 'jpg', function (err, image) {
if (err) return reject(err);
resolve({
width: image.width(),
height: image.height(),
_lwip: image
});
});
});
},
resample: function(image, width, height) {
return Promise.resolve({
width: ~~width,
height: ~~height,
_lwip: image._lwip,
});
},
getData: function(image) {
return new Promise(function(resolve, reject) {
image._lwip.resize(image.width, image.height, function (err, resizedImage) {
if (err) return reject(err);
var buffer = convertBuffer(image.width, image.height, resizedImage.__lwip.buffer());
resolve(new smartcrop.ImgData(image.width, image.height, buffer));
});
});
},
};
exports.crop = function(img, options, callback) {
options = options || {};
options.imageOperations = iop;
return smartcrop.crop(img, options, callback);
};