Home » Scripts, Tutorials

PHP Favicon Generator Script

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, 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!

1

Popularity: 33% [?]

Share/Bookmark this!

8 Comments

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on July 24, 2010 at 12:12 PM and filed under Scripts, Tutorials category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors