-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize.js
More file actions
39 lines (31 loc) · 1.04 KB
/
optimize.js
File metadata and controls
39 lines (31 loc) · 1.04 KB
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
const { spawn } = require('child_process');
async function compressGIF(gifBuffer, optimization, loss) {
return new Promise((resolve, reject) => {
const gifsicle = spawn('gifsicle', [
`--optimize=${optimization}`,
'--colors=128',
`--lossy=${loss}`
]);
let output = Buffer.alloc(0);
gifsicle.stdin.write(gifBuffer);
gifsicle.stdin.end();
gifsicle.stdout.on('data', (chunk) => {
output = Buffer.concat([output, chunk]);
});
gifsicle.stderr.on('data', (data) => {
console.error('Gifsicle stderr:', data.toString());
});
gifsicle.on('close', (code) => {
if (code === 0) {
resolve(output);
} else {
reject(new Error(`Gifsicle process failed with code ${code}.`));
}
});
gifsicle.on('error', (err) => {
console.error('Gifsicle error:', err);
reject(err);
});
});
}
module.exports = { compressGIF };