-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageProcessor2.js
More file actions
73 lines (60 loc) · 2.02 KB
/
imageProcessor2.js
File metadata and controls
73 lines (60 loc) · 2.02 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
70
71
72
73
const sharp = require('sharp');
const processImage = async (inputBuffer, format, width, height, compress) => {
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,
width: info.width,
height: info.height,
size: info.size
};
};
const compressImage = async (inputBuffer, quality) => {
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,
width: info.width,
height: info.height,
size: info.size
};
};
const args = process.argv.slice(2);
const command = args[0];
const format = args[1];
const width = args[2] ? parseInt(args[2]) : null;
const height = args[3] ? parseInt(args[3]) : null;
const compress = args[4] === 'true';
const quality = args[5] ? parseInt(args[5]) : 80;
let inputBuffer = Buffer.alloc(0);
// 스트림을 통해 이미지를 수신
process.stdin.on('data', chunk => {
inputBuffer = Buffer.concat([inputBuffer, chunk]);
});
process.stdin.on('end', () => {
if (command === 'process') {
processImage(inputBuffer, format, width, height, compress)
.then(result => {
process.stdout.write(JSON.stringify(result));
})
.catch(error => {
process.stderr.write(JSON.stringify({ error: error.message }));
});
} else if (command === 'compress') {
compressImage(inputBuffer, quality)
.then(result => {
process.stdout.write(JSON.stringify(result));
})
.catch(error => {
process.stderr.write(JSON.stringify({ error: error.message }));
});
}
});