forked from imagemin/imagemin-jpeg-recompress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·78 lines (60 loc) · 1.4 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
'use strict';
const execBuffer = require('exec-buffer');
const isJpg = require('is-jpg');
const isPng = require('is-png');
const jpegRecompress = require('jpeg-recompress-bin');
module.exports = options => buf => {
options = {...options};
if (!Buffer.isBuffer(buf)) {
return Promise.reject(new TypeError('Expected a buffer'));
}
if (!isJpg(buf) && !isPng(buf)) {
return Promise.resolve(buf);
}
const args = ['--quiet'];
if (options.accurate) {
args.push('--accurate');
}
if (options.quality) {
args.push('--quality', options.quality);
}
if (options.method) {
args.push('--method', options.method);
}
if (options.target) {
args.push('--target', options.target);
}
if (options.min) {
args.push('--min', options.min);
}
if (options.max) {
args.push('--max', options.max);
}
if (options.loops) {
args.push('--loops', options.loops);
}
if (options.defish) {
args.push('--defish', options.defish);
}
if (options.zoom) {
args.push('--zoom', options.zoom);
}
if (options.progressive === false) {
args.push('--no-progressive');
}
if (options.subsample) {
args.push('--subsample', options.subsample);
}
if (options.strip !== false) {
args.push('--strip');
}
args.push(execBuffer.input, execBuffer.output);
return execBuffer({
input: buf,
bin: jpegRecompress,
args
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
});
};