This repository has been archived by the owner on May 8, 2024. It is now read-only.
forked from imagemin/imagemin-mozjpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (73 loc) · 1.67 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
90
91
92
93
94
95
'use strict';
const execa = require('execa');
const isJpg = require('is-jpg');
const mozjpeg = require('@mole-inc/mozjpeg');
module.exports = options => async buffer => {
options = {trellis: true,
trellisDC: true,
overshoot: true, ...options};
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('Expected a buffer');
}
if (!isJpg(buffer)) {
return buffer;
}
const args = [];
if (typeof options.quality !== 'undefined') {
args.push('-quality', options.quality);
}
if (options.progressive === false) {
args.push('-baseline');
}
if (options.targa) {
args.push('-targa');
}
if (options.revert) {
args.push('-revert');
}
if (options.fastCrush) {
args.push('-fastcrush');
}
if (typeof options.dcScanOpt !== 'undefined') {
args.push('-dc-scan-opt', options.dcScanOpt);
}
if (!options.trellis) {
args.push('-notrellis');
}
if (!options.trellisDC) {
args.push('-notrellis-dc');
}
if (options.tune) {
args.push(`-tune-${options.tune}`);
}
if (!options.overshoot) {
args.push('-noovershoot');
}
if (options.arithmetic) {
args.push('-arithmetic');
}
if (options.dct) {
args.push('-dct', options.dct);
}
if (options.quantBaseline) {
args.push('-quant-baseline', options.quantBaseline);
}
if (typeof options.quantTable !== 'undefined') {
args.push('-quant-table', options.quantTable);
}
if (options.smooth) {
args.push('-smooth', options.smooth);
}
if (options.maxMemory) {
args.push('-maxmemory', options.maxMemory);
}
if (options.sample) {
args.push('-sample', options.sample.join(','));
}
const {stdout} = await execa(mozjpeg, args, {
encoding: null,
input: buffer,
maxBuffer: Infinity
});
return stdout;
};