#!/bin/bash ### # Consumes a given image to convert it to ICO # @param Path to the input file ### function convertToIco { imageWidth=$(identify -ping -format '%w ' "${1}" | awk -F' ' '{ print $NF }') imageHeight=$(identify -ping -format '%h ' "${1}" | awk -F' ' '{ print $NF }') if [ ${imageWidth} -lt 64 ] || [ ${imageHeight} -lt 64 ]; then echo "Warning: Favicon is only ${imageWidth}x${imageHeight}, it'll probably look pixely" fi mimeType=$(file -bi "${1}") if [ $? -eq 0 ] && ! grep -qE "^image/vnd.microsoft.icon" <<< "${mimeType}"; then if convert "${1}" -resize "64x64" "$(dirname "${1}")/$(basename "${1}" ".${1##*.}").ico"; then rm "${1}" else return 1 fi fi } ### # Tries to download the favicon of a website # @param URL of the website # @param Filename of the downloaded ico file ### function fetchFavicon { domain=$(awk -F'/' '{ print $3 }' <<< "${1}") protoAndDomain=$(awk -F'/' '{ print $1"//"$3 }' <<< "${1}") echo "Fetching favicon for ${protoAndDomain}" faviconUrl=$(curl -s -L "${1}" | grep -E '/dev/null) if [ $? -eq 0 ] && [ "${faviconUrl}" != "" ]; then if ! grep -E "https?://" <<< "${faviconUrl}"; then faviconUrl="${protoAndDomain}${faviconUrl}" fi mimeType=$(curl -Lso "${2}" -w "%{content_type}" "${faviconUrl}") if [ $? -eq 0 ] && grep -E '^image/' <<< "${mimeType}"; then echo "Success using link tag method" convertToIco "${2}" return 0 fi fi curlOut=($(curl -Lso "${2}" -w '%{http_code} %{content_type}' "${protoAndDomain}/favicon.ico")) if [ $? -eq 0 ] && [ "${curlOut[0]}" = "200" ] && grep -qE '^image/' <<< "${curlOut[1]}"; then echo "Success using /favicon.ico method" convertToIco "${2}" return 0 fi curlOut=($(curl -Lso "${2}" -w '%{http_code} %{content_type}' "https://www.google.com/s2/favicons?domain=${domain}&sz=64")) if [ $? -eq 0 ] && [ "${curlOut[0]}" = "200" ] && grep -qE '^image/' <<< "${curlOut[1]}"; then echo "Success using Google Favicons API" convertToIco "${2}" return 0 fi echo "Failed. You'll have to supply your own icon file at:" echo " ${2}" }