-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcompress-project-images.js
More file actions
34 lines (24 loc) · 876 Bytes
/
Copy pathcompress-project-images.js
File metadata and controls
34 lines (24 loc) · 876 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
33
34
import fs from "fs";
import path from "path";
import sharp from "sharp";
const folder = path.join(__dirname, "../public/projects");
async function compress() {
const files = fs.readdirSync(folder);
for (const file of files) {
const filePath = path.join(folder, file);
const ext = path.extname(file).toLowerCase();
if (![".jpg", ".jpeg", ".png"].includes(ext)) continue;
const original = fs.statSync(filePath).size;
await sharp(filePath)
.resize({ width: 1400 }) // reduce huge images
.jpeg({ quality: 80 }) // safe compression
.toBuffer()
.then((data) => fs.writeFileSync(filePath, data));
const compressed = fs.statSync(filePath).size;
console.log(
`${file}: ${(original / 1024).toFixed(2)}KB → ${(compressed / 1024).toFixed(2)}KB`,
);
}
console.log("✅ Compression complete");
}
compress();