34

Generally it is considered good practice to serve favicon.ico in multiple sizes, because that way it looks better when a shortcut is made or the site is pinned (IE9). The size of a favicon easily increases tenfold by doing so though, which results in slower site loading (in my case the 16x16 favicon is 1kb, the 16, 32, 64=30kb).

Sites like Facebook and Yahoo serve a 16x16 favicon by default that is <1kb, but when you pin these sites, the image used is proper size. I assume the larger picture is loaded only when needed, which fixes the dilemma. I have unsuccessfully been trying to figure out how these sites do this. Does anybody know this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Do you have a specific problem that you are dealing with? Please read the [faq] to learn what types of questions are allowed here... – Lix Feb 05 '12 at 16:12
  • 3
    How to make sure a favicon gets supplied in multiple sizes, without it being too big. Using 10kb+ for a favicon that the majority of users only sees in 16x16 is a waste, considering Facebook's is 152 bytes, yet they achieve the same thing. Seems like a specific programming question to me? –  Feb 05 '12 at 16:21
  • 3
    What you are asking for is details on how a system works. If you are trying to implement this on your own site and are having difficulties then it would be more specific as you could post code/more info on what you have tried. There are lots of questions asking "how does Facebook do X"... These are not specific question and attract speculation as opposed to knowledgeable answers/solutions. I mean no dis-respect my friend - I am just trying to show you that your question (in its current form) might not be suited for this forum... – Lix Feb 05 '12 at 16:25
  • I see what you mean now, thanks. I will try to see if I can reformulate it. –  Feb 05 '12 at 16:57
  • 4
    This is a perfectly valid question. I'm looking for the same answer. Facebook seems to be serving up only a favicon that includes 16 x 16. To rephrase without using facebook: How can a website serve up only the appropriately sized favicon to satisfy the request. Browser Request, Icon to pin on IPhone, etc... Asked another way, what are the best practice for keeping the favicon small(< 1k)? – Thomas Jul 20 '12 at 22:07

5 Answers5

36

Update:

My original answer is below, however, since writing this post I believe there may be a better way to handle Favicons with HTML 5. I would create a 32x32 favicon (only that size) for Internet Explorer 9 and below but use other methods for creating higher resolution favicons (PNG file type) for other browsers including mobile devices. You can see my answer here for additional information.


Original Answer to Question:

Here is how it can be done:

  1. Download png2ico. Extract to c:\

  2. Create your Icons in your favorite program. Create a 64x64, 32x32, 16x16. (Note: 64x64 is probably not needed and will increase file size. However, use at least 32x32 and 16x16) Save as icon-64.png (for 64x64 size) icon-32.png (32x32) and icon-16.png (16x16) in the same folder as png2ico. Keep the colors to a minimum.

  3. Open Command Prompt - Change directories to the png2ico folder. (cd \png2ico)

  4. Once in the right directory run this command:

    png2ico.exe favicon.ico --colors 16 icon-64.png icon-32.png icon-16.png
    

    Note: The difference in file size for the addition of a 64x64 size icon increased the file by 2kb. I would just use 32x32 and 16x16. (Run same code as above removing icon-64.png)

  5. Grab the favicon.ico file from the png2ico folder. Upload it to the server add <link rel="shortcut icon" href="http://example.com/favicon.ico" /> to the header and you are good to go.

While you are at it go ahead and create Icons for iPad and iPhone. You can find more info on recommended sizes here and how to implement them into your site.

Also more general info on Favicons can be found here.

Note: I do not know if this is how Facebook or Yahoo do theirs but this answers your question of how it can be done.

Community
  • 1
  • 1
