PHP Favicon Generator Script
July 24, 2010 # 12:12 PM # Scripts, Tutorials # 8 CommentsI 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, if the directory is writable, 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:
<?php
// bgallz.org - Web coding and 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"]))));
$directory = "favicon/"; // Directory to save favicons. Include trailing slash.
// 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 = "./".$directory.$rand.$postvars["image"];
// Create image file with 100% quality.
if(is_writable($directory)){
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:</stong>
<p><img title="\Favicon Preview\" src="\".$filename."\" border="\0\" alt=\"Favicon\" /></p>
<p>Favicon successfully generated. <a name="\Download Favicon\" href=\"".$filename."\" target=\"_blank\">Click here to download your favicon.</a></p>
<p><strong>If favicon doesn't download, right click the link above. Click SAVE AS to download .ico.</strong>";
} else {
return "The directory: \"".$directory."\" is not writable.";
}
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 = "".generate_favicon()."";
$generate_favicon .= "Place your download instructions and anything else you want here to follow the download link after upload.";
} else {
$generate_favicon = "";
}
}
}
?>Then we must include the HTML form that will submit the image and dimensions to PHP:
<form action="index.php?do=create" enctype="multipart/form-data" method="post">Image Dimensions:<br />
<select style="width: 170px;" name="image_dimensions">
<option selected="selected" value="16">16px x 16px</option>
<option value="32">32px x 32px</option>
</select>
<p><span style="font-size: 14pt;">Favicon Image:</span></p>
<input name="image" size="40" type="file" />
<input style="font: 14pt verdana;" name="submit" type="submit" value="Submit!" />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.
Click here to download the script!
Popularity: 33% [?]

Subscribe RSS
Comment RSS










Will be using it on my website improvement site
Thanks
I have a few questions can you answer them?
Absolutely, you can email me at: support@bgallz.org anytime.
Its amazing, loing at the time and effort you put into your blog and detailed information you provide. I’ll bomark your blog and visit it weekly for your new posts o
This PHP script is allowing to create own site. so try this trial
I’ve been struggling to sort out a favicon for my client on his site theme. But inow i’m using Favicon generator and it’s great!.It helps me a lot.
wow, awesome post, I was wondering how to cure acne naturally. and found your blog by bing, many userful stuff here, now i have got some idea. bookmarked and also signed up your rss. keep us updated.
Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!