Skip to content

Instantly share code, notes, and snippets.

@nazares
Created June 5, 2024 09:12
Show Gist options
  • Save nazares/df29e10b19e003a91b845377ae38dadc to your computer and use it in GitHub Desktop.
Save nazares/df29e10b19e003a91b845377ae38dadc to your computer and use it in GitHub Desktop.
make favicon in a right way
#!/usr/bin/env bash
svgFile=$1
extension=${svgFile##*.}
OUT_DIR="./favicons"
TEMP_DIR='/tmp'
sizes=( 16 32 140 192 512 )
convertFiles() {
echo -n "Converting files"
for size in "${sizes[@]}"
do
echo -n "."
inkscape "${svgFile}" --export-width="${size}" --export-filename="${TEMP_DIR}/icon-${size}.png"
done
convert "${TEMP_DIR}"/icon-140.png -background none -gravity center -extent 180x180 "${OUT_DIR}"/apple-touch-icon.png
convert "${TEMP_DIR}"/icon-32.png "${TEMP_DIR}"/icon-16.png "${OUT_DIR}"/favicon.ico
}
mkManifest() {
echo -e "\nCreate manifest file"
cat << EOF > "${OUT_DIR}"/manifest.webmanifest
{
"icons": [
{ "src": "icon-192.png", "type": "image/png", "sizes": "192x192" },
{ "src": "icon-512.png", "type": "image/png", "sizes": "512x512" }
]
}
EOF
}
printText() {
echo -e "Put these lines in your index file\n"
cat << EOF
<link rel="icon" href="/favicon.ico" sizes="any"><!-- 32×32 -->
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png"><!-- 180×180 -->
<link rel="manifest" href="/manifest.webmanifest">
EOF
}
cleanup () {
echo "Cleaning up."
for size in "${sizes[@]}"; do
rm "${TEMP_DIR}"/icon-"${size}".png
done
}
main() {
if [[ ${extension} != svg ]]; then
echo "Usage: ${0} <file.svg>"
exit 0
fi
if [[ -d ${OUT_DIR} ]]; then
echo "Attention! The folder ${OUT_DIR} is actually exist."
exit 0;
fi
mkdir "${OUT_DIR}"
convertFiles
mkManifest
cp "${svgFile}" "${OUT_DIR}"/icon.svg
cp -t "${OUT_DIR}" "${TEMP_DIR}"/{icon-192.png,icon-512.png}
cleanup
printText
xdg-open "${OUT_DIR}/"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment