-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert.js
105 lines (95 loc) · 4.2 KB
/
convert.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
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
// Importing required modules
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
// Define input and output directories
const inputDirectory = './input';
const outputDirectory = './output';
// Configuration object with image size, scale, and format options
const config = {
sizes: [1024, 2048, 4096], // array of image widths to resize to
scales: [1, 0.5, 0.25], // array of scales to resize the image by
formats: ['png', 'jpg', 'webp'] // array of image formats to save the resized images as
};
// Read the directory to get a list of files
fs.readdir(inputDirectory, (err, files) => {
if (err) {
// Log the error and return
console.error(err);
return;
}
// Iterate through each file
files.forEach(file => {
// Build the input file path
const inputFile = path.join(inputDirectory, file);
// Get the base file name
const baseFileName = path.parse(file).name;
// Build the output folder path
const outputFolder = path.join(outputDirectory, baseFileName);
// Check if the output folder exists, if not create it
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder);
} else {
// If the output folder exists, remove it and recreate it
fs.rmdirSync(outputFolder, { recursive: true });
fs.mkdirSync(outputFolder);
}
// Use the sharp module to get metadata from the input file
sharp(inputFile)
.metadata()
.then(metadata => {
// Store the original width of the image
const originalWidth = metadata.width;
// Iterate through each format in the config
config.formats.forEach(format => {
// Iterate through each size in the config
config.sizes.forEach(size => {
// Build the output file path
const outputFile = path.join(
outputFolder,
`${baseFileName}-${size}.${format}`
);
// Use the sharp module to resize the image and save it to the output file path
sharp(inputFile)
.resize({ width: size })
.toFormat(format)
.toFile(outputFile, (error, info) => {
if (error) {
// Log the error
console.error(error);
} else {
// Log success message
console.log(
`Successfully converted ${inputFile} to ${outputFile} in ${format} format`
);
}
});
});
// Iterate through each scale in the config
config.scales.forEach(scale => {
// Build the output file path
const outputFile = path.join(
outputFolder,
`${baseFileName}-${scale}x.${format}`
);
// Use the sharp module to resize the image and save it to the output file path
sharp(inputFile)
.resize({ width: Math.round(originalWidth * scale) })
.toFormat(format)
.toFile(outputFile, (error, info) => {
if (error) {
console.error(error);
} else {
console.log(
`Successfully converted ${inputFile} to ${outputFile} in ${format} format`
);
}
});
});
});
})
.catch(error => {
console.error(error);
});
});
});