I will start off by saying very quickly I am completely new to PHP. I found a fairly nice PHP script to begin learning from: looking at the fuctions, returns and so forth. Without further ado, I have seemingly come very close to executing a "favicon generator script" at http://www.cornerlinks.com but Im not getting a proper return.
Here is the PHP code I am using which in my "public_html" folder is called "script.php"

<?php
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:</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>";

} 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 = "<p>".generate_favicon()."</p>";
} else {
$generate_favicon = "";
}
}
}

?>

Next, is the the HTML portion which is as follows:

 <form action="index.php?do=create" method="post" enctype="multipart/form-data">
            Image Dimensions: <select name="image_dimensions" style="width: 170px">
            	<option value="16">16px &nbsp;x&nbsp; 16px</option>
                <option value="32">32px &nbsp;x&nbsp; 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>

So in my "public_html" there is the following files: index.html (just above), script.php (which is the code above html), style.css, "favicon" folder, "image" folder. That's it. I have seemingly executed everything but the return accordingly. Please help with any suggestions. Thank you so much and Merry Christmas

    Please help with this script! Thanks so Much

      look at your form action

        I did look at the form action in the html section where the form action begins and realize that i have "index.php" where it should have been "script.php" but that still didnt give me a return. It is not returning the error screen as it was, but it still does not seem to be uploading the image and returning a converted image, or a download link. All suggestions are welcome. And thank very much for the help.

          This script is now executing perfect; however, it is returning the image as "....ico.jpeg" so it is a fake script apparently. Unless by mistake, the provider whose made this script missed something in the PHP which results in the image not saving as a true .ico file but into a .ico.jpeg file. If I may please recive help getting this script to executs the return as a .ICO that would be great.

          Sincerely,

          Philip

          <?php
          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:</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>";
          
          } 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 = "<p>".generate_favicon()."</p>";
          } else {
          $generate_favicon = "";
          }
          }
          }
          
          ?> 
           <form action="index.php?do=create" method="post" enctype="multipart/form-data">
                      Image Dimensions: <select name="image_dimensions" style="width: 170px">
                      	<option value="16">16px &nbsp;x&nbsp; 16px</option>
                          <option value="32">32px &nbsp;x&nbsp; 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>

            This pretty much spells it out right here:

            Munificent wrote:
            imagejpeg($tmp,$filename,100); 

            The standard GD library in PHP can't output ICO icon files, so the script simply pretends that renaming any JPEG file to have a .ico file means you've actually created an ICO icon file.

              I will just keep studying GD and image functioning through php. I'll eventually learn the convert process from a standard image to ICON.

              Thanks again for you help.

                you could study the phone book for a million years and never learn how to bake a cake. The gd library does not support ico, you will need some third party software.

                  Write a Reply...