24 lines
747 B
JavaScript
24 lines
747 B
JavaScript
import sharp from "sharp";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
// Input and output file paths
|
|
const inputPath = "./imageFolder";
|
|
const outputPath = "./compressedImage";
|
|
|
|
export async function compressImages() {
|
|
const images = fs.readdirSync(inputPath);
|
|
|
|
await Promise.all(
|
|
images.map((image) => {
|
|
const inputImagePath = path.join(inputPath, image);
|
|
const outputImagePath = path.join(outputPath, image);
|
|
// Compress the image (adjust quality for JPEG)
|
|
return sharp(inputImagePath)
|
|
.resize({ width: 700 }) // optional resize
|
|
.jpeg({ quality: 70 }) // lower quality = higher compression
|
|
//.rotate(90) // rotate by 90 degrees clockwise
|
|
.toFile(outputImagePath);
|
|
}),
|
|
);
|
|
}
|