I decided to make a free easy to use favicon generating script that uses PHP. The script has a few features and requirements when uploading your image to be converted to a favicon. First the script checks for a few things such as file type, file size, and the specified dimensions. You can create a 16×16 icon or a 32×32 icon.
Click here to check out the demo for this script!
Just select the image file you want converted to a 16×16 or 32×32 icon and hit submit! It’s that easy. Here is how it works:
// bgallz.org – Web coding & design tutorials, scripts, resources and more.
// favicon Generator Script
function generate_favicon(){
// Create favicon.
$postvars = array("image" => trim($_FILES["image"]["name"]),
"image_tmp" => $_FILES["image"]["tmp_name"],
"image_size" => (int)$_FILES["image"]["size"],
"image_dimensions" => (int)$_POST["image_dimensions"]);
$valid_exts = array("jpg","jpeg","gif","png");
$ext = end(explode(".",strtolower(trim($_FILES["image"]["name"]))));
// Check not larger than 175kb.
if($postvars["image_size"] <= 179200){
// Check is valid extension.
if(in_array($ext,$valid_exts)){
if($ext == "jpg" || $ext == "jpeg"){
$image = imagecreatefromjpeg($postvars["image_tmp"]);
}
else if($ext == "gif"){
$image = imagecreatefromgif($postvars["image_tmp"]);
}
else if($ext == "png"){
$image = imagecreatefrompng($postvars["image_tmp"]);
}
list($width,$height) = getimagesize($postvars["image_tmp"]);
$newwidth = $postvars["image_dimensions"];
$newheight = $postvars["image_dimensions"];
$tmp = imagecreatetruecolor($newwidth,$newheight);
// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
$rand = rand(1000,9999);
$filename = "./favicon/".$rand.$postvars["image"];
// Create image file with 100% quality.
imagejpeg($tmp,$filename,100);
// Image created, now rename it to its
$ext_pos = strpos($postvars["image"],"." . $ext);
$strip_ext = substr($postvars["image"],0,$ext_pos);
// Rename image to .ico file
rename($filename,"./favicon/".$strip_ext.".ico");
return "<strong>Icon Preview:</strong><br/>
<img src=\"./favicon/".$strip_ext.".ico\" border=\"0\" title=\"Favicon Image Preview\" style=\"padding: 4px 0px 4px 0px;background-color:#e0e0e0\" /><br/>
Favicon successfully generated. <a href=\"./favicon/".$strip_ext.".ico\" target=\"_blank\" name=\"Download favicon.ico now!\">Click here to download your favicon.</a>";
imagedestroy($image);
imagedestroy($tmp);
} else {
return "File size too large. Max allowed file size is 175kb.";
}
} else {
return "Invalid file type. You must upload an image file. (jpg, jpeg, gif, png).";
}
}
if(isset($_GET["do"])){
if($_GET["do"] == "create"){
if(isset($_POST["submit"])){
$generate_favicon = "<p>".generate_favicon()."</p>";
} else {
$generate_favicon = "";
}
}
}
?>
Then we must include the HTML form that will submit the image and dimensions to PHP:
Image Dimensions: <select name="image_dimensions" style="width: 170px">
<option value="16">16px x 16px</option>
<option value="32">32px x 32px</option>
</select><br/><br/>
<span style="font-size: 14pt">Favicon Image:</span><br/>
<input type="file" name="image" size="40" /><br/><br/>
<input type="submit" name="submit" value="Submit!" style="font: 14pt verdana" />
</form>
Be sure to include the HTML head tags in your HTML pages that use the favicon. These are given on the script’s index and on the demo.