error_reporting(E_ALL);
ini_set("display_errors", 1);

put that at the top of your script to see if theres an issue

    any reason you are converting the file from jpeg to another format and then back to jpeg? why not just use [man]gd[/man] and [man]imagecopyresized[/man] and skip the system() calls

      I did and that seems to work fine. Just was puzzled why it worked on one server and not the other with same libs installed.

        it seems to work fine with images of small file size. anything with some size say 1.5 mb or more gets 500 server error. any thoughts

        here is the new code

        // Create an Image from it so we can do the resize
        $src = imagecreatefromjpeg($imgfile);
        
        // Capture the original size of the uploaded image
        list($width,$height)=getimagesize($imgfile);
        $newwidth=400;
        
        $diff = $width / $newwidth;
        
        //$newheight=($height/$width)*100;
        $newheight= $height / $diff;
        
        $tmp=imagecreatetruecolor($newwidth,$newheight);
        
        // this line actually does the image resizing, copying from the original
        // image into the $tmp image
        $newpic = imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
        
        // now write the resized image to disk. I have assumed that you want the
        // resized, uploaded image file to reside in the ./images subdirectory.
        
        
        
        /*== setup final file location and name ==*/
        /*== change spaces to underscores in filename  ==*/
        $new_name = str_replace(" ","_",$old_name);
        
        $newfile = $dir . "/$new_name";
        
        /*== do extra security check to prevent malicious abuse==*/
            if (is_uploaded_file($imgfile))
        {
        
           /*== move file to proper directory ==*/
           if (!imagejpeg($tmp,$newfile,100)) 
           {
              /*== if an error occurs the file could not
                   be written, read or possibly does not exist ==*/
              print "Error Uploading File.";
              exit();
           }
        }
        
        /*== delete the temporary uploaded file ==*/
        
        imagedestroy($src);
        imagedestroy($tmp);
        imagedestroy($newpic);
        unlink($imgfile);
        
        
        

          Try increasing your memory_limit

          A 1.5MB JPEG is about 4.5MB uncompressed, which is how PHP/GD works with it.

            still no help. i increased it to 128M it is running out of memory though. I have destroyed the images after use. Cant figure out why it is running out of mem

              at the top of your script go:

              echo ini_get("memory_limit");
              ini_set("memory_limit","128M");
              echo ini_get("memory_limit");
              

              that will show you your default and verify if its actually being changed

              also a good way to determine how much memory youll need do the following calc:

              width height color_depth

              that is the number of bytes that image will take once loaded into GD

                It shows

                128M 128M

                I am adding the whole script so someone can see if something wrong catches their eye.

                error_reporting(E_ALL);
                ini_set("display_errors", 1); 
                
                
                echo ini_get("memory_limit");
                ini_set("memory_limit","128M");
                echo ini_get("memory_limit");
                
                
                
                foreach ($_FILES["userfile"]["error"] as $key => $error) {
                
                if ($error == UPLOAD_ERR_OK) {
                
                
                // Grabs everything before and after the last dot and turn it into the new filename. 
                
                $imgfile = $_FILES['userfile']['tmp_name'][$key];
                $imgfile_name = $_FILES['userfile']['name'][$key]; 
                $imgfile_size = $_FILES['userfile']['size'][$key]; 
                $imgfile_type = $_FILES['userfile']['type'][$key]; 
                
                
                $dir="/home/u1/tomthumb/html/gallery/";
                srand((double)microtime()*1000000);  
                $unique_str = md5(rand(0,9999999)); $p = strrpos($imgfile_name, "." ); $old_name = substr($imgfile_name , 0 , $p ) . $unique_str . substr($imgfile_name , $p , ( strlen($imgfile_name ) - $p ) ); /*== upload directory where the file will be stored relative to where script is run ==*/ $uploaddir = "/gallery"; /*== get file extension (fn at bottom of script) ==*/ $pext = getFileExtension($imgfile_name); $pext = strtolower($pext); //-- RE-SIZING UPLOADED IMAGE /*== only resize if the image is larger than 50 x 50 ==*/ // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($imgfile); // Capture the original size of the uploaded image list($width,$height)=getimagesize($imgfile); $newwidth=400; $diff = $width / $newwidth; //$newheight=($height/$width)*100; $newheight= $height / $diff; $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // now write the resized image to disk. /*== setup final file location and name ==*/ /*== change spaces to underscores in filename ==*/ $new_name = str_replace(" ","_",$old_name); $newfile = $dir . "/$new_name"; if (is_uploaded_file($imgfile)) { /*== move file to proper directory ==*/ if (!copy($imgfile,$newfile)) { /*== if an error occurs the file could not be written, read or possibly does not exist ==*/ print "Error Uploading File."; exit(); } } /*== delete the temporary uploaded file ==*/ imagedestroy($tmp); imagedestroy($src); unlink($imgfile); } } /*== FUNCTIONS ==*/ function getFileExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }

                  whats the exact error output you are getting to the screen? also, the file you are uploading, can you please indicate its dimensions and/or file size?

                    128M128M

                    Fatal error: Out of memory (allocated 36175872) (tried to allocate 13056 bytes) in /home/u1/tomthumb/html/admin/add_image.php on line 49

                    3264 X 2448
                    1.95MB

                      3264 2448 32 = 255688704 bytes

                      255688704 bytes = 243.84375 megabytes
                      (using 1024 notation)

                      your best bet is to dynamically raise/lower the set limit based on the image, however you need to make sure that you don't go beyond the memory in the machine or allocated to you, so before you actually set it, check that its not greater than what you have physically or allocated

                        or dont try to upload huge images. It is not a big deal to have my client change resolution on camera.

                        Thanks for the help

                          no problem...

                          as a note to your solution, you can't expect users to blindly follow your advice or keep the camera settings so what i would suggest is:

                          figure out the max memory you want to use... then based on the image the user wants to upload determine how much memory it will take to re-size it... If it's over your limit output a friendly message that the native image is too large and to use either a desktop app to re-size it to X by Y or point to one of the many online sites that let you re-size images...

                          it's always nice to have options 🙂

                            Write a Reply...