Forums | Mahara Community
Support
/
issue about resize big image
22 September 2016, 14:54
Dear Mahara Developers,
I'm not sure if this is a bug or not, so I didn't create a bug track in launchpad.net.
It looks as if the PHP processes related to resizing images on upload may be running out of memory when Mahara attempts to resize very large images (some of these images were as big as 16MB). Maybe there’s a PHP setting we can adjust to accommodate this? Could you please help to advice on this issue?
Many thanks.
Our environment:
Mahara: 16.04.3
PHP Version => 5.6.23
Server version: Apache/2.4.6 (CentOS)
The below screenshot shows the warning message we've got while resizing the big image:
Best wishes,
Lina
23 September 2016, 17:14
Hi Lina
Take a look at htdocs/lib/imageresizer.php and in there are a couple of checks to do with image resizing and memory
First thing you can check is the $cfg->maximageresizememory setting in either config.php or lib/config-defaults.php.
By default it is set to 104857600 bytes (100MB) so that should be enough.
But another thing to check is php's memory_get_usage() value, which returns the amount of memory, in bytes, that's currently being allocated to your PHP script.
if the memory usage + the memory needed to resize the image is greater than $cfg->maximageresizememory then you'll get that error.
Cheers
Robert
23 September 2016, 20:43
Hi Robert,
Thank you very much for your help.
I've checked the $cfg->maximageresizememory setting in lib/config-defaults.php is the default 100M. The image we uploaded was 16M, it should be ok.
Do you have any other suggestions?
Many thanks.
Best wishes,
Lina
23 September 2016, 22:52
Hi Linda,
What value are you getting from php's memory_get_usage() ?
The process could be using more than 100M.
Please refer to the php manual about the memory_get_usage () function :
http://php.net/manual/en/function.memory-get-usage.php
Cheers,
Ghada
23 September 2016, 22:56
Hi Lina,
Sorry about misspelling your name in the last post. I missed the auto correction!
Cheers,
Ghada
29 September 2016, 22:10
Hi Ghada,
Many thanks for your reply.
I checked the lib/config-defaults.php file on our Mahara server, the configurations are as the below screenshot shows:
I read the manual in your link, but still don't know how can I track the real time memory usage. I think it is a little strange for a system to use more than 100M memory to just resize a 16M image, maybe the process could be improved?
No worries about the wrong spelling. Thanks.
Best wishes,
Lina
03 October 2016, 12:59
Hi Lina,
The basic difficulty is that JPG and PNG files are highly compressed, but to resize and otherwise transform images, PHP needs to decompress the image and store it in RAM. The size of a decompressed image is roughly the number of pixels in the image (width times height), multiplied by the color depth of the image. And PHP's GD image library seems to need some additional room beyond that (perhaps for indexes and such), so we further multiply that by a "tweak factor" of 1.8.
You can see firsthand how much memory an image needs if you open it up in PhotoShop or GIMP, and look at the Image Properties. I've got a 13.8MB PNG file on my computer, and opening it up in GIMP, I see that it is 77.8MB decompressed in memory. With the tweak factor, that's 140MB of RAM to process it.
To prevent Mahara from crashing due to running out of memory when resizing images, we try to pre-emptively calculate how much memory we'll need, and allocate it in advance. The code for this is in htdocs/lib/imageresizer.php. We use a formula based on this note in the PHP manual: http://uk3.php.net/manual/en/function.imagecreatefromjpeg.php#64155
That said, there probably is room for improvement there. We haven't updated that code in quite a while, so the situation may have changed. :)
Cheers,
Aaron
10 October 2016, 21:40
Hi Aaron,
Sorry for the late reply. I just back to office after our national holidays.
Many thanks for the nice and detail information. It's really helpful. I think I understand better now. Thanks for your help.
I've changed the $cfg->maximageresizememory setting from 104857600 bytes (100MB) to 400MB and the issue has been temporally solved.
Many thanks again for your kind help.
Best wishes,
Lina