const sharp = require('sharp');
const fs = require('fs');
// Input image path
const inputImagePath = 'input.jpg';
// Output directory for resized images
const outputDirectory = 'resized_images';
// Array of sizes for the resized images
const sizes = [300, 600, 1200];
// Ensure the output directory exists
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory);
}
// Read the input image
sharp(inputImagePath).toBuffer((err, buffer, info) => {
if (err) throw err;
// Iterate through each size and create a resized image
sizes.forEach(size => {
sharp(buffer)
.resize(size, size, { fit: 'inside', withoutEnlargement: true })
.toFile(`${outputDirectory}/image_${size}.jpg`, (err, info) => {
if (err) throw err;
console.log(`Image resized to ${size}x${size}: ${info.width}x${info.height}`);
});
});
});