Fix: Add base64 placeholder for hero video and squash all massive photo assets down to max 1920px
This commit is contained in:
41
scripts/optimize-images.ts
Normal file
41
scripts/optimize-images.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import sharp from 'sharp';
|
||||
|
||||
const PHOTOS_DIR = path.join(process.cwd(), 'public', 'assets', 'photos');
|
||||
|
||||
async function optimizeImages() {
|
||||
const files = fs.readdirSync(PHOTOS_DIR);
|
||||
let optimized = 0;
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.match(/\.(jpg|jpeg|png)$/i)) continue;
|
||||
|
||||
const filePath = path.join(PHOTOS_DIR, file);
|
||||
const metadata = await sharp(filePath).metadata();
|
||||
|
||||
// If image is larger than 1920px wide, or is a massive file
|
||||
if ((metadata.width && metadata.width > 1920) || fs.statSync(filePath).size > 1024 * 1024) {
|
||||
console.log(`Optimizing ${file} (Width: ${metadata.width}, Size: ${Math.round(fs.statSync(filePath).size / 1024)}KB)`);
|
||||
|
||||
const tmpPath = `${filePath}.tmp`;
|
||||
try {
|
||||
await sharp(filePath)
|
||||
.resize({ width: 1920, withoutEnlargement: true })
|
||||
.jpeg({ quality: 80, progressive: true })
|
||||
.toFile(tmpPath);
|
||||
|
||||
fs.renameSync(tmpPath, filePath);
|
||||
console.log(`✅ Optimized ${file} (New Size: ${Math.round(fs.statSync(filePath).size / 1024)}KB)`);
|
||||
optimized++;
|
||||
} catch (err) {
|
||||
console.error(`Failed to optimize ${file}`, err);
|
||||
if (fs.existsSync(tmpPath)) fs.unlinkSync(tmpPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Finished optimizing ${optimized} images.`);
|
||||
}
|
||||
|
||||
optimizeImages();
|
||||
Reference in New Issue
Block a user