-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebp.js
27 lines (23 loc) · 876 Bytes
/
webp.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
const fs = require('fs');
const sharp = require('sharp');
const path = require('path');
const inputDir = './images/products_opt'; // Directory with JPG images
fs.readdir(inputDir, (err, files) => {
if (err) {
console.error('Error reading input directory:', err);
return;
}
files.filter(file => path.extname(file).toLowerCase() === '.jpg').forEach(file => {
const inputFile = path.join(inputDir, file);
const outputFile = path.join(inputDir, `${path.parse(file).name}.webp`);
sharp(inputFile)
.toFormat('webp')
.toFile(outputFile)
.then(() => {
console.log(`${file} has been compressed and saved as ${path.parse(file).name}.webp`);
})
.catch(err => {
console.error('Error processing file', file, err);
});
});
});