50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
const sharp = require('sharp');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
const imagesToConvert = [
|
|
'2-body.png',
|
|
'2-hero.png',
|
|
'qr-code-analytics-hero.png',
|
|
'1-hero.png'
|
|
];
|
|
|
|
const blogDir = path.join(__dirname, '../public/blog');
|
|
|
|
async function compressImages() {
|
|
console.log('🖼️ Starting image compression...\n');
|
|
|
|
for (const imageName of imagesToConvert) {
|
|
const inputPath = path.join(blogDir, imageName);
|
|
const outputName = imageName.replace('.png', '.webp');
|
|
const outputPath = path.join(blogDir, outputName);
|
|
|
|
if (!fs.existsSync(inputPath)) {
|
|
console.log(`⚠️ Skipping ${imageName} - file not found`);
|
|
continue;
|
|
}
|
|
|
|
const originalSize = fs.statSync(inputPath).size;
|
|
|
|
try {
|
|
await sharp(inputPath)
|
|
.webp({ quality: 85 })
|
|
.toFile(outputPath);
|
|
|
|
const newSize = fs.statSync(outputPath).size;
|
|
const savings = ((1 - newSize / originalSize) * 100).toFixed(1);
|
|
|
|
console.log(`✅ ${imageName}`);
|
|
console.log(` Original: ${(originalSize / 1024 / 1024).toFixed(2)} MB`);
|
|
console.log(` WebP: ${(newSize / 1024 / 1024).toFixed(2)} MB`);
|
|
console.log(` Savings: ${savings}%\n`);
|
|
} catch (err) {
|
|
console.error(`❌ Failed to convert ${imageName}:`, err.message);
|
|
}
|
|
}
|
|
|
|
console.log('Done! Remember to update image references in blog-data.ts');
|
|
}
|
|
|
|
compressImages();
|