mirror of
https://github.com/kogakure/website-astro-stefanimhoff.de.git
synced 2026-02-03 12:05:28 +00:00
feat: add script to convert JPG to WebP
This commit is contained in:
31
convert-images.cjs
Normal file
31
convert-images.cjs
Normal file
@@ -0,0 +1,31 @@
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const publicDir = path.join(__dirname, 'public');
|
||||
|
||||
async function convertJpgToWebp(directory) {
|
||||
try {
|
||||
const entries = await fs.readdir(directory, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(directory, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
await convertJpgToWebp(fullPath);
|
||||
} else if (entry.isFile() && path.extname(entry.name).toLowerCase() === '.jpg') {
|
||||
const webpName = path.join(directory, `${path.parse(entry.name).name}.webp`);
|
||||
|
||||
await sharp(fullPath).webp().toFile(webpName);
|
||||
|
||||
console.log(`Converted: ${fullPath} -> ${webpName}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error processing directory ${directory}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
convertJpgToWebp(publicDir)
|
||||
.then(() => console.log('Conversion complete'))
|
||||
.catch((error) => console.error('Conversion failed:', error));
|
||||
@@ -9,9 +9,9 @@
|
||||
"preview": "astro preview",
|
||||
"og:generate": "node og-generate.cjs",
|
||||
"icons:generate": "node icons-generate.cjs",
|
||||
"webp": "node convert-images.cjs",
|
||||
"plop": "plop",
|
||||
"astro": "astro",
|
||||
"cspell": "cspell --words-only --unique 'src' | sort --ignore-case >> .cspell/dictionary.txt"
|
||||
"astro": "astro"
|
||||
},
|
||||
"dependencies": {
|
||||
"@astro-community/astro-embed-youtube": "^0.5.2",
|
||||
|
||||
Reference in New Issue
Block a user