Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Fatal error: Allowed memory size exhausted when resizing an image

    Hello,

    I'm trying to resize an uploaded image, but get the following error:

    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 3000 bytes) in /home/httpd/vhosts/crscuderia.com/httpdocs/crs/cms/uploadFile2.php on line xxx

    And here's the script:
    PHP Code:
    ini_set("display_errors","On");
    ini_set("memory_limit","64M");

    if (
    $_FILES['file']) {
        
        
    $uploaddir '/home/img/uploaded_images/large/'
        
        for (
    $i=0$i<count($_FILES['file']['type']);$i++) {
                
    $uploadfile $uploaddir.basename($_FILES['file']['name'][$i]);
                
    $type $_FILES['file']['type'][$i];
                
    $tmp_name $_FILES['file']['tmp_name'][$i];
                
                if (
    $type=="image/jpeg" || $type=="image/pjpeg") {
                    
    //check if the file with the same name exists
                    
    if (!file_exists($uploadfile)) {
                        
    move_uploaded_file($tmp_name$uploadfile);
                        
    //copy and resize
                        //copy file to 'medium' folder
                        
    $copydir1 '/home/img/uploaded_images/medium/';
                        
    $newfile1 $copydir1.basename($uploadfile);
                        
    copy($uploadfile$newfile1) or die('Could not copy file '.basename($uploadfile).' to medium folder');
                        
                        
                        
    //resize new file
                        
    $im = @imagecreatefromjpeg($newfile1);
                        if (
    $im === false) { 
                            die(
    'unable to open image'); 
                        } 
                        
                        
                        list(
    $Width$Height) = getimagesize($newfile1);
                        
    $NewWidth 750;
                        
    $NewHeight 500;
                        
            
                        
    $ResizedImage imagecreatetruecolor($NewWidth$NewHeight);
                        
    imagecopyresampled($ResizedImage$im0000$NewWidth$NewHeight$Width$Height) or die('image not resampled');
                        
                        
                    } else {
                        
    $errorstring .= 'File '.basename($uploadfile).' already exists<br>';
                    }
                } elseif (
    $type == '') {
                    
    $errorstring .= '';
                } else {
                    
    $errorstring .= 'File '.basename($uploadfile).' is not a valid file type<br>';
                }
            }


    //display message to user
    if ($errorstring != '') {
        echo 
    $errorstring;
        echo 
    '<a href="uploadFile1.php">Go back</a>';
    } else {
        echo 
    'File(s) uploaded.<br><a href="uploadFile1.php">Upload more files<a>';

    Can anyone tell me what I'm doing wrong?

    Thanks

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Apparently, you're just running out of memory. I see you've tried to increase the limit to no avail b/c it still needs more. The imagecreatefromjpeg function creates a bitmap image in memory from the jpeg file. For every pixel in the image it will use at least 3 bytes, plus some overhead. Its trying to allocate another 3000 bytes thats not available.... for another pixel. How many pictures are you trying to resize at once? What happens when you try to resize one picture ( < 1-2MB)?
    Last edited by StupidRalph; 01-02-2008 at 12:43 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm trying to resize just one picture, in order to test the script. My test image is a jpeg, 224 KB.
    However, I still get the same error.

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •