-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize-gallery.js
More file actions
32 lines (30 loc) · 930 Bytes
/
optimize-gallery.js
File metadata and controls
32 lines (30 loc) · 930 Bytes
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
import fs from "fs";
import path from "path";
import sharp from "sharp";
async function resize_images(dir, max_height = 1080) {
const walk = async (directory) => {
const files = await fs.promises.readdir(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stats = await fs.promises.stat(filePath);
if (stats.isFile()) {
try {
const meta_data = await sharp(filePath).metadata();
if (meta_data.height > max_height) {
await sharp(filePath)
.resize(null, max_height)
.toBuffer(function (err, buffer) {
fs.writeFileSync(filePath, buffer);
});
}
} catch (e) {
// Do nothing
}
} else if (stats.isDirectory()) {
await walk(filePath);
}
}
};
await walk(dir, 1080);
}
resize_images("public/locations", 1080);