Skip to content

Instantly share code, notes, and snippets.

@joduplessis
Created January 20, 2025 12:02
Show Gist options
  • Save joduplessis/e3e17b81fa5e4d8e2e8cf1beb4457f2b to your computer and use it in GitHub Desktop.
Save joduplessis/e3e17b81fa5e4d8e2e8cf1beb4457f2b to your computer and use it in GitHub Desktop.
Simple NodeJS based workflow for 1) resizing images & 2) cropping them.
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
// Input and Output Directories
const inputDir = 'images-resized';
const outputDir = 'images-cropped';
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// Function to process images recursively
function processImages(inputPath, outputPath) {
// Read the directory
fs.readdirSync(inputPath).forEach(file => {
const inputFilePath = path.join(inputPath, file);
const outputFilePath = path.join(outputPath, file);
if (fs.statSync(inputFilePath).isDirectory()) {
// If it's a directory, create the corresponding output directory and recurse
if (!fs.existsSync(outputFilePath)) {
fs.mkdirSync(outputFilePath, { recursive: true });
}
processImages(inputFilePath, outputFilePath);
} else if (file.toLowerCase().endsWith('.png')) {
// If it's a PNG image, process it
sharp(inputFilePath)
.trim() // Automatically crop transparent borders
.toFile(outputFilePath)
.then(() => console.log(`Cropped and saved: ${outputFilePath}`))
.catch(err => console.error(`Error processing ${inputFilePath}:`, err));
}
});
}
// Start the recursive process
processImages(inputDir, outputDir);
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
// Input and Output Directories
const inputDir = 'images';
const outputDir = 'images-resized';
// Resize dimensions
const width = 180
const height = 180
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// Function to process images recursively
function processImages(inputPath, outputPath) {
// Read the directory
fs.readdirSync(inputPath).forEach(file => {
const inputFilePath = path.join(inputPath, file);
const outputFilePath = path.join(outputPath, file);
if (fs.statSync(inputFilePath).isDirectory()) {
// If it's a directory, create the corresponding output directory and recurse
if (!fs.existsSync(outputFilePath)) {
fs.mkdirSync(outputFilePath, { recursive: true });
}
processImages(inputFilePath, outputFilePath);
} else if (file.toLowerCase().endsWith('.png')) {
// If it's a PNG image, resize it
sharp(inputFilePath)
.resize(width, height, {
fit: 'inside', // Maintain aspect ratio and fit within the dimensions
withoutEnlargement: true, // Avoid enlarging smaller images
})
.toFile(outputFilePath)
.then(() => console.log(`Resized and saved: ${outputFilePath}`))
.catch(err => console.error(`Error processing ${inputFilePath}:`, err));
}
});
}
// Start the recursive process
processImages(inputDir, outputDir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment