user31929
user31929

Reputation: 1145

How i can know if an Image will cause a php memory_limit error?

During the resize of an image I receive the error "Out of Memory" of PHP. I can solve it by set a greater value in my php.ini but what happens if I can't modify my php.ini (and also I can't set this value runtime with PHP) due webhosting security policy?

I upload the image using a normal post with $_Files, my memory_limit is 32 mb. How can I calculate if an image will cause this error during the resize? The uploaded photos may have different formats and weight; I’m trying to resize to an unique width of 820px.

EDIT

I have found this site http://www.dotsamazing.com/en/labs/phpmemorylimit Maybe i have to replicate this calculation but it seems very hard to do.

Upvotes: 2

Views: 81

Answers (1)

Danoweb
Danoweb

Reputation: 208

It looks like you would want to get the image size before you attempted to resize it.

For example once you have a temporary path for the file on the server, pass it to the function:

$image_size = getimagesize('/path/to/image');

This will provide you will lots of info about the image, including it's size.

Once you have tthis, then subtract your memory_limit, which you can get the value of with:

$my_memory_limit = ini_get('memory_limit');

So your available memory becomes

$memory_available = $my_memory_limit - $size;

At which point you can make the decision of whether you have the memory to resize it (depending on how memory costly your re size process is, or if you kick it back to the user and tell them to use a smaller image.

Upvotes: 2

Related Questions