Dynamic unread count Favicon in PHP

Gmail has just launched small but really useful feature in Gmail Labs. Dynamic Favicon showing unread email counts directly in your browser tab icon. If your browser window has lots and lots of tabs open at anytime, this might be really a wonderful feature that lets user know of any unread item. Here is a small and powerful script in PHP that lets you create your own Dynamic favicon. We will use PHP GD library to manipulate the favicon image and add text into it. Below is the simple script that reads a favicon image add add some text character on it. File: favicon.php
<?php //Read the favicon template from favicon.png //file from current directory $im = imagecreatefrompng("favicon.png"); //$im = imagecreatefromjpg("favicon.jpg"); //using this function to load favicon of jpeg type //$im = imagecreatefrombmp("favicon.bmp"); //using this function to load favicon of bmp type /* Read the character which needs to be added in favicon from * get request */ if(isset($_GET['char']) && !empty($_GET['char'])) { $string = $_GET['char']; } else { /* If no character is specified; add some default value */ $string = 'V'; } /* background color for the favicon */ $bg = imagecolorallocate($im, 255, 255, 255); /* foreground (font) color for the favicon */ $black = imagecolorallocate($im, 0, 0, 0); /* Write the character in favicon * arguements: image, fontsize, x-coordinate, * y-coordinate, characterstring, color */ imagechar($im, 2, 5, 1, $string, $black); header('Content-type: image/png'); imagepng($im); ?>
Code language: PHP (php)
The above code is pretty much self explanatory. We read a character from GET request and add it into the favicon image. Note here that we are using a template favicon image which me modify. You can place any favicon of your choice near favicon.php file.

Download Source Code

Click here to download complete source code (ZIP, 3kb)

Online Demo

Click here to view online demo

View Comments

Share
Published by
Viral Patel
Tags: dynamic elements favicon PHP

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

5 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

5 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago