Sunday, July 15, 2012

Create icon php



Create icon php

First create 2 folders called icon and images where uploaded and created images are stored.Then create following php file

Select your image:
Image Size:
                                                        Click here to download
<?php
if($_POST['submit'])
{
    $imgname = $_FILES['icoimage']['name'];
    if($_POST['s1'])
        $size=16;     
    elseif($_POST['s2'])        
        $size=32;
    if(str_replace(" ","",$size)=="")
        $size=32;

    $target_path = "images/";
    $target_path = $target_path . $imgname;
    if(move_uploaded_file($_FILES['icoimage']['tmp_name'], $target_path))
    {
        include_once "create.php";
        makeimage($target_path,$size,$size);
        unset($target_path);unset($size);unset($imgname);  
    }
    else
    {
        echo "Error in converting File";
    }

  
}

?>
<html>

<script language="javascript">
function check()
{
    var file=document.getElementById("icoimage");
    var f=file.value;
    var extn=f.substring(f.lastIndexOf(".")+1,(f.length));
    file=file.value;
    if (extn=="jpg" || extn=="gif" || extn=="jpeg" || extn=="png")
    {
        return true;
    }
    if (f.length==0)
    {
        return false;
    }
  
    else
    {
        return true;
    }
}
</script>
<form enctype="multipart/form-data" method="post"   name="iconconverter" onSubmit="return check();">

<table align=center bgcolor=#858278>
<tr bgcolor=#eeece7>
<td>
Select your image: </td><td><input type="file" id="icoimage" name="icoimage" />
</td>
</tr>
<tr bgcolor=#eeece7>
<td>
Image Size:</td><td>
<select name="size">
<option value="s1">16x16 </option>
<option value="s2">32x32</option>
</select>
</td>
</tr>
<tr bgcolor=#eeece7>
<td align="right" colspan=2>
<input type="submit" value="submit" name="submit"/>
</td>
</tr>
</form>
</table>
</html>
<?php
if($_POST['submit'])
{ ?>
<table align=center width=30%><tr><td align="center">
<a href="download.php"><?php

 $folder = "icon/";
$handle = opendir($folder);
while ($file = readdir($handle))
{
 $files[ ] = $file;
}
closedir($handle);
foreach ($files as $file)
 {
echo "Click here to download<img src='icon/$file'>";
}
?>
</a>
<?php
}
else
{

}
?>
create.php 

<?php
function makeimage($filename, $newwidth, $newheight) {
$image_type = strstr($filename,  '.');
switch($image_type) {
            case '.jpg':
                $source = imagecreatefromjpeg($filename);
                break;
            case '.png':
                $source = imagecreatefrompng($filename);
                break;
            case '.gif':
                $source = imagecreatefromgif($filename);
                break;
            default:
                echo("Error Invalid Image Type");
                die;
                break;
            }
    $file = "created.ico";
    $fullpath = $file;
    list($width,  $height) = getimagesize($filename);
    $thumb = imagecreatetruecolor($newwidth,  $newheight);
    imagecopyresized($thumb,  $source,  0,  0,  0,  0,  $newwidth,  $newheight,  $width,  $height);
   $fullpath="icon/".$fullpath;
    imagepng($thumb,$fullpath);
 }
?>
download.php 

<?php
$file = 'icon/created.ico';
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

No comments:

Post a Comment