Skip to content

Commit 8d92f46

Browse files
committed
initial
0 parents  commit 8d92f46

File tree

8 files changed

+743
-0
lines changed

8 files changed

+743
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Node modules
2+
node_modules
3+
4+
# generated files
5+
favicon
6+
7+
# secret data
8+
.env

LICENSE.MD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## ISC License
2+
3+
Copyright (c) 2024 Lene Saile
4+
5+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Favicon Generator
2+
3+
A very simple utility to generate various sizes and formats of favicons from a single SVG source file. I use this to generate favicons for my projects. It follows the recommendations from the article on [evilmartians.com](https://evilmartians.com/chronicles/how-to-favicon-in-2021-six-files-that-fit-most-needs).
4+
5+
## Installation
6+
7+
```bash
8+
npm install
9+
```
10+
11+
## Usage
12+
13+
Replace favicon.svg with your own SVG file.
14+
15+
You can also configure your settings in `settings.js`:
16+
17+
```javascript
18+
// settings.js
19+
export const pathToSvgLogo = "./favicon.svg";
20+
export const outputDir = "./favicons";
21+
```
22+
23+
Then run the script:
24+
25+
```bash
26+
node index.js
27+
# or:
28+
npm run build
29+
```
30+
31+
The script will generate the following favicon files in the specified output directory:
32+
33+
- SVG favicon
34+
- PNG icons (192x192, 512x512)
35+
- Apple touch icon (180x180)
36+
- Maskable icon (512x512 with padding)
37+
- ICO file (32x32)

favicon.svg

Lines changed: 6 additions & 0 deletions
Loading

index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import fs from "node:fs";
2+
import sharp from "sharp";
3+
import { sharpsToIco } from "sharp-ico";
4+
import { pathToSvgLogo, outputDir } from "./settings.js";
5+
6+
async function createFavicons() {
7+
// Ensure the output directory exists
8+
fs.mkdirSync(outputDir, { recursive: true });
9+
10+
// Read the SVG logo
11+
const svgBuffer = fs.readFileSync(pathToSvgLogo);
12+
13+
// Write the SVG favicon
14+
fs.writeFileSync(`${outputDir}/favicon.svg`, svgBuffer);
15+
16+
// Create PNG favicons
17+
await sharp(svgBuffer).resize(192, 192).toFile(`${outputDir}/icon-192x192.png`);
18+
await sharp(svgBuffer).resize(512, 512).toFile(`${outputDir}/icon-512x512.png`);
19+
await sharp(svgBuffer).resize(180, 180).toFile(`${outputDir}/apple-touch-icon.png`);
20+
21+
// Create a maskable icon
22+
await sharp(svgBuffer)
23+
.resize(512, 512)
24+
.extend({
25+
top: 50,
26+
bottom: 50,
27+
left: 50,
28+
right: 50,
29+
background: { r: 0, g: 0, b: 0, alpha: 0 }, // Transparent padding
30+
})
31+
.toFile(`${outputDir}/maskable-icon.png`);
32+
33+
// Create an ICO file
34+
const iconSharp = sharp(svgBuffer).resize(32, 32);
35+
await sharpsToIco([iconSharp], `${outputDir}/favicon.ico`, { sizes: [32] });
36+
37+
console.log("All favicons generated.");
38+
}
39+
40+
createFavicons();

0 commit comments

Comments
 (0)