L84
  • 45,514
  • 58
  • 177
  • 257
  • 3
    I found [Niall's png2ico](http://www.niallmoody.com/apps/nialls-png2ico) much easier to use and it also supports a full alpha channel. – Twilite Apr 02 '12 at 15:19
  • 1
    On Ubuntu, you can use icotool as per http://steve.kargs.net/hosting/create-a-website-faviconico-with-ubuntu-linux/ – Sam King Aug 07 '12 at 03:55
  • 7
    This doesn't address the OP's question. He's asking how to serve up various sizes without using a bloated ICO file. – Doug S Oct 21 '12 at 23:35
  • the correct sizes are 32px(win7 taskbar pinned size), 24px(IE9), 16px (default) – gcb Jun 02 '13 at 20:38
  • @DougS - This method can create a bloated ico file but I was able to create some ico files that were around 1kb and I do not consider that bloated. Do not use 64x64 file and keep your colors to a minimum and, as you stated in your answer, use optimized PNG files. There are many places on the web that you can optimize PNG files. I imagine some are better than others but this is not the focus of the question or my answer. – L84 Jun 13 '13 at 02:59
  • While off topic, for those looking for PNG optimization I have found [TinyPNG](https://tinypng.com/) is very good at creating highly optimized PNG images without losing quality. – L84 May 25 '14 at 22:12
  • unfortunately the specified tool doesn't support alpha, if you need alpha use the method below with GIMP - open first largest PNG then open others as layers then export to ICO – VirtualVAT Jun 09 '23 at 09:08
21

The Facebook "favicon.ico" contains two sizes: 16x16 and 32x32. I'm sure you know how to combine two ico images into one, however, the "trick" is they're using two extremely optimized images.

I've found that Photoshop creates bloated PNG files, and bloated ICO files. (Note: Photoshop requires a plugin to create ICO files.)

The best way I've found to create tiny, optimized ICO files is to use a well-known free image editor called "Gimp". In short, just follow this tutorial for creating ICO files:
https://www.catalyst.net.nz/news/creating-multi-resolution-favicon-including-transparency-gimp
Important Tip: When you get to the step for saving your .ico image and you can specify the bits-per-pixel (bpp), change it to 4bpp or something similiar (you'll have to experiment to see how low you can go without degrading the image quality).

Using the instructions above, I was able to create a 1kb favicon that contains 16x16 and 32x32 images. In comparison, the smallest I could get the same favicon using Photoshop, plugins, and various other tools was 5kb.

Naeio
  • 1,112
  • 7
  • 21
Doug S
  • 10,146
  • 3
  • 40
  • 45
  • 6
    Link now dead. Archived copy here: http://web.archive.org/web/20130124171138/http://egressive.com/tutorial/creating-a-multi-resolution-favicon-including-transparency-with-the-gimp – simon Jul 16 '13 at 01:17
  • 1
    New URL is the following: http://catalyst.net.nz/tutorial/creating-a-multi-resolution-favicon – drAlberT Feb 17 '14 at 12:56
  • and the latest is broken too, use this one https://www.tutorialspoint.com/How-to-create-a-multi-resolution-favicon-with-GIMP# – VirtualVAT Jun 09 '23 at 09:09
5

Or you could just log into any linux box with ImageMagick installed, rename your source image (with a resolution of at least 256x256 pixels and a PNG format file) to 'favicon.png', and run the following command:

convert favicon.png -bordercolor white -border 0 \
      \( -clone 0 -resize 16x16 \) \
      \( -clone 0 -resize 32x32 \) \
      \( -clone 0 -resize 48x48 \) \
      \( -clone 0 -resize 57x57 \) \
      \( -clone 0 -resize 64x64 \) \
      \( -clone 0 -resize 72x72 \) \
      \( -clone 0 -resize 110x110 \) \
      \( -clone 0 -resize 114x114 \) \
      \( -clone 0 -resize 120x120 \) \
      \( -clone 0 -resize 128x128 \) \
      \( -clone 0 -resize 144x144 \) \
      \( -clone 0 -resize 152x152 \) \
      \( -clone 0 -resize 180x180 \) \
      \( -clone 0 -resize 228x228 \) \
      -delete 0 -alpha off -colors 256 favicon.ico

And you'll have your favicon.ico with most well-known formats baked right into one file.

Also, be sure to checkout the favicon cheat sheet @ https://github.com/audreyr/favicon-cheat-sheet for more favicon information.

Henry van Megen
  • 2,159
  • 2
  • 23
  • 35
  • 3
    you can remove the "-alpha off" if you want to use transparancy in your favicon... or maybe even go to http://realfavicongenerator.net/ (this site has even more options (you can specify a background of iOS specific stuff, etc.) – Henry van Megen Jun 24 '14 at 09:27
  • 1
    Best answer of this topic ! Thanks @cappie ! – pierallard Aug 12 '14 at 09:20
1

Windows batch file, which creates multiple sized .PNGs and merge them to one .ICO file:

@echo off

set inkScape="C:\SOFTWARE\GRAPHIC\INKSCAPE\inkscape.exe"
set imageMagick="C:\SOFTWARE\DEVELOPER\IMAGEMAGICK\magick.exe"
set fileName=favicon
set importType=svg
set exportType=png
set exportDpi=300
set imageSizes=(16 24 32 48 57 60 64 70 72 76 96 114 120 128 144 150 152 180 192 196 256 300 320 400 450 460 480 512 600)

for %%s in %imageSizes% do (
 %inkScape% -z -f %~dp0%fileName%.%importType% -w %%s -h %%s -e %~dp0%fileName%-%%sx%%s.%exportType% -d %exportDpi%
 echo CREATED: %fileName%-%%sx%%s.%exportType%
 set e=%fileName%-%%sx%%s.%exportType%
 call :concat (e)
)

%imageMagick% %exportFileNames%"%~dp0%fileName%.ico"
echo MERGED IN: %fileName%.ico

pause goto :eof


:concat (e) (
 set exportFileNames=%exportFileNames%"%~dp0%e%" 
)

If you don't need the .PNG files, you can delete (or remove) them by del FILE or you save all PNGs inside a directory you can remove (after %imageMagick% %exportFileNames%"%~dp0%fileName%.ico").

Hope it helps somebody. :)

Froschkoenig84
  • 566
  • 4
  • 13
0

If you like scripts, I wrote one to convert any image to a multi-resolution favicon using ImageMagick.

http://blog.lavoie.sl/2012/11/multi-resolution-favicon-using-imagemagick.html

For your question about why Facebook and Yahoo are able to have high-res "pinned" images, it is because they also have apple-touch-icon and og:image.

sebastien
  • 418
  • 4
  • 7