-
Notifications
You must be signed in to change notification settings - Fork 7
/
converter.js
46 lines (42 loc) · 1.26 KB
/
converter.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
const fs = require('fs');
const sharp = require('sharp');
const LogUtil = require('./log');
sharp.concurrency(1);
sharp.cache(false);
function createSharp(ext) {
const sharpInstance = sharp();
switch (ext) {
case '.heic':
return sharpInstance.heif({
compression: 'hevc'
});
case '.avif':
return sharpInstance.avif();
case '.webp':
return sharpInstance.webp();
default:
return sharpInstance;
}
}
module.exports = (targetExts, inputImagePath) => {
const input = fs.createReadStream(inputImagePath);
targetExts.forEach(ext => {
const outputImagePath = inputImagePath + ext;
const output = fs.createWriteStream(outputImagePath); // 输出流
input
.pipe(createSharp(ext))
.pipe(output)
.on('finish', () => {
LogUtil.info(`Converted ${inputImagePath} to ${outputImagePath}.`);
})
.on('error', (err) => {
LogUtil.error(`Failed to converting ${inputImagePath} to ${outputImagePath}!`);
})
.on('close', () => {
if (global.gc) {
global.gc();
}
});
;
});
}