-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
42 lines (33 loc) · 1.13 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
'use strict';
const fileType = require('file-type');
const Jimp = require('jimp');
const supportedFormats = {
bmp: Jimp.MIME_BMP,
jpg: Jimp.MIME_JPEG,
png: Jimp.MIME_PNG
};
module.exports = async (buffer, options = {}) => {
if (!Buffer.isBuffer(buffer)) {
throw new TypeError(
`Expected \`buffer\` to be of type \`Buffer\` but received type \`${typeof buffer}\``
);
}
const type = fileType(buffer);
if (!type || !Object.keys(supportedFormats).includes(type.ext)) {
throw new Error('Image format not supported');
}
if (!Number.isFinite(options.width) && !Number.isFinite(options.height)) {
throw new TypeError('You need to set either `width` or `height` options');
}
const image = await Jimp.read(buffer);
const mime = supportedFormats[options.format] || Jimp.AUTO;
if (typeof options.width !== 'number') {
options.width = Math.trunc(image.bitmap.width * (options.height / image.bitmap.height));
}
if (typeof options.height !== 'number') {
options.height = Math.trunc(image.bitmap.height * (options.width / image.bitmap.width));
}
return image
.resize(options.width, options.height)
.getBufferAsync(mime);
};