-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize_images.js
More file actions
36 lines (29 loc) · 969 Bytes
/
Copy pathoptimize_images.js
File metadata and controls
36 lines (29 loc) · 969 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
35
36
import fs from "fs";
import path from "path";
import sharp from "sharp";
const dirs = [
path.join(process.cwd(), "public/applications"),
path.join(process.cwd(), "public/Entertaiment"),
];
async function optimizeImages() {
for (const dir of dirs) {
if (!fs.existsSync(dir)) continue;
const files = fs.readdirSync(dir);
for (const file of files) {
if (file.endsWith(".webp")) continue; // Skip existing webp files
const ext = path.extname(file);
if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") continue;
const inputPath = path.join(dir, file);
const outputPath = path.join(dir, file.replace(ext, ".webp"));
try {
await sharp(inputPath)
.webp({ quality: 80 })
.toFile(outputPath);
console.log(`Optimized: ${file} -> ${path.basename(outputPath)}`);
} catch (err) {
console.error(`Error processing ${file}:`, err);
}
}
}
}
optimizeImages();