This repository has been archived by the owner on May 8, 2024. It is now read-only.
forked from imagemin/imagemin-webp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (69 loc) · 1.71 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
87
88
89
'use strict';
const {cwebpBin} = require('@mole-inc/cwebp-bin');
const execBuffer = require('exec-buffer');
const isCwebpReadable = require('is-cwebp-readable');
module.exports = (options = {}) => async input => {
if (!Buffer.isBuffer(input)) {
throw new TypeError(`Expected \`input\` to be of type \`Buffer\` but received type \`${typeof input}\``);
}
if (!isCwebpReadable(input)) {
return 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);
try {
return execBuffer({
args,
bin: cwebpBin.path,
input,
});
} catch (error) {
error.message = error.stderr || error.message;
throw error;
}
};