tags:

views:

157

answers:

1

I am working on an image upload script and ran into this problem. Using ImageMagick I would run out of time resizing and uploading images so I inserted these two lines:

set_time_limit(120);
ini_set('max_input_time', 120);

Now however, the script never ends. It is continually running the page even though it uploads, resizes, and inserts to the database fine. Do these conflict with each other in some way? It even goes past the 120 seconds I set as the time limit.

Edit: This is the full image editing portion of the script, it works with say 1 or 2 images but when I put in more (thereby making it take longer) it does not.

set_time_limit(120);
ini_set('max_input_time', 120);
$resource = NewMagickWand(); 
MagickReadImage($resource,$image); 
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500'); 
MagickWriteImage($resource, $image);
DestroyMagickWand($resource);

This is the code I use to read all the images that are to be uploaded: (reSizePic is the function that calls the code above)

$numberImages = count($_FILES['galFile']['name'])-1;

    for($i=1;$i<=$numberImages;$i++)
    {
    $imageName = $_FILES['galFile']['name'][$i];
       $imageType = $_FILES['galFile']['type'][$i];
       $imageSize = $_FILES['galFile']['size'][$i];
       $imageTemp = $_FILES['galFile']['tmp_name'][$i];
       $imageError = $_FILES['galFile']['error'][$i];

       //Make sure it is an image
       if(in_array(end(explode(".", $imageName)), $allowed))
       {
         //Where to upload image to
         $uploadFile = $uploadDir . $imageName;
         if (file_exists($uploadFile))
      {
       //What to do if file already exists
       //Append random number to the end
       $front = explode(".", $imageName);
       $randomNum = rand(1,100);
       $front[0] = $front[0].$randomNum;
       $imageName = $front[0].".".$front[1];
       $uploadFile = $uploadDir . $imageName;
      }
             if(move_uploaded_file($imageTemp,$uploadFile))
             {
             //Add $imageName to DB
              $query = "INSERT INTO galleryImages VALUES(\"0\",\"$lastInsert\",\"$imageName\",\"$i\")";
                mysql_query($query);
                reSizePic($uploadFile);
             }
       }



Levi

+2  A: 

You should make sure to only call set_time_limit() once, as each time you call it, the timer will be reset.

For instance, if you call set_time_limit(30) 10 seconds in to your script, your script will run for a total of 40 seconds. So setting it on each resizePic() call is a bad idea.

zombat
If I take them out of the loop I get an Internal Server Error after it uploads like 10 images out of 15.
Levi
try to put it at the beginning of the script, and set the timeout to 0 (infinite)
Aziz
Didn't work, but what I did notice that I didn't before was that only some of the pictures were being resized. They get uploaded and then resized so I need to figure out how to resize then upload them. I think the server has a time limit I can't override.
Levi
I don't think it is possible resize the image before it is uploaded. Try to show some output in the resize() function in order to see which function is the one that stops.
Aziz