Skip to content

Instantly share code, notes, and snippets.

@aldoyh
Forked from og-shawn-crigger/favicon.sh
Last active June 18, 2024 03:28
Show Gist options
  • Save aldoyh/5c3d002bfaf0241dcf4ee28a06fe065f to your computer and use it in GitHub Desktop.
Save aldoyh/5c3d002bfaf0241dcf4ee28a06fe065f to your computer and use it in GitHub Desktop.
favicon icon generator shell script

One Line Favicon Creator

Coded by: Hasan AlDoy


MIT License

Copyright (c) Facebook, Inc. and its affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#!/bin/bash
# Favicon and Apple Touch Icon Generator
#
# This bash script takes an image as a parameter, and uses ImageMagick to convert it to several
# other formats used on modern websites. The following copies are generated:
#
# * apple-touch-icon-144x144-precomposed.png
# * apple-touch-icon-114x114-precomposed.png
# * apple-touch-icon-57x57-precomposed.png
# * apple-touch-icon-72x72-precomposed.png
# * apple-touch-icon-precomposed.png
# * apple-touch-icon.png
# * favicon.ico
#
# Concept from http://bergamini.org/computers/creating-favicon.ico-icon-files-with-imagemagick-convert.html
CONVERT_CMD=`which convert`
SRC_IMAGE=$1
PWD=`pwd`
if [ -z $CONVERT_CMD ] || [ ! -f $CONVERT_CMD ] || [ ! -x $CONVERT_CMD ];
then
echo "ImageMagick needs to be installed to run this script"
exit;
fi
if [ -z $SRC_IMAGE ];
then
echo "You must supply a source image as the argument to this command."
exit;
fi
if [ ! -f $SRC_IMAGE ];
then
echo "Source image \"$SRC_IMAGE\" does not exist."
exit;
fi
echo "Generating square base image"
$CONVERT_CMD $SRC_IMAGE -resize 256x256! -transparent white $PWD/favicon-256.png
echo "Generating various sizes for ico"
$CONVERT_CMD $PWD/favicon-256.png -resize 16x16 $PWD/favicon-16.png
$CONVERT_CMD $PWD/favicon-256.png -resize 32x32 $PWD/favicon-32.png
$CONVERT_CMD $PWD/favicon-256.png -resize 64x64 $PWD/favicon-64.png
$CONVERT_CMD $PWD/favicon-256.png -resize 128x128 $PWD/favicon-128.png
echo "Generating ico"
$CONVERT_CMD $PWD/favicon-16.png $PWD/favicon-32.png $PWD/favicon-64.png $PWD/favicon-128.png $PWD/favicon-256.png -colors 256 $PWD/favicon.ico
echo "Generating touch icons"
$CONVERT_CMD $PWD/favicon-256.png -resize 57x57 $PWD/apple-touch-icon.png
cp $PWD/apple-touch-icon.png $PWD/apple-touch-icon-precomposed.png
cp $PWD/apple-touch-icon.png $PWD/apple-touch-icon-57x57-precomposed.png
$CONVERT_CMD $PWD/favicon-256.png -resize 72x72 $PWD/apple-touch-icon-72x72-precomposed.png
$CONVERT_CMD $PWD/favicon-256.png -resize 114x114 $PWD/apple-touch-icon-114x114-precomposed.png
$CONVERT_CMD $PWD/favicon-256.png -resize 144x144 $PWD/apple-touch-icon-144x144-precomposed.png
echo "Removing temp files"
rm -rf $PWD/favicon-16.png
rm -rf $PWD/favicon-32.png
rm -rf $PWD/favicon-64.png
rm -rf $PWD/favicon-128.png
rm -rf $PWD/favicon-256.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment