Skip to content

Instantly share code, notes, and snippets.

@maximal
Last active March 26, 2024 18:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximal/40032fbed2fd74f83e3b7944116027a5 to your computer and use it in GitHub Desktop.
Save maximal/40032fbed2fd74f83e3b7944116027a5 to your computer and use it in GitHub Desktop.
Generate favicons from SVG image
#!/usr/bin/bash
##
# Generate favicons from SVG image.
#
# This script generates PNG images and optimizes them.
# All assets will be deployed under `favicon` directory alongside with the initial SVG file.
#
# The initial SVG image must be square.
# PNG sizes: 32, 64, 128 and 256 px.
#
# Dependencies: Inkscape vector editor (`inkscape` command), OptiPNG optimizer (`optipng` command).
#
# @author MaximAL
# @since 2018-11-30
# @date 2018-11-30
# @time 16:47
#
# @copyright © MaximAL, Sijeko 2018
# @link https://t.me/sijekotech/1614
# @link https://sijeko.ru
##
svg=$1
dir=$(dirname "${svg}")
favDir="$dir/favicon"
echo 'Generating favicons from SVG file:' $1
echo 'Working directory:' $dir
mkdir "$favDir"
cp "$svg" "$favDir/icon.svg"
echo 'Generating PNGs…'
inkscape "$svg" --export-width=32 --export-height=32 --export-png="$favDir/32.png" --export-background-opacity=0 > /dev/null
inkscape "$svg" --export-width=64 --export-height=64 --export-png="$favDir/64.png" --export-background-opacity=0 > /dev/null
inkscape "$svg" --export-width=128 --export-height=128 --export-png="$favDir/128.png" --export-background-opacity=0 > /dev/null
inkscape "$svg" --export-width=256 --export-height=256 --export-png="$favDir/256.png" --export-background-opacity=0 > /dev/null
echo 'Optimizing PNGs…'
optipng "$favDir/32.png" > /dev/null 2>&1
optipng "$favDir/64.png" > /dev/null 2>&1
optipng "$favDir/128.png" > /dev/null 2>&1
optipng "$favDir/256.png" > /dev/null 2>&1
cp "$favDir/32.png" "$favDir/favicon.ico"
echo 'Done.'
@maximal
Copy link
Author

maximal commented Nov 30, 2018

Russian / По-русски:

Скрипт для генерации фавиконок для сайтов из исходного SVG-изображения.
Исходная SVG-картинка должна быть квадратной.
Скрипт генерирует четыре PNG-шки (квадраты размерами 32, 64, 128, 256 пк), максимально оптимизирует картинки и складывает их в отдельную папку favicon рядом с исходной SVG-шкой.
Используются: векторный редактор Inkscape и PNG-оптимизатор OptiPNG в команднострочном режиме.

Может, кому пригодится.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment