Created
December 20, 2024 02:16
-
-
Save JEuler/81bdc373a01a883850629597c4bc5e7b to your computer and use it in GitHub Desktop.
Generate Favicons Node JS script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sharp from 'sharp'; | |
import path from 'path'; | |
import { fileURLToPath } from 'url'; | |
const __filename = fileURLToPath(import.meta.url); | |
const __dirname = path.dirname(__filename); | |
const sizes = [16, 32, 48, 64, 128, 256]; | |
const inputFile = path.join(__dirname, '../public/logo.png'); | |
const outputDir = path.join(__dirname, '../public'); | |
async function generateFavicons() { | |
try { | |
// Use sharp to create PNG favicons | |
await Promise.all( | |
sizes.map(async size => { | |
const outputFile = path.join(outputDir, `favicon-${size}x${size}.png`); | |
await sharp(inputFile) | |
.resize(size, size, { | |
fit: 'contain', | |
background: { r: 255, g: 255, b: 255, alpha: 0 } | |
}) | |
.toFormat('png') | |
.toFile(outputFile); | |
console.log(`Generated ${outputFile}`); | |
}) | |
); | |
// Create apple-touch-icon | |
await sharp(inputFile) | |
.resize(180, 180, { | |
fit: 'contain', | |
background: { r: 255, g: 255, b: 255, alpha: 0 } | |
}) | |
.toFormat('png') | |
.toFile(path.join(outputDir, 'apple-touch-icon.png')); | |
console.log('Favicon generation complete!'); | |
} catch (error) { | |
console.error('Error generating favicons:', error); | |
} | |
} | |
generateFavicons(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment