Created
August 31, 2023 21:32
-
-
Save Thomashighbaugh/53bc4c4d99c50ea5f235739bec1258d4 to your computer and use it in GitHub Desktop.
Bash script to generate favicons of an assortment of sizes. Lighter, faster and more portable than online tools to achieve the same task necessary for creativity infused web development.
This file contains 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
#!/usr/bin/env bash | |
# from https://github.com/fariasmateuss/favicon-generator/blob/main/favicon.sh | |
# Provide an image as a parameter, recieve favicons | |
SRC_IMAGE=$1 | |
CYAN='\033[0;36m' | |
RED='\033[0;31m' | |
COLORRESET='\033[0m' | |
declare -a SIZES | |
declare -a GENERATED | |
declare -a ICONS | |
# missing critical infrastructure | |
if ! command -v convert >/dev/null 2>&1; then | |
echo -e "${RED}ImageMagick is not installed.${COLORRESET}" | |
exit 1 | |
fi | |
# no orovided image | |
if [ -z "$SRC_IMAGE" ]; then | |
echo -ne "${RED}You must supply a source image as the argument to this command. \n${COLORRESET}" | |
exit 1 | |
fi | |
# For when the image is not found | |
if [ ! -f "$SRC_IMAGE" ]; then | |
echo -ne "${RED}Source image \"$SRC_IMAGE\" does not exist. \n${COLORRESET}" | |
exit 1 | |
fi | |
# create a square image as a base, make sure the base image is square too | |
NAME="${SRC_IMAGE/\.*/-512x512.png}" | |
echo -ne "${CYAN}Generating base image and regularizing image dimensions... \n${COLORRESET}" | |
convert "$SRC_IMAGE" -resize 512x512! -transparent white "${NAME}" | |
# Dimensioms of output images, adding new sizes is as simple as adding am item to this array with the square dimensions inside quotation marks | |
SIZES=("16x16" "32x32" "57x57" "60x60" "64x64" "70x70" "72x72" "76x76" "96x96" "114x114" "120x120" "128x128" "144x144" "150x150" "152x152" "167x167" "180x180" "192x192" | |
"256x256" "310x310" "512x512") | |
# Loop through sizes array and resize + rename image correspondingly | |
echo -ne "${CYAN}Generating favicons... \n${COLORRESET}" | |
for i in "${SIZES[@]}"; do | |
: "$NAME" | |
convert "$_" -resize "${i}" "${_/512x512\.png/$i\.png}" | |
ICONS+=("$_") | |
done | |
echo -ne "${CYAN}Generating .ico file... \n${COLORRESET}" | |
convert "${ICONS[@]:0:4}" -colors 256 favicon.ico | |
# Create HTML tags in generated file | |
if [[ $2 == "-x" ]]; then | |
count=0 | |
: >favicons.txt | |
echo -ne "${CYAN}Creating html tags in favicons.txt.... \n${COLORRESET}" | |
for i in "${GENERATED[@]}"; do | |
echo "<link rel=\"shortcut icon\" type=\"image/png\" sizes=\"${SIZES[count]}\" href=\"${i}\" />" >>favicons.txt | |
count=$((count + 1)) | |
done | |
fi | |
echo -ne "${RED}Favicon generation is now completed!\n${COLORRESET}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment