-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageProcessor.js
More file actions
69 lines (57 loc) · 1.91 KB
/
imageProcessor.js
File metadata and controls
69 lines (57 loc) · 1.91 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
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
const sharp = require('sharp');
const processImage = async (inputBuffer, format, width, height, compress) => {
console.log('Processing image...');
let image = sharp(inputBuffer);
if (format) {
image = image.toFormat(format);
}
if (width || height) {
image = image.resize(width, height);
}
const { data, info } = await image.toBuffer({ resolveWithObject: true });
return {
buffer: data.toString('base64'),
width: info.width,
height: info.height,
size: info.size
};
};
const compressImage = async (inputBuffer, quality) => {
console.log('Compressing image...');
let image = sharp(inputBuffer);
// 압축 품질 설정
image = image.jpeg({ quality: quality }).png({ quality: quality }).webp({ quality: quality });
const { data, info } = await image.toBuffer({ resolveWithObject: true });
return {
buffer: data.toString('base64'),
width: info.width,
height: info.height,
size: info.size
};
};
const args = process.argv.slice(2);
const command = args[0];
const inputBase64 = args[1];
const format = args[2];
const width = args[3] ? parseInt(args[3]) : null;
const height = args[4] ? parseInt(args[4]) : null;
const compress = args[5] === 'true';
const quality = args[6] ? parseInt(args[6]) : 80;
const inputBuffer = Buffer.from(inputBase64, 'base64');
if (command === 'process') {
processImage(inputBuffer, format, width, height, compress)
.then(result => {
console.log(JSON.stringify(result));
})
.catch(error => {
console.error(JSON.stringify({ error: error.message }));
});
} else if (command === 'compress') {
compressImage(inputBuffer, quality)
.then(result => {
console.log(JSON.stringify(result));
})
.catch(error => {
console.error(JSON.stringify({ error: error.message }));
});
}