-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageopt
executable file
·140 lines (122 loc) · 3.15 KB
/
imageopt
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! node
const inquirer = require("inquirer");
const fs = require("fs");
const path = require("path");
const sharp = require("sharp");
const INPUT_IMAGE_FORMATS = [
"jpeg",
"jpg",
"png",
"webp",
"avif",
"tiff",
"gif",
"svg",
];
const OUTPUT_IMAGE_FORMATS = ["jpeg", "png", "webp", "avif", "tiff"];
const COLOR_MAP_OUTPUT = [
"b-w",
"cmyk",
"hsv",
"lab",
"rgb",
"rgb16",
"scrgb",
"srgb",
];
async function getMetadata(file) {
let metadata = await sharp(file).metadata();
fs.stat(file, (err, { size }) => {
if (err) {
return console.error(err);
}
const sizeToMb = `${(size / 1048576).toFixed(4)}Mb`;
metadata = {
...metadata,
size: sizeToMb,
};
console.log(file);
console.table(metadata);
});
}
const compress = (image, format, compress) => {
const process = {
jpeg: () => image.jpeg({ quality: compress * 10 }),
webp: () => image.webp({ quality: compress * 10 }),
png: () => image.png({ quality: compress * 10 }),
avif: () => image.avif({ quality: compress * 10 }),
tiff: () => image.tiff({ quality: compress * 10 }),
};
process[format]();
return image;
};
fs.readdir(process.cwd(), (err, files) => {
if (err) {
return console.error(`Unable to scan directory ${err}`);
}
files = files.filter((file) =>
INPUT_IMAGE_FORMATS.includes(path.extname(file).toLowerCase().substr(1))
);
if (files.length < 1) {
return console.error(`There's no images here!`);
}
inquirer
.prompt([
{
type: "checkbox",
name: "images",
message: "Select images to be optimized: ",
choices: [...files],
},
{
type: "input",
name: "sizeX",
message: "New width: ",
},
{
type: "input",
name: "sizeY",
message: "New height: ",
},
{
type: "list",
name: "compression",
message: "Compression level(1->lowest quality,9->highest quality): ",
choices: [1, 2, 3, 4, 5, 6, 7, 8, 9],
},
{
type: "list",
name: "outputFormat",
message: "Select an output format: ",
choices: [...OUTPUT_IMAGE_FORMATS],
},
{
type: "list",
name: "colorFormat",
message: "Select a color format: ",
choices: [...COLOR_MAP_OUTPUT],
},
])
.then(
({ images, sizeX, sizeY, outputFormat, colorFormat, compression }) => {
for (const image of images) {
getMetadata(image).catch((err) => console.error(err));
const newImage = sharp(image)
.resize(+sizeX, +sizeY)
.toFormat(outputFormat)
.toColorspace(colorFormat);
const newImageName = `${
image.substr(0, image.lastIndexOf(".")) || image
}-${sizeX}x${sizeY}-${colorFormat}.${outputFormat}`;
compress(newImage, outputFormat, compression)
.toFile(newImageName, (err) => {
if (err) {
return console.error(err);
}
getMetadata(newImageName).catch((err) => console.error(err));
})
}
}
)
.catch((err) => console.error(err));
});