I'm having some problems with resizing and uploading an image. The normal upload works fine, the image is uploaded and the entry for it is inserted into the database, but when the checkbox on the form is checked to resize the image, the entry is inserted but the image is nowhere to be found.

if (isset($_POST['addmood'])) {
   $moods = str_replace(" ", "_", $_POST['moods']);
   $size = $_POST['size'];
   $description = $_POST['description'];
   if ($size == "") { echo '<p class="warning">Error: You must enter a size</p>'; die(); }
   if ($description == "") { echo '<p class="warning">Error: You must enter a description</p>'; die(); }
   if ($moods == "") { echo '<p class="warning">Error: You must enter a name</p>'; die(); }
   if ($_POST['filename'] != "") {
     $filename = $_POST['filename'];
   } elseif ($_FILES['imgfile'] != "") {
     if(isset($_POST['overwrite'])) { $overwrite = "yes"; } else { $overwrite = "no"; }
     $allowed_ext = "jpg,gif,png,jpeg";
     $folder = "images/";
     chmod($folder, 0777);
     $match = ""; // For security purposes
     $filesize = $_FILES['imgfile']['size'];
     $filename = strtolower($_FILES['imgfile']['name']);
     $filename = str_replace(" ", "_", $filename);
     $tempname = $_FILES['imgfile']['tmp_name'];
     if(!$filename || $filename=="") {
        $error = "- No file selected for upload.<br>";
     } elseif (file_exists($folder.$filename) && $overwrite=="no") {
        $error = "File already exists: ".$filename."<br>";
     }
     if($filesize < 1){
        $error .= "File is empty.<br>";
     } elseif ($filesize > 1000000) {
        $error .= "File size is too big.<br>";
     }
     $file_ext = preg_split("/./",$filename);
     if(($file_ext[1] == "png") || ($file_ext[1] == "gif") || ($file_ext[1] == "jpeg") || ($file_ext[1] == "jpg")) {
       $match = "1"; 
     }
     if(!isset($match)){
        $error .= "File type isn't allowed: ".$filename."<br>";
     }

 if(isset($error)) {
    echo "Error trying to upload file:<br>" . $error;
 } else {
         if(isset($_POST['resizesmiley'])) {
             $newname = $folder.'small_'.$filename;
             if ($_FILES['imgfile']['type'] == "image/gif"){
                 $im = ImageCreateFromGIF($tempname);
                 $width = ImageSx($im);
                 $height = ImageSy($im);
                 $aspect = $width / 20;
                 $n_width = 20;
                 $n_height = $height / $aspect;
                 $newimage = imagecreatetruecolor($n_width,$n_height);
                 imageCopyResized($newimage,$im,0,0,0,0,20,20,$width,$height);
//                     imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                 if (function_exists("imagegif")) {
                     Header("Content-type: image/gif");
                     ImageGIF($newimage,$newname);
                 } elseif (function_exists("imagepng")) {
                     Header("Content-type: image/png");
                     ImagePNG($newimage,$newname);
                 } elseif (function_exists("imagejpeg")) {
                     Header("Content-type: image/jpeg");
                     ImageJPEG($newimage,$newname);
                 }
             }
             if($_FILES['imgfile']['type'] == "image/pjpeg"){
                $im = ImageCreateFromJPEG($tempname);
                $newimage = imagecreatetruecolor($n_width,$n_height);
                imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
                ImageJpeg($newimage,$newname);
             }
             $filename = $sigfilesurl . $newname;
         } else {
            if(move_uploaded_file($tempname, $folder.$filename)) { $filename = $smileypath.$filename; }
            else { echo "Error! File size might exceed upload limit of server. Try again."; die(); }
         }

}
   }
   elseif (($_FILES['imgfile'] == "") && ($_POST['filename'] == "")) { echo '<p class="warning">Error: You must enter a file to upload</p>'; die(); }

   $sql = "INSERT INTO mood (moods,filename,size,description) VALUES ('$moods','$filename','$size','$description')";
   mysql_query($sql);
   echo "<p>Added mood: " . $description . "</p>";
   ?>
   <p align="center">Reloading in 2 seconds...</p><script language="javascript" type="text/javascript">setTimeout("location.href='sigadmin.php'",2000)</script></body></html>
   <?php
   mysql_close();
   die();
}

The problem isn't with permissions because the standard upload works, it's just the resize causing probs.

    1. Do you seriously want to make 3 different versions of the same image?

      if (function_exists("imagegif")) {
      Header("Content-type: image/gif");
      ImageGIF($newimage,$newname);
      } elseif (function_exists("imagepng")) {
      Header("Content-type: image/png");
      ImagePNG($newimage,$newname);
      } elseif (function_exists("imagejpeg")) {
      Header("Content-type: image/jpeg");
      ImageJPEG($newimage,$newname);
      }

    2. What about the extension?

    3. Have you browsed via FTP to see if there are extension-less files?

    ~Brett

      1. They're elseif statements. If some part of the GD library is missing/disabled, then the script moves onto the next image type until it's tried them all. Only one image should be generated.
      2. In the other part of the upload script, the filename and extension are taken by "$_FILES['imgfile']['name']);", on line 17. This works perfectly for the normal upload, it's just the resizing that's causing problems.
      3. The file uploads and is added to the db for a normal upload. For a resize, the location where the file should be is added to the db (/images/small_$filename), but the file doesn't move to there. The browser uploads something, but it seems to disappear when the script tries to resize it.

        What are you trying to do here? Move the original or the small one? The current code shows you moving the original image... but not doing anything with the small one....

        if(move_uploaded_file($tempname, $folder.$filename)) { $filename = $smileypath.$filename; }

        ~Brett

          I'm trying to output the smaller files once theybeen resized with ImagePNG and ImageJPEG, using

          ImageJpeg($newimage,$newname);
          

          At http://uk.php.net/manual/en/function.imagejpeg.php the PHP docs say you can output to a file. I've worked with outputting to the browser, but never to a file. I think I'm using the right syntax. 😕

            Sending the header is not required.... try not sending the headers....

            ~Brett

              Write a Reply...