-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.92 KB
/
index.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
// Script to format the images in gallery format
const fs = require("fs");
const path = require("path");
const sharp = require("sharp");
const folderPath = "./images/thumbnails"; // Replace with your folder path
const outputFilePath = "./images.json";
fs.readdir(folderPath, (err, files) => {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
const imageFiles = files.filter((file) => {
const ext = path.extname(file).toLowerCase();
return ext === ".jpg" || ext === ".jpeg" || ext === ".png"; // Add other image extensions if needed
});
const imageMetadata = [];
imageFiles.forEach((file, index) => {
const newFileName = `IMG-${1000 + index}.jpg`;
const oldFilePath = path.join(folderPath, file);
const newFilePath = path.join(folderPath, newFileName);
sharp(oldFilePath)
.metadata()
.then((metadata) => {
const { width, height } = metadata;
// Rename the file
fs.rename(oldFilePath, newFilePath, (err) => {
if (err) {
console.error(`Could not rename file ${file} to ${newFileName}.`, err);
return;
}
// Store the image metadata
imageMetadata.push({
filenumber: index + 1,
name: newFileName,
width: width,
height: height,
});
// If we have processed all files, write to images.json
if (imageMetadata.length === imageFiles.length) {
fs.writeFile(outputFilePath, JSON.stringify(imageMetadata, null, 2), (err) => {
if (err) {
console.error("Could not write JSON file.", err);
return;
}
console.log("Renaming completed and images.json file created.");
});
}
});
})
.catch((err) => {
console.error(`Could not read metadata of file ${file}.`, err);
});
});
});