Skip to content

Instantly share code, notes, and snippets.

@bf4
Forked from tasdikrahman/ico.rst
Last active March 14, 2016 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/d301cdb4cab7615fa9b6 to your computer and use it in GitHub Desktop.
Save bf4/d301cdb4cab7615fa9b6 to your computer and use it in GitHub Desktop.
Everything you ever wanted to know about ico's
#!/bin/bash
# Copied from https://github.com/tlvince/scripts-shell/blob/808f36d2144f5b9ca4399e6cef4add96279c75d7/favicon.bash
# Favicon and Apple Touch Icon Generator.
# Copyright 2012 @emarref
# Copyright 2012 Tom Vincent <http://tlvince.com/contact>
# From: https://gist.github.com/3374193
#
# 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-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
info() { echo "$0: $1"; }
error() { info "$1" >&2 && exit 1; }
have() { which "$1" >/dev/null 2>&1; }
usage() { echo "usage: $0 src_image [dest_dir]"; exit 1; }
[ $1 ] || usage
have convert || error "ImageMagick not found"
tmp="$(mktemp -d favicon.XXX)"
info "Generating square base image"
convert "$1" -flatten -resize 256x256! -transparent white "$tmp/favicon-256.png"
[ -f "$tmp/favicon-256.png" ] || error "Generating square base image failed"
info "Generating ico"
convert "$tmp/favicon-256.png" -resize 16x16 "$tmp/favicon-16.png"
convert "$tmp/favicon-16.png" -colors 256 "$tmp/favicon.ico"
info "Generating touch icons"
convert "$tmp/favicon-256.png" -resize 57x57 "$tmp/apple-touch-icon.png"
cp "$tmp/apple-touch-icon.png" "$tmp/apple-touch-icon-precomposed.png"
cp "$tmp/apple-touch-icon.png" "$tmp/apple-touch-icon-57x57-precomposed.png"
convert "$tmp/favicon-256.png" -resize 72x72 "$tmp/apple-touch-icon-72x72-precomposed.png"
convert "$tmp/favicon-256.png" -resize 114x114 "$tmp/apple-touch-icon-114x114-precomposed.png"
convert "$tmp/favicon-256.png" -resize 120x120 "$tmp/apple-touch-icon-120x120-precomposed.png"
convert "$tmp/favicon-256.png" -resize 144x144 "$tmp/apple-touch-icon-144x144-precomposed.png"
info "Removing temp files"
rm "$tmp/favicon-16.png"
[ "$2" ] && { mkdir -p "$2"; mv $tmp/* "$2"; rm -rf "$tmp"; } || info "$tmp"

This is a clone of the repo favicon-cheat-sheet

favicon-cheat-sheet

A painfully obsessive cheat sheet to favicon sizes/types. Compiled from:

The HTML

Basics

For the main favicon itself, it's best for cross-browser compatibility not to use any HTML. Just name the file favicon.ico and place it in the root of your domain.12

This works in every desktop browser/version all the way back to IE6, except for SeaMonkey.3

Optional But Encouraged

You probably also want the following:

  1. Touch icon for iOS 2.0+ and Android 2.1+:

    <link rel="apple-touch-icon-precomposed" href="path/to/favicon-152.png">
  2. IE 10 Metro tile icon (Metro equivalent of apple-touch-icon):

    <meta name="msapplication-TileColor" content="#FFFFFF">
    <meta name="msapplication-TileImage" content="/path/to/favicon-144.png">

    Replace #FFFFFF with your desired tile color.

Very Optional, for the Obsessive

If you're obsessive, you want all this too:

  1. Largest to smallest apple-touch-icons4:

    <!-- For iPad with high-resolution Retina display running iOS ≥ 7: -->
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="/path/to/favicon-152.png">
    
    <!-- For iPad with high-resolution Retina display running iOS ≤ 6: -->
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/path/to/favicon-144.png">
    
    <!-- For iPhone with high-resolution Retina display running iOS ≥ 7: -->
    <link rel="apple-touch-icon-precomposed" sizes="120x120" href="/path/to/favicon-120.png">
    
    <!-- For iPhone with high-resolution Retina display running iOS ≤ 6: -->
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="/path/to/favicon-114.png">
    
    <!-- For first- and second-generation iPad: -->
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="/path/to/favicon-72.png">
    
    <!-- For non-Retina iPhone, iPod Touch, and Android 2.1+ devices: -->
    <link rel="apple-touch-icon-precomposed" href="/path/to/favicon-57.png">
  2. Favicons targeted to any additional png sizes that you add that aren't covered above:

    <link rel="icon" href="/path/to/favicon-32.png" sizes="32x32">

The Images

Create at least this:

Sizes Name Purpose
16x16 & 32x32 favicon.ico Default required by IE. Chrome and Safari may pick ico over png, sadly.

More about favicon.ico below. Yes, it's 1 file with multiple sizes.

If you also sort of care about iOS and Android but are lazy:

Size Name Purpose
152x152 favicon-152.png General use iOS/Android icon, auto-downscaled by devices.

But keep in mind that icons with complex detail often don't downscale well. Often you have to tweak subtle design details for smaller sizes.

If you're obsessive, create these too:

Size Name Purpose
32x32 favicon-32.png Certain old but not too old Chrome versions mishandle ico
57x57 favicon-57.png Standard iOS home screen (iPod Touch, iPhone first generation to 3G)
72x72 favicon-72.png iPad home screen icon
96x96 favicon-96.png GoogleTV icon
120x120 favicon-120.png iPhone retina touch icon (Change for iOS 7: up from 114x114)
128x128 favicon-128.png Chrome Web Store icon
144x144 favicon-144.png IE10 Metro tile for pinned site
152x152 favicon-152.png iPad retina touch icon (Change for iOS 7: up from 144x144)
195x195 favicon-195.png Opera Speed Dial icon
228x228 favicon-228.png Opera Coast icon

ICO File

An .ico file is a container for multiple .bmp or .png icons of different sizes. In favicon.ico, create at least these:

Size Purpose
16x16 IE9 address bar, Pinned site Jump List/Toolbar/Overlay
32x32 New tab page in IE, taskbar button in Win 7+, Safari Read Later sidebar
48x48 Windows site icons5

If you're obsessive and don't mind 1-3kb extra size, also include these sizes in your .ico:

Size Purpose
24x24 IE9 Pinned site browser UI
64x64 Windows site icons6, Safari Reading List sidebar in HiDPI/Retina

Create your .ico out of optimized .png files.

TODO: get confirmation that IE9+ supports .ico files that contain .png files (issue #9)

Helpful Tools

I recommend:

  1. OptiPNG, to optimize .png files before putting them into an .ico: http://optipng.sourceforge.net/
  2. ImageMagick, to create an .ico from .png files: http://blog.morzproject.com/convert-multiple-png-images-into-a-single-icon-file/ & http://www.imagemagick.org/Usage/thumbnails/#favicon

    $ convert favicon-16.png favicon-32.png favicon.ico

Others that I haven't tried:

Forcing a Favicon Refresh

Not normally needed. This is only for those frustrating times when you can't get your favicon to refresh, during development:

  • Clear the browser cache on Windows (Ctrl+F5 or Ctrl+Shift+R) and on Mac (Command + Shift + R).
  • Also close and reopen browser if IE.
  • If still stuck, try opening new tab. Or see http://stackoverflow.com/questions/2208933/how-do-i-force-a-favicon-refresh
  • Temporarily add explicit HTML markup and append a query string. Remove this when you're done:

    <link rel="shortcut icon" href="http://www.yoursite.com/favicon.ico?v=2" />
    <link rel="icon" sizes="16x16 32x32" href="/favicon.ico?v=2">

For large versioned deployments, if all site visitors need their favicon force-refreshed in an extreme situation:

  • Add explicit HTML markup (customize the sizes part) and put your version number in the filename.

    <link rel="shortcut icon" href="/favicon-v2.ico" />
    <link rel="icon" sizes="16x16 32x32" href="/favicon-v2.ico">

    TODO: find edge cases where this markup doesn't work (issue #3).

FAQ

What about having both a default root favicon.ico and favicon.png? I think it's actually better to provide only favicon.ico and not favicon.png, because:

  • An .ico is a container for multiple .bmp or .png files. If you specify 1 default favicon.png, and if that favicon.png overrides the favicon.ico, you are giving up control over how the favicon looks at different resolutions and allowing the browser to do all resizing. For example, you might want the 64x64 version to contain text and the 16x16 version to not display the text at all, since at 16x16 it would be unreadable anyway.
  • There is no favicon.png in the HTML5 specification, just /favicon.ico. From http://www.w3.org/TR/html5/links.html#rel-icon:
    • 'In the absence of a link with the icon keyword, for Documents obtained over HTTP or HTTPS, user agents may instead attempt to fetch and use an icon with the absolute URL obtained by resolving the URL "/favicon.ico" against the document's address, as if the page had declared that icon using the icon keyword.'

More about this in http://stackoverflow.com/questions/1344122/favicon-png-vs-favicon-ico-why-should-i-use-pngs-instead-of-icos/1344379#1344379 (Note: the text in the chosen answer about alpha transparency is incorrect. See the 2nd answer.)

Is it true that favicons should be in the site root? No, that's only if you don't explicitly specify the browser/device-specific <link> tags with a favicon path. See https://en.wikipedia.org/wiki/Favicon.ico.

If you don't have favicon.ico in the root consider adding one, or returning a HTTP 204 instead. Many tools and services e.g. bookmarking sites, feed readers, web crawlers etc., request a favicon.ico from the site root, and so receive a HTTP 404 if it's not present. In the worst case some frameworks will return a custom error page which is likely to be many times larger than the missing favicon.

Is it true that the png has to be named favicon.png? No, this has never been true as far as I can tell from my obsessive research.

Is it true that the ico has to be named favicon.ico? If you don't explicitly specify its <link> tag, yes. Explicitness is best, so we both name it favicon.ico and explicitly specify the <link> tag.

Why not prefix with "apple-touch-icon"? If you don't specify <link> tags, iOS looks for files prefixed with apple-touch-icon or apple-touch-icon-precomposed. Many (e.g. HTML5 Boilerplate) rely on this assumption, but:

  • Explicitly specifying <link> tags is clearer and supported by Apple.
  • Not hard-coding names as apple-touch-icon clears up confusion as to whether the same icons can be reused for other purposes as-is, e.g. reusing favicon-144.png for Windows pinned site.

Why use iOS precomposed icons?

  • iOS non-precomposed icons add rounded corners, drop shadow, and reflective shine. Sounds great in theory, but in practice the results can be very frustrating, especially to designers.
  • Non-precomposed icons don't work with Android 2.1.

Why absolute paths? Some Firefox versions require absolute paths. Since all browsers support them, it's the simplest choice.

Why not append a query string to force-refresh for all visitors? Some proxies and load balancers can fail to read query strings in edge cases.

Contribute!

Send pull requests if you have anything to add/change, providing citations and justification. I'd love to see this improve.

References


  1. http://mathiasbynens.be/notes/rel-shortcut-icon

  2. http://www.w3.org/html/wg/drafts/html/CR/links.html#rel-icon

  3. http://mathiasbynens.be/notes/rel-shortcut-icon

  4. Adapted from http://mathiasbynens.be/notes/touch-icons

  5. No specifics given by MSDN.

  6. No specifics given by MSDN.

#!/usr/bin/env bash
# Copied from https://github.com/emarref/webicon/blob/master/webicon.sh
# MIT license https://github.com/emarref/webicon/blob/8fcad9f25411048bb52b13973224042a7d6667a9/LICENSE
######################################
# INITIALIZE VARS
CONVERT_CMD=`which convert`
SRC_IMAGE=$1
PWD=`pwd`
TRANSPARENT_COLOUR="#FFFFFF"
IMAGE_NAME="favicon"
WEBSITE_DOMAIN="http://www.yourwebsite.com"
######################################
# COLOURS
GREY="\033[90m"
GREEN="\033[32m"
BLUE="\033[34m"
CYAN="\033[36m"
RED="\033[31m"
NO_COLOUR="\033[0m"
COLOUR_COMMENT=$GREY
COLOUR_TAG=$BLUE
COLOUR_ATTR_NAME=$CYAN
COLOUR_ATTR_VALUE=$RED
COLOUR_INTRO=$GREEN
######################################
# REQUIREMENTS
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
function generate_png {
local SIZE=$1
local SOURCE=$2
if [ -z "$SOURCE" ];
then
SOURCE="$PWD/$IMAGE_NAME-256.png"
fi
if [ ! -f $SOURCE ];
then
echo "Could not find the source image $SOURCE"
exit 1;
fi
if [[ $SIZE =~ ^([0-9]+)x([0-9]+)$ ]];
then
WIDTH=${BASH_REMATCH[1]}
HEIGHT=${BASH_REMATCH[2]}
else
WIDTH=$SIZE
HEIGHT=$SIZE
fi
echo "$IMAGE_NAME-${SIZE}.png"
$CONVERT_CMD $SOURCE -resize ${WIDTH}x${HEIGHT}! -crop ${WIDTH}x${HEIGHT}+0+0 -alpha On $PWD/$IMAGE_NAME-${SIZE}.png
}
echo "Generating square base image"
# Converts the source image to 256 square, ignoring aspect ratio
generate_png 256 $SRC_IMAGE
######################################
# GENERATE THE VARIOUS SIZE VERSIONS
echo "Generating required versions at different sizes"
generate_png 16
generate_png 32
generate_png 48
generate_png 57
generate_png 64
generate_png 70
generate_png 72
generate_png 114
generate_png 120
generate_png 144
generate_png 150
generate_png 152
# TODO Figure out crop/resize priority etc.
# generate_png 310x150
generate_png 310
######################################
# GENERATE THE FAVICON.ICO FILE
echo "Generating ico"
$CONVERT_CMD \
$PWD/$IMAGE_NAME-16.png \
$PWD/$IMAGE_NAME-32.png \
$PWD/$IMAGE_NAME-48.png \
$PWD/$IMAGE_NAME-64.png \
-background $TRANSPARENT_COLOUR $PWD/$IMAGE_NAME.ico
######################################
# OUTPUT USEFUL MARKUP
echo
echo
echo -e "${COLOUR_COMMENT}_______________________________________________________________________________________${NO_COLOUR}"
echo
echo -e "${COLOUR_INTRO}Add the following tags to the head of your document. Remove any you don't want.${NO_COLOUR}"
echo -e "${COLOUR_INTRO}Compiled from https://github.com/audreyr/favicon-cheat-sheet${NO_COLOUR}"
echo
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"icon\"${COLOUR_ATTR_NAME} sizes=${COLOUR_ATTR_VALUE}\"16x16 32x32 48x48 64x64\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon.ico\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!--[if IE]><link rel=\"shortcut icon\" href=\"${WEBSITE_DOMAIN}/favicon.ico\"><![endif]-->${NO_COLOUR}"
echo
echo -e "${COLOUR_COMMENT}<!-- The below are optional but encouraged -->${NO_COLOUR}"
echo
echo -e "${COLOUR_COMMENT}<!-- Touch icon for iOS 2.0+ and Android 2.1+: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"apple-touch-icon-precomposed\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-152.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo
echo -e "${COLOUR_COMMENT}<!-- The below are optional -->${NO_COLOUR}"
echo
echo -e "${COLOUR_COMMENT}<!-- IE 10 Metro tile icon (Metro equivalent of apple-touch-icon): -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<meta${COLOUR_ATTR_NAME} name=${COLOUR_ATTR_VALUE}\"msapplication-TileColor\"${COLOUR_ATTR_NAME} content=${COLOUR_ATTR_VALUE}\"$TRANSPARENT_COLOUR\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_TAG}<meta${COLOUR_ATTR_NAME} name=${COLOUR_ATTR_VALUE}\"msapplication-TileImage\"${COLOUR_ATTR_NAME} content=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-144.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!-- IE 11 live tiles: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<meta${COLOUR_ATTR_NAME} name=${COLOUR_ATTR_VALUE}\"msapplication-square70x70logo\"${COLOUR_ATTR_NAME} content=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-70.png\"${COLOUR_TAG} />${NO_COLOUR}"
echo -e "${COLOUR_TAG}<meta${COLOUR_ATTR_NAME} name=${COLOUR_ATTR_VALUE}\"msapplication-square150x150logo\"${COLOUR_ATTR_NAME} content=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-150.png\"${COLOUR_TAG} />${NO_COLOUR}"
# echo -e "${COLOUR_TAG}<meta${COLOUR_ATTR_NAME} name=${COLOUR_ATTR_VALUE}\"msapplication-wide310x150logo\"${COLOUR_ATTR_NAME} content=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-310x150.png\"${COLOUR_TAG} />${NO_COLOUR}"
echo -e "${COLOUR_TAG}<meta${COLOUR_ATTR_NAME} name=${COLOUR_ATTR_VALUE}\"msapplication-square310x310logo\"${COLOUR_ATTR_NAME} content=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-310.png\"${COLOUR_TAG} />${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!-- For iPad with high-resolution Retina display running iOS ≥ 7: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"apple-touch-icon-precomposed\"${COLOUR_ATTR_NAME} sizes=${COLOUR_ATTR_VALUE}\"152x152\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-152.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!-- For iPad with high-resolution Retina display running iOS ≤ 6: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"apple-touch-icon-precomposed\"${COLOUR_ATTR_NAME} sizes=${COLOUR_ATTR_VALUE}\"144x144\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-144.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!-- For iPhone with high-resolution Retina display running iOS ≥ 7: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"apple-touch-icon-precomposed\"${COLOUR_ATTR_NAME} sizes=${COLOUR_ATTR_VALUE}\"120x120\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-120.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!-- For iPhone with high-resolution Retina display running iOS ≤ 6: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"apple-touch-icon-precomposed\"${COLOUR_ATTR_NAME} sizes=${COLOUR_ATTR_VALUE}\"114x114\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-114.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!-- For first- and second-generation iPad: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"apple-touch-icon-precomposed\"${COLOUR_ATTR_NAME} sizes=${COLOUR_ATTR_VALUE}\"72x72\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-72.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo -e "${COLOUR_COMMENT}<!-- For non-Retina iPhone, iPod Touch, and Android 2.1+ devices: -->${NO_COLOUR}"
echo -e "${COLOUR_TAG}<link${COLOUR_ATTR_NAME} rel=${COLOUR_ATTR_VALUE}\"apple-touch-icon-precomposed\"${COLOUR_ATTR_NAME} href=${COLOUR_ATTR_VALUE}\"${WEBSITE_DOMAIN}/favicon-57.png\"${COLOUR_TAG}>${NO_COLOUR}"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment