Home > Article > Backend Development > Favicon dynamics of unread count in PHP_PHP Tutorial
In Gmail A small, but really useful feature has just been launched 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
//Files from the 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 {
/* Add some default values if no characters are specified */
$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 characters
* 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 are using a template favicon image,
I modified. You can place any favicon of your choice near the favicon.php file.