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:
- <?php
- $img = imagecreatefrompng("icon.png");
-
- if(isset($_GET['char']) && !emptyempty($_GET['char'])) {
- $string = $_GET['char'];
- } else {
- $string = 'V';
- }
- $bg = imagecolorallocate($img, 255, 255, 255);
- $black = imagecolorallocate($img, 0, 0, 0);
- imagechar($img, 2, 5, 1, $string, $black);
- header('Content-type: image/png');
- imagepng($img);
- ?>
This is a "favicon.php" file for creating a dynamic Favicon icon in the PHP website.
- <html>
- <head>
- <title>Dynamic favicon in PHP!</title>
- <link rel="shortcut icon" href="icon.php?char=<?php if(isset($_POST['char'])) { echo $_POST['char']; } ?>" />
- <STYLE>
- body, input{
- font-family: Calibri, Arial;
- margin:0px;
- font-size: 19px;
- }
- h1 {
- margin: 0 0 0 20px;
- }
- html, body, #container { height: 100%; }
- body > #container { height: auto; min-height: 100%; }
-
- #header {
- height:50px;
- background-color:#ddd;
- border-bottom:1px solid #aaa;
- width:100%;
- font-size: 15px;
- }
- #footer {
- font-size: 12px;
- clear: both;
- position: relative;
- z-index: 10;
- height: 3em;
- margin-top: -3em;
- text-align:center;
- }
- .demo {
- width:150px;
- height:150px;
- padding:5px;
- background-color:#ff8811;
- position:absolute;
- top:150px;
- left:300px;
- }
- #content { padding-bottom: 3em; margin: 20px;}
- </STYLE>
- </head>
- <body>
- <div id="container">
- <div id="header">
- <h1>Dynamic favicon in PHP</h1>
- </div>
- <div id="content">
- <p><a href="http://www.c-sharpcorner.com">Click here to view this Article</a></p>
- <p>Enter any character in the following textbox and Generate favicon</p>
- <form method="post">
- <label>Enter a Character to add favicon</label>
- <input type="text" name="char" style="width:40px" maxlength="1"/>
- <br/>
- <input type="submit" value="Generate favicon" />
- </form>
- Dynamic Favicon
- <img src="icon.php?char=<?php if(isset($_POST['char'])) { echo $_POST['char']; } ?>" border="0"/>
- <br/>
- </div>
- </div>
- </body>
- </html>
Output
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:
You will see your Favicon in your tab.