1. ## 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. You can see the real memory needed by comparing the result of memory_get_usage() immediately before and after the call to imagecreatefrom*()

3. 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. 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. Originally Posted by simshaun
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

Originally Posted by simshaun
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.

Originally Posted by simshaun
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. Originally Posted by Itch
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. 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. Is the SMF forum using imagemagick, or GD?

9. GD, as I don't have imagemagick installed.

10. You don't know how much memory is really being used by your script nor SMF until you measure it.

11. Originally Posted by crmalibu
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. 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. Originally Posted by Itch
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. 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?