-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (67 loc) · 1.7 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
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
'use strict';
const execBuffer = require('exec-buffer');
const isCwebpReadable = require('is-cwebp-readable');
const cwebp = require('./cwebp-bin');
module.exports = (options = {}) => input => {
if (!Buffer.isBuffer(input)) {
return Promise.reject(new TypeError(`Expected \`input\` to be of type \`Buffer\` but received type \`${typeof input}\``));
}
if (!isCwebpReadable(input)) {
return Promise.resolve(input);
}
const args = [
'-quiet',
'-mt'
];
if (options.preset) {
args.push('-preset', options.preset);
}
if (options.quality) {
args.push('-q', options.quality);
}
if (options.alphaQuality) {
args.push('-alpha_q', options.alphaQuality);
}
if (options.method) {
args.push('-m', options.method);
}
if (options.size) {
args.push('-size', options.size);
}
if (options.sns) {
args.push('-sns', options.sns);
}
if (options.filter) {
args.push('-f', options.filter);
}
if (options.autoFilter) {
args.push('-af');
}
if (options.sharpness) {
args.push('-sharpness', options.sharpness);
}
if (options.lossless) {
args.push('-lossless');
}
if (options.nearLossless) {
args.push('-near_lossless', options.nearLossless);
}
if (options.crop) {
args.push('-crop', options.crop.x, options.crop.y, options.crop.width, options.crop.height);
}
if (options.resize) {
args.push('-resize', options.resize.width, options.resize.height);
}
if (options.metadata) {
args.push('-metadata', Array.isArray(options.metadata) ? options.metadata.join(',') : options.metadata);
}
args.push('-o', execBuffer.output, execBuffer.input);
return execBuffer({
args,
bin: cwebp,
input
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
});
};