In Gmail Just launched a small, but really useful feature in Gmail Labs. A dynamic favicon displays the number of unread emails directly on your browser tab icon. If you have lots of browser windows and lots of open tabs at all times, this could be a really fantastic feature to let the user know about any unread items.
Here is a small but powerful script in PHP that allows you to create your own dynamic favicon. We will use PHP's GD library to manipulate the favicon image and add text to it.
Below is a simple script that reads an icon image and adds some text characters.
File: favicon.php
//Read favicon favicon.png template
//File from current directory
$im = imagecreatefrompng("favicon.png");
//$im = imagecreatefromjpg("favicon.jpg"); //Use this function to load a JPEG type favicon
//$im = imagecreatefrombmp("favicon.bmp"); //Use this function to load BMP type favicon
/* The characters to be read need to be added to the favicon
* get request
*/
if(isset($_GET['char']) && !empty($_GET['char'])) {
$string = $_GET['char'];
} else {
/* If no characters are specified, add some default values */
$string = 'V';
}
/* The background color of the favicon */
$bg = imagecolorallocate($im, 255, 255, 255);
/* foreground (font) color for the favicon */
$black = imagecolorallocate($im, 0, 0, 0);
/* Write favicon character
* arguments: image, font size, x coordinate,
* Y coordinate, characterstring, color
*/
imagechar($im, 2, 5, 1, $string, $black);
header('Content-type: image/png');
imagepng($im);
?>
The code above is almost self-explanatory. We request from GET and add the favicon image to a character. Note that here, we use a template favicon image,
I modify. You can place any favicon of your choice near the favicon.php file.