forked from imagemin/imagemin-optipng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (52 loc) · 1.09 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
import {Buffer} from 'node:buffer';
import execBuffer from 'exec-buffer';
import isPng from 'is-png';
import optipng from 'optipng-bin';
const main = options => async buffer => {
options = {
optimizationLevel: 3,
bitDepthReduction: true,
colorTypeReduction: true,
paletteReduction: true,
interlaced: false,
errorRecovery: true,
...options,
};
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('Expected a buffer');
}
if (!isPng(buffer)) {
return buffer;
}
const arguments_ = [
'-strip',
'all',
'-clobber',
'-o',
options.optimizationLevel,
'-out',
execBuffer.output,
];
if (options.errorRecovery) {
arguments_.push('-fix');
}
if (!options.bitDepthReduction) {
arguments_.push('-nb');
}
if (typeof options.interlaced === 'boolean') {
arguments_.push('-i', options.interlaced ? '1' : '0');
}
if (!options.colorTypeReduction) {
arguments_.push('-nc');
}
if (!options.paletteReduction) {
arguments_.push('-np');
}
arguments_.push(execBuffer.input);
return execBuffer({
input: buffer,
bin: optipng,
args: arguments_,
});
};
export default main;