Create Favicon in PHP

In this article I will explain Favicons in PHP. In this article I will use a small and powerful script for creating Dynamic Favicons and use the PHP GD library to manipulate the Favicon image and add text into it.

Introduction
 
In this article I will explain Favicons in PHP. In this article I will use a small and powerful script for creating Dynamic Favicons and use the PHP GD library to manipulate the Favicon image and add text into it. Use the GD library to create the image. You can download GD library and include it in the WWW folder. After including the GD library you can run PHP info() to check that GD Support is enabled.
 
Example
 
This is the "icon.php" file for creating the image:
  1. <?php  
  2. $img = imagecreatefrompng("icon.png");  
  3. //"icon.png" this your image png file  
  4. if(isset($_GET['char']) && !emptyempty($_GET['char'])) {  
  5.        $string = $_GET['char'];  
  6. else {  
  7.        $string = 'V';  
  8. }  
  9. $bg = imagecolorallocate($img, 255, 255, 255);  
  10. $black = imagecolorallocate($img, 0, 0, 0);  
  11. imagechar($img, 2, 5, 1, $string$black);  
  12. header('Content-type: image/png');  
  13. imagepng($img);  
  14. ?>  
This is a "favicon.php" file for creating a dynamic Favicon icon in the PHP website.
  1. <html>  
  2. <head>  
  3.        <title>Dynamic favicon in PHP!</title>  
  4.        <link rel="shortcut icon" href="icon.php?char=<?php if(isset($_POST['char'])) { echo $_POST['char']; } ?>" />  
  5. <STYLE>  
  6. body, input{  
  7.        font-family: Calibri, Arial;  
  8.        margin:0px;  
  9.        font-size: 19px;  
  10. }  
  11. h1 {  
  12.        margin: 0 0 0 20px;  
  13. }  
  14. html, body, #container { height: 100%; }  
  15. body > #container { height: auto; min-height: 100%; }  
  16.    
  17. #header {  
  18.        height:50px;  
  19.        background-color:#ddd;  
  20.        border-bottom:1px solid #aaa;  
  21.        width:100%;  
  22.        font-size: 15px;  
  23. }  
  24. #footer {  
  25.        font-size: 12px;  
  26.        clear: both;  
  27.        position: relative;  
  28.        z-index: 10;  
  29.        height: 3em;  
  30.        margin-top: -3em;  
  31.        text-align:center;  
  32. }  
  33. .demo {  
  34.        width:150px;  
  35.        height:150px;  
  36.        padding:5px;  
  37.        background-color:#ff8811;  
  38.        position:absolute;  
  39.        top:150px;  
  40.        left:300px;  
  41. }  
  42. #content { padding-bottom: 3em; margin: 20px;}  
  43. </STYLE>  
  44. </head>  
  45. <body>  
  46. <div id="container">  
  47. <div id="header">  
  48.        <h1>Dynamic favicon in PHP</h1>  
  49. </div>  
  50. <div id="content">  
  51.        <p><a href="http://www.c-sharpcorner.com">Click here to view this Article</a></p>  
  52.        <p>Enter any character in the following textbox and Generate favicon</p>  
  53.        <form method="post">  
  54.               <label>Enter a Character to add favicon</label>  
  55.               <input type="text" name="char" style="width:40px" maxlength="1"/>  
  56.               <br/>  
  57.               <input type="submit" value="Generate favicon" />  
  58.        </form>  
  59.        Dynamic Favicon  
  60.        <img src="icon.php?char=<?php if(isset($_POST['char'])) { echo $_POST['char']; } ?>" border="0"/>  
  61.        <br/>  
  62. </div>  
  63. </div>  
  64. </body>  
  65. </html>  
Output
 
Clipboard03.jpg
 
This image is already set with a Favicon as "V" and I want to use "R" for the new Favicon. Such as shown in the following image:
 
image2.jpg
 
You will see your Favicon in your tab.
 
image3.jpg