Error Creating an Image from an Uploaded File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ctiggerf

    #1

    Error Creating an Image from an Uploaded File

    I was hopeing someone could help me out here. Been stumped on this
    one all day.

    This function
    1. Checks uploaded files.
    2. Creates two resized images from each (a full size, and a
    thumbnail).
    3. Returns an array with a bool (false if the upload failed), and an
    error message.

    I am having trouble createing a copy of the image from the functions
    imagecreatefrom *

    Here is the function:

    <?php
    function process_images( ) {
    //options
    $max_bytes = 65536; // max file size
    $max_h = 425; // maximum hieght for images
    $max_w = 425; // maximum width for images
    $t_h = 125; // max hieght for thumbnails
    $t_w = 50; // max width for thumbnails
    $max = 2; // max number of images

    $i = 1;
    while((list($k, $err) = each($_FILES['image']['error'])) and $i<=
    $max) {
    //check for an error
    if($err != UPLOAD_ERR_OK) { return array(false,"Th ere was an
    error uploading Image #$i."); }
    //TODO: Need better error message for errors here

    //get file information
    $name = $_FILES['image']['name'][$k];
    $type = $_FILES['image']['type'][$k];
    $temp = $_FILES['image']['tmp_name'][$k];
    $size = $_FILES['image']['size'][$k];

    //check that this file is an uploaded file
    if(!is_uploaded _file($temp)) { return array(false,"Th ere was
    an error uploading Image #$i."); }
    //check file size
    if($size $max_bytes) { return array(false, "Image #$i
    exceeds maximum size"); }

    //check type and create a new image based off it
    switch($type) {
    //jpeg
    case "image/pjpeg" :
    case "image/jpeg" :
    case "image/jpg" : $new_img =
    imagecreatefrom jpeg($temp); break;

    //png
    case "image/x-png" :
    case "image/png" : $new_img = imagecreatefrom png($temp);
    break;

    //gif
    case "image/gif" : $new_img = imagecreatefrom gif($temp);
    break;

    //unrecognized format
    default : return array(false, "Image #$i must be in jpeg,
    gif, or png format.");
    }

    #no matter what image I put in here .. it always fails here
    if(!$new_image) { return array(false, "Error Creating Image #
    $i from $temp."); }

    //get the original image size
    list($w, $h) = getimagesize($t emp);

    //caluclate scale percentage
    $perc = min($max_w/$w, $max_h/$h);
    $thumb_p = min($t_w/$w, $t_h/$h);
    //calucalte new dimensions
    $new_w = round($perc * $w);
    $new_h = round($perc * $h);
    $thumb_w = round($thumb_p * $w);
    $thumb_h = round($thumb_p * $h);

    //create a new image with those dimensions
    $res_img = imagecreatetrue color($new_w, $new_h);
    $thumb_img = imagecreatetrue color($thumb_w, $thumb_h);
    //resize now
    imagecopyresamp led($res_img, $new_img, 0,0,0,0, $new_w,
    $new_h, $w,$h);
    imagecopyresamp led($thumb_img, $new_img, 0,0,0,0, $thumb_w,
    $thumb_h, $w,$h);
    //save the images
    $new_name = time();
    imagejpeg($res_ img, "img/".$new_name.".j pg");
    imagejpeg($thum b_img, "img/thumbs/".$new_name.".j pg");
    //free memory
    imagedestroy($r es_img);
    imagedestroy($t humb_img);
    imagedestroy($n ew_image);

    $i++;
    }

    return array(true,'');
    }
    ?>

    The functions: imagecreatefrom jpeg(), imagecreatefrom png(), and
    imagecreatefrom gif() all seem to be returning false. Anyone have any
    suggestions?

    Thanks,
    Chris F.

  • P Pulkkinen

    #2
    Re: Error Creating an Image from an Uploaded File


    While debugging, you should also print $type, since error might just lie
    there.


    Comment

    • ctiggerf

      #3
      Re: Error Creating an Image from an Uploaded File

      On Feb 5, 2:44 pm, "ctiggerf" <ctigg...@gmail .comwrote:
      I was hopeing someone could help me out here. Been stumped on this
      one all day.
      >
      This function
      1. Checks uploaded files.
      2. Creates two resized images from each (a full size, and a
      thumbnail).
      3. Returns an array with a bool (false if the upload failed), and an
      error message.
      >
      I am having trouble createing a copy of the image from the functions
      imagecreatefrom *
      >
      Here is the function:
      >
      <?php
      function process_images( ) {
      //options
      $max_bytes = 65536; // max file size
      $max_h = 425; // maximum hieght for images
      $max_w = 425; // maximum width for images
      $t_h = 125; // max hieght for thumbnails
      $t_w = 50; // max width for thumbnails
      $max = 2; // max number of images
      >
      $i = 1;
      while((list($k, $err) = each($_FILES['image']['error'])) and $i<=
      $max) {
      //check for an error
      if($err != UPLOAD_ERR_OK) { return array(false,"Th ere was an
      error uploading Image #$i."); }
      //TODO: Need better error message for errors here
      >
      //get file information
      $name = $_FILES['image']['name'][$k];
      $type = $_FILES['image']['type'][$k];
      $temp = $_FILES['image']['tmp_name'][$k];
      $size = $_FILES['image']['size'][$k];
      >
      //check that this file is an uploaded file
      if(!is_uploaded _file($temp)) { return array(false,"Th ere was
      an error uploading Image #$i."); }
      //check file size
      if($size $max_bytes) { return array(false, "Image #$i
      exceeds maximum size"); }
      >
      //check type and create a new image based off it
      switch($type) {
      //jpeg
      case "image/pjpeg" :
      case "image/jpeg" :
      case "image/jpg" : $new_img =
      imagecreatefrom jpeg($temp); break;
      >
      //png
      case "image/x-png" :
      case "image/png" : $new_img = imagecreatefrom png($temp);
      break;
      >
      //gif
      case "image/gif" : $new_img = imagecreatefrom gif($temp);
      break;
      >
      //unrecognized format
      default : return array(false, "Image #$i must be in jpeg,
      gif, or png format.");
      }
      >
      #no matter what image I put in here .. it always fails here
      if(!$new_image) { return array(false, "Error Creating Image #
      $i from $temp."); }
      >
      //get the original image size
      list($w, $h) = getimagesize($t emp);
      >
      //caluclate scale percentage
      $perc = min($max_w/$w, $max_h/$h);
      $thumb_p = min($t_w/$w, $t_h/$h);
      //calucalte new dimensions
      $new_w = round($perc * $w);
      $new_h = round($perc * $h);
      $thumb_w = round($thumb_p * $w);
      $thumb_h = round($thumb_p * $h);
      >
      //create a new image with those dimensions
      $res_img = imagecreatetrue color($new_w, $new_h);
      $thumb_img = imagecreatetrue color($thumb_w, $thumb_h);
      //resize now
      imagecopyresamp led($res_img, $new_img, 0,0,0,0, $new_w,
      $new_h, $w,$h);
      imagecopyresamp led($thumb_img, $new_img, 0,0,0,0, $thumb_w,
      $thumb_h, $w,$h);
      //save the images
      $new_name = time();
      imagejpeg($res_ img, "img/".$new_name.".j pg");
      imagejpeg($thum b_img, "img/thumbs/".$new_name.".j pg");
      //free memory
      imagedestroy($r es_img);
      imagedestroy($t humb_img);
      imagedestroy($n ew_image);
      >
      $i++;
      }
      >
      return array(true,''); }
      >
      ?>
      >
      The functions: imagecreatefrom jpeg(), imagecreatefrom png(), and
      imagecreatefrom gif() all seem to be returning false. Anyone have any
      suggestions?
      >
      Thanks,
      Chris F.


      I did forget to mention that my php is compiled with GD support
      sufficient enough to use the functions and I am using PHP Version
      4.3.11 ...

      GD Support enabled
      GD Version bundled (2.0.28 compatible)
      FreeType Support enabled
      FreeType Linkage with freetype
      GIF Read Support enabled
      GIF Create Support enabled
      JPG Support enabled
      PNG Support enabled
      WBMP Support enabled
      XBM Support enabled

      Comment

      • ctiggerf

        #4
        Re: Error Creating an Image from an Uploaded File

        On Feb 5, 2:58 pm, "P Pulkkinen"
        <perttu.POISTAT AMA.pulkki...@P OISTATAMA.elisa net.fiwrote:
        While debugging, you should also print $type, since error might just lie
        there.
        I went ahead and added that debugging info and the type seems to be
        reporting back correctly for all the types I tried.

        Comment

        • P Pulkkinen

          #5
          Re: Error Creating an Image from an Uploaded File


          "ctiggerf" <ctiggerf@gmail .comkirjoitti
          viestissä:11707 05840.807626.28 1870@q2g2000cwa .googlegroups.c om...
          On Feb 5, 2:58 pm, "P Pulkkinen"
          <perttu.POISTAT AMA.pulkki...@P OISTATAMA.elisa net.fiwrote:
          >While debugging, you should also print $type, since error might just lie
          >there.
          I went ahead and added that debugging info and the type seems to be
          reporting back correctly for all the types I tried.
          What's your error level reporting level, since imagecreatefrom something
          functions echo error message when they fail.


          Comment

          • P Pulkkinen

            #6
            Re: Error Creating an Image from an Uploaded File

            What's your error level reporting level, since imagecreatefrom something
            functions echo error message when they fail.
            you could put to the beginning of script:

            error_reporting (E_ALL);


            Comment

            • ctiggerf

              #7
              Re: Error Creating an Image from an Uploaded File

              On Feb 5, 3:37 pm, "P Pulkkinen"
              <perttu.POISTAT AMA.pulkki...@P OISTATAMA.elisa net.fiwrote:
              What's your error level reporting level, since imagecreatefrom something
              functions echo error message when they fail.
              >
              you could put to the beginning of script:
              >
              error_reporting (E_ALL);
              Thanks for your quick replies.
              I turned the error reporting on, (it wasn't set high enough to see the
              notices) and it is reporting:

              Notice: Undefined variable: new_image in /home/content/o/t/b/
              otbadmin356/html/order.php on line 365

              Which corresponds to the line after the switch statement:

              if(!$new_image) { return array(false, "Error Creating Image #$i from
              $temp - $type."); }

              So I put some echos in to see if I was ever getting inside the switch
              statement, and I am. However I do not see any output from the
              imagecreatefrom functions.

              Comment

              • Rik

                #8
                Re: Error Creating an Image from an Uploaded File

                ctiggerf <ctiggerf@gmail .comwrote:
                Notice: Undefined variable: new_image in /home/content/o/t/b/
                otbadmin356/html/order.php on line 365
                $new_img != $new_image

                --
                Rik Wasmus

                Comment

                • ctiggerf

                  #9
                  Re: Error Creating an Image from an Uploaded File

                  On Feb 5, 4:40 pm, Rik <luiheidsgoe... @hotmail.comwro te:
                  ctiggerf <ctigg...@gmail .comwrote:
                  Notice: Undefined variable: new_image in /home/content/o/t/b/
                  otbadmin356/html/order.php on line 365
                  >
                  $new_img != $new_image
                  >
                  --
                  Rik Wasmus
                  DOH!!

                  Nothing like spending a whole day looking for a typo. I love
                  programming.

                  Thanks for the help, it works great now.
                  --Chris

                  Comment

                  Working...