Web Analytics Made Easy -
StatCounter imagecreatefromjpeg() out of memory error - CodingForum

Announcement

Collapse
No announcement yet.

imagecreatefromjpeg() out of memory error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • imagecreatefromjpeg() out of memory error

    Fatal error: Out of memory (allocated 46661632) (tried to allocate 24000 bytes)

    My problem comes when i use imagecreatefromjpeg() on a large file... smaller files seem to work fine. Only if the file is around 5mb or bigger will this script fail. I set my php.ini memory_limit to 256mb hoping to solve the problem (did not work). I have also tried to use some scripts that detect how much memory is needed, then set it (again, did not work).

    i also do not understand how it can allocate 46661632 bytes when it "tried" to allocate 24000 bytes. maybe i just don't know what i'm doing. help would be appreciated.

    Here is my function to resize an image to a certain width...
    PHP Code:
    function setImageWidth($image_path,$thumb_path,$image_name,$thumb_width)
            {
                
    $src_img imagecreatefromjpeg("$image_path/$image_name");
                
    $origw=imagesx($src_img);
                
    $origh=imagesy($src_img);
                if(
    $origw <= $thumb_width)
                    {}
                else
                    {
                        
    $new_w $thumb_width;
                        
    $diff=$origw/$new_w;
                        
    $new_h=$origh $diff;
                        
    $dst_img imagecreatetruecolor($new_w,$new_h);
                        
    imagefill($dst_img,0,0,imagecolorallocate($dst_img255255255));
                        
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
                        
    imagejpeg($dst_img"$thumb_path/$image_name"100);

                        return 
    true;
                    }
            } 
    phpinfo()
    memory_limit: 256M

    GD Support: enabled
    GD Version: bundled (2.0.34 compatible)
    FreeType Support: enabled
    FreeType Linkage: with freetype
    FreeType Version: 2.2.1
    GIF Read Support: enabled
    GIF Create Support: enabled
    JPG Support: enabled
    PNG Support: enabled
    WBMP Support: enabled
    XPM Support: enabled
    XBM Support: enabled

  • #2
    That will tell you how much memory is needed

    PHP Code:
    <?php
    $imageInfo 
    GetImageSize($imageFilename);
    $memoryNeeded Round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / Pow(216)) * 1.65);
    ?>
    I havn't a 5MB image file to test it on. But don't assume 256MB is enough
    You can not say you know how to do something, until you can teach it to someone else.

    Comment


    • #3
      i ran the script on one of the files i was trying to resize

      PHP Code:
      echo $memoryNeeded// 178308134 
      that would be 178mb needed right? its set for 256mb...

      Comment


      • #4
        Did you restart apache yeah?

        what do these functions return

        Memory_Get_Usage() and Get_CFG_Var('memory_limit')
        You can not say you know how to do something, until you can teach it to someone else.

        Comment


        • #5
          not really sure how the Memory_Get_Usage() is supposed to be used... i just made a new file to get the results... should it be in my function to read it as its in action?

          results:
          PHP Code:
          echo Memory_Get_Usage(); // 55752

          echo Get_CFG_Var('memory_limit'); // 512M 

          Comment


          • #6
            PHP Code:
            Memory_Get_Usage(); //returns amount of memory being used by your script. 
            Get_CFG_Var('memory_limit'); //Gets the value of a PHP configuration option for memory_limit 
            Though you might need to consider how much memory other users on the system are using.
            You can not say you know how to do something, until you can teach it to someone else.

            Comment


            • #7
              Originally posted by konx80 View Post
              not really sure how the Memory_Get_Usage() is supposed to be used... i just made a new file to get the results... should it be in my function to read it as its in action?

              results:
              PHP Code:
              echo Memory_Get_Usage(); // 55752

              echo Get_CFG_Var('memory_limit'); // 512M 
              It will tell you how much memory that script is using. But thinking about it the Fatal Error may not help matters.
              You can not say you know how to do something, until you can teach it to someone else.

              Comment


              • #8
                Its weird you got 512M of mem available and your file requires 170MB to run. Unless the rest of your script and other users of your site are using 342MB then it should be fine. I am trying to work out how i can determine how much memory other users are using.
                You can not say you know how to do something, until you can teach it to someone else.

                Comment


                • #9
                  Sorry i couldn't help more but i just noticed the time and really have to go to bed.
                  You can not say you know how to do something, until you can teach it to someone else.

                  Comment


                  • #10
                    cool man... if nothing else, i guess i will be stuck with upload <5mb files...

                    Comment


                    • #11
                      very much appreciated... ill keep researching...

                      Comment

                      Working...
                      X