A fail-safe way to prevent GD image library from running out of memory? (PHP)

怎甘沉沦 提交于 2019-12-30 03:24:07

问题


Is there a way to prevent the PHP GD image library from running out of memory? If too large an image is uploaded, GD tends to run out of memory, terminating the script. I'd like it to throw a catchable exception or something to that extend, but alas it doesn't.

Right now I'm using a cobbled-together script that first issues an ini_set('memory_limit', '128M'), if that works I'm usually all set. Depending on the server configuration though that may not be possible, so I'm falling back on an algorithm that tries to estimate the amount of memory needed (taking resolution, color depth, channels and a fudge factor into account), then compares it to memory_get_usage() if the function exists, otherwise does a rough estimate.

The whole thing works so far, but it's far from elegant and will fail in some edge cases, I'm sure. Is there any better way to do this, i.e. have GD fail gracefully if it has to, instead of grinding everything to a halt?


回答1:


Buy more memory! :-P

Seriously though, it is impossible to handle being out of memory because any action you take would require more memory.

Your best bet is to limit the size of image being uploaded based on the current memory settings.




回答2:


After you create an image.

imagepng($image);
imagedestroy($image);

will remove the memory problem




回答3:


There is another way to do it, but it can be time consuming, as certain parts of the image editing process would be repeated a number of times, but you can set the memory limit to your estimated value, then try to process the image, if it fails catch the exception, increase the memory limit, then process the image again - repeating this until you succeed or reach a certain memory limit - at which point you'd throw an error message to the user explaining that their image is too big to be used.

Edit: To catch the out-of-memory error, you could use this solution: http://au2.php.net/set_error_handler#35622




回答4:


Do some tests to check how much memory each gd function need.

  • imagecreatetruecolor seems to need width*height*5 bytes.

  • imagepng seems to need width*height*4 bytes.




回答5:


Your best bet is to stop trying to figure out how much ram it will need, and just max it out at the outset - if you have 4 GB available, tell the image script to use between 2 and 4 GB or so, and when the script ends, have it go back to normal, that will cover off all potentially fatal situations. That's the only "Fail-safe" way I can think of anyway ...




回答6:


To catch PHP's fatal errors, like "Out of memory" or "PHP Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate … bytes) in", see here : http://php.net/manual/en/function.set-error-handler.php#88401



来源:https://stackoverflow.com/questions/1117344/a-fail-safe-way-to-prevent-gd-image-library-from-running-out-of-memory-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!