#!/bin/bash ## mkfavicon-transparent -- convert any image into a transparent image in the FAVICON-format -- by Eugene Reimer, 2007-Apr; ## using png2ico, since it's simpler when wanting transparency (compared to ppmtowinicon, as used by mkfavicon for nontransparent favicon-making); ## note: uses TOP-LEFT pixel to get the colour that's to become transparent; done in img2transparent, which uses fuzzy-matching; ## ## the ppmtowinicon documentation says Microsoft recommends including 16x16 (16-color), 32x32 (16-color), and 48x48 (256-color) images; ## but that seems a waste of bandwidth, as it makes a big file, and most browsers never use anything other than 16x16; ## I made nativeorchid's favicon.ico that way (except for max 64-colors?), for a filesize of 3638 bytes; ## making it with only the first two image-sizes, yields a filesize of 1078 bytes; ## using only a 16x16 image, yields a filesize of 318 bytes; (these filesizes are independent of image complexity, transparency, etc) ## ## if input image is smaller than 32x32, then makes only the 16x16 size; otherwise makes both 16x16 and 32x32 sizes, each in 16-colours. OF=favicon.ico; [ $# -ge 2 ] && OF=$2; ##optional 2nd param is the result-name (default: favicon.ico) convert $1 tmp$$.ppm ##convert input to PPM w=$(pnmfile tmp$$.ppm |gawk '{ print $4 }') ##get w=width of image h=$(pnmfile tmp$$.ppm |gawk '{ print $6 }') ##get h=height of image pnmscale -xysize 16 16 tmp$$.ppm 2>/dev/null >tmp$$16r.ppm; pnmquant 16 tmp$$16r.ppm >tmp$$16q.ppm; img2transparent tmp$$16q.ppm tmp$$16.png ##to 16x16 in 16-colors if ((w*h >= 32*32)); then pnmscale -xysize 32 32 tmp$$.ppm 2>/dev/null >tmp$$32r.ppm; pnmquant 16 tmp$$32r.ppm >tmp$$32q.ppm; img2transparent tmp$$32q.ppm tmp$$32.png ##to 32x32 in 16-colors png2ico $OF --colors 16 tmp$$16.png tmp$$32.png ##produce favicon output file containing both 16x16 and 32x32 images echo "wrote $OF with 16x16 and 32x32 images" else png2ico $OF --colors 16 tmp$$16.png ##produce favicon output file containing only 16x16 image echo "wrote $OF with 16x16 image only" fi rm tmp$$* ##yank to debug using tmp files