-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (68 loc) · 1.74 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
import {Buffer} from 'node:buffer';
import {execa} from 'execa';
import gifsicle from 'gifsicle';
import isGif from 'is-gif';
const main = (opts = {}) => async buf => {
if (!Buffer.isBuffer(buf)) {
return Promise.reject(new TypeError('Expected a buffer'));
}
if (!isGif(buf)) {
return Promise.resolve(buf);
}
const args = ['--no-warnings', '--no-app-extensions'];
if (opts.interlaced) {
args.push('--interlace');
}
if (opts.optimizationLevel) {
args.push(`--optimize=${opts.optimizationLevel}`);
}
if (opts.colors) {
args.push(`--colors=${opts.colors}`);
}
if (opts.lossy) {
args.push(`--lossy=${opts.lossy}`);
}
if (opts.resize_method) {
args.push(`--resize-method=${opts.resize_method}`);
}
if (opts.gamma) {
args.push(`--gamma=${opts.gamma}`);
}
if (opts.crop) {
args.push(`--crop=${opts.crop[0]},${opts.crop[1]}+${opts.crop[2]}x${opts.crop[3]}`);
}
if (opts.flip_h) {
args.push(`--flip-horizontal`);
}
if (opts.flip_v) {
args.push(`--flip-vertical`);
}
if (opts.rotate) {
if(opts.rotate == 90) args.push(`--rotate-90`);
if(opts.rotate == 180) args.push(`--rotate-180`);
if(opts.rotate == 270) args.push(`--rotate-270`);
}
if(opts.width){
if(!opts.stretch){
args.push(`--resize-fit-width=${opts.width}`);
} else {
args.push(`--resize-width=${opts.width}`);
}
}
if(opts.height){
if(!opts.stretch){
args.push(`--resize-fit-height=${opts.height}`);
} else {
args.push(`--resize-height=${opts.height}`);
}
}
args.push('--output', "-");
try {
const gif_output = await execa(gifsicle, args, {input: buf, encoding: null, timeout: opts.timeout});
return gif_output.stdout;
} catch (error) {
error.message = error.stderr || error.message;
throw error;
}
};
export default main;