-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-to-webp.js
46 lines (40 loc) · 1.25 KB
/
convert-to-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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
// Define the input and output directories
const inputDir = './input';
const outputDir = './output';
// Create the output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// Function to convert an image to WebP format
function convertToWebP(inputFilePath, outputFilePath) {
sharp(inputFilePath)
.webp()
.toFile(outputFilePath, (err, info) => {
if (err) {
console.error(`Error converting ${inputFilePath} to WebP: ${err}`);
} else {
console.log(`Converted ${inputFilePath} to WebP at ${outputFilePath}`);
}
});
}
// Read the files in the input directory
fs.readdir(inputDir, (err, files) => {
if (err) {
console.error(`Error reading input directory: ${err}`);
return;
}
// Filter only JPG and PNG files
const imageFiles = files.filter((file) => /\.(jpg|jpeg|png)$/i.test(file));
// Convert each image to WebP format
imageFiles.forEach((file) => {
const inputFilePath = path.join(inputDir, file);
const outputFilePath = path.join(
outputDir,
path.basename(file, path.extname(file)) + '.webp'
);
convertToWebP(inputFilePath, outputFilePath);
});
});