-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-images.js
49 lines (41 loc) · 1.36 KB
/
generate-images.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
import sharp from 'sharp';
import fs from 'fs';
import path from 'path';
// Input and output directories
const inputDir = 'cover';
const outputDir = 'cover/srcset';
// Read the input directory
fs.readdir(inputDir, (err, files) => {
if (err) {
console.error('Error reading input directory:', err);
return;
}
const filesFiltered = files.filter((f) => {
return !new RegExp(".*_\d*\.*$").test(f)
})
// Process each image
filesFiltered.forEach((file) => {
const inputImagePath = path.join(inputDir, file);
const outputImageDir = path.join(outputDir);
const outputBaseName = path.basename(file, path.extname(file));
// Create output directory if not exists
if (!fs.existsSync(outputImageDir)) {
fs.mkdirSync(outputImageDir, { recursive: true });
}
// Define widths for srcset
const widths = [120, 200, 320, 640, 800];
// Generate and save images with different widths
widths.forEach((width) => {
const outputFile = path.join(outputImageDir, `${outputBaseName}_${width}.jpg`);
sharp(inputImagePath)
.resize({ width })
.toFile(outputFile, (err) => {
if (err) {
console.error(`Error processing image ${inputImagePath} at width ${width}:`, err);
} else {
console.log(`Image processed successfully: ${outputFile}`);
}
});
});
});
});