SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: Memory Limit question

  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Memory Limit question

    So I'm writing an upload script for images. Once the image is uploaded and copied to a directory, it then resizes the image and makes it less than 500px wide/tall. After getting the code to actually run for the first time it gave me an out of memory error. I found this gem after reading up on imagecreatefromjpeg on the PHP site.

    Code:
    $imageInfo = GetImageSize($imageFilename);
    $memoryNeeded = Round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
    It calculates the amount of memory needed for creating an image-resource out of and image-file. According to this code I need just over 30MB. The image that I uploaded is 1.5MB. Is this right? I mean seriously, 30+MB to process a single image of 1.5MB? I know that other scripts do this with half that, and probably even less. How can I figure out what is using up the memory?

    I'll post some code if needed. Any thoughts? Thanks!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can see the real memory needed by comparing the result of memory_get_usage() immediately before and after the call to imagecreatefrom*()

  3. #3
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1.5mb is just the filesize of the image, not how much space it takes to load into memory.

    To get the actual size of the image, you follow the formula (mostly).
    Width x Height x Bits x Channels = size in bits.

    So lets say we have a 3000x1000px 8-bit RGB jpg (RGB = 3 channels).
    Thats 3000 x 1000 x 8 x 3 = 72,000,000 bits that must be loaded into memory.

    Add into the equation the fact that PHP adds considerable overhead when resizing an image, and you get an even larger amount of memory needed to process the image.
    Image resizing is one of the most CPU-intensive calculations that a PC goes through...

  4. #4
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess what I'm getting at, is I'm trying to figure out if I'm doing something wrong and my code is bloated, or if doing this kind of calculation should really use this much memory. I'll trouble shoot a bit with different size images and stuff. Thanks.

  5. #5
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simshaun View Post
    1.5mb is just the filesize of the image, not how much space it takes to load into memory.

    To get the actual size of the image, you follow the formula (mostly).
    Width x Height x Bits x Channels = size in bits.

    So lets say we have a 3000x1000px 8-bit RGB jpg (RGB = 3 channels).
    Thats 3000 x 1000 x 8 x 3 = 72,000,000 bits that must be loaded into memory.
    Or simplify the formula: width * height * channels. No point involving bits here, unless the point is to confuse.

    3000 x 1000 x 3 (1 byte per each channel) = 9,000,000 bytes ~= 9 MB

    Quote Originally Posted by simshaun View Post
    Add into the equation the fact that PHP adds considerable overhead when resizing an image, and you get an even larger amount of memory needed to process the image.
    Considerable? Arguable. There's not much overhead, really. Input image size + output image size is pretty much what it takes.

    Quote Originally Posted by simshaun View Post
    Image resizing is one of the most CPU-intensive calculations that a PC goes through...
    Hardly. But where CPU fits in the memory topic I don't know.

    And to answer OP's question: It is very much possible that a 1.5MB JPG will take up 30MB of memory uncompressed. It can take up more depending on the level of compression, which JPG is very good at due to its use of lossy compression algorithm.

  6. #6
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Itch View Post
    The image that I uploaded is 1.5MB. Is this right? I mean seriously, 30+MB to process a single image of 1.5MB? I know that other scripts do this with half that, and probably even less. How can I figure out what is using up the memory?
    Its possible those other scripts are using imagemagick or other lib which doesnt rely on PHP's memory allocation to resize an image.

  7. #7
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, here's a simple example of what I'm trying to get at. I have an SMF forum, the PHP memory limit on that server is set to 16MB. I can upload an image that is 1.5MB in that forum as an attachment just fine. It actually resizes it to a thumbnail and shows the original when you click on it. I can then try to upload that same image in my script, and it requires me to up the memory limit to 32MB for the process to finish.

    To me, I'm obviously doing something wrong to make it require this much memory. So, I guess I need to figure out what is using up the memory.

  8. #8
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the SMF forum using imagemagick, or GD?

  9. #9
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GD, as I don't have imagemagick installed.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't know how much memory is really being used by your script nor SMF until you measure it.

  11. #11
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You don't know how much memory is really being used by your script nor SMF until you measure it.
    I get that. My point is, that my script is obviously using more memory, doing the same thing as SMF's upload/resize. Hopefully doing some testing today to see how the memory looks.

  12. #12
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so that was a little eye opening. lol Was able to jump right on and do some testing.

    echo'ing the memory usage before any image creation shows 206012 bytes (Not even a MB yet) and after the image function is processed it shows 31635780 bytes (30MB+).

    I have written my class to do the same thing as "zorroswordsman at gmail dot com" on the imagecopyresampled function page at php dot net. Since I can't post a link yet.

    The only difference is, I'm passing a couple of parameters when creating the object so that all the functions can be parsed at once. Instead of declaring multiple functions.

    Any ideas on what is happening in that class to make it use this much memory?

  13. #13
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Itch View Post
    I get that. My point is, that my script is obviously using more memory
    You don't know that until you measure how much memory they BOTH use.

    hint- memory_limit can be changed using ini_set() in the script at runtime. SMF might be setting thier memory to 4 gigabytes for all you know.

  14. #14
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You were absolutely right! They change the memory to 48MB during the image functions. I guess, what I'm doing isn't so bad after all?

Tags for this Thread

Bookmarks

Posting Permissions

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