开发者

PHP rebuilding images: memory usage

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4912 bytes) in /var/www/development/example/system/libraries/Image.php on line 130.

The JPEG image in question does not have a particularly large file size (741 KB). We've used this same code to rebuild larger images. However, the image does have unusually large dimensions (4912px x 3264px). Would this have an effect?

What determines memory usage when PHP is rebuilding an image? Is it just the file size? The dimensions? The colour density? The file type?

The line on which it broke was

$f1    = 'imagecreatefrom' . $tag;
$s开发者_运维知识库rc   = $f1($file);

I think that's enough context. It didn't get as far as trying to rebuild the image. Loading it into memory was enough to break it.


As riky said, set the memory limit higher if you can. Also realize that the dimensions are more important than the file size (as the file size is for a compressed image). When you open an image in GD, every pixel gets 3-4 bytes allocated to it, RGB and possibly A. Thus, your 4912px x 3264px image needs to use 48,098,304 to 64,131,072 bytes of memory, plus there is overhead and any other memory your script is using.


Increase your memory buffer size

php_value memory_limit 64M in your .htacess

or ini_set('memory_limit','64M'); in your php file


It depends your implimentation. last time when I was working on csv file with more then 500000 records, I got the same message. Later I introduce classes and try to close the open objects. it reduces it memeory consumption. if you are opening an image and editing it. it means it is loading in a memory. in that case size really matter. if you are operating multiple images. I will record to one per image and then close that image. In my experience when I was working on pdf artwork files to check the crop marks. I was having the same error.

//you can set the memory limits values 
// in htaccess
php_value memory_limit 64M 
//or in you using following in php
ini_set('memory_limit', '128M'); 
//or update it in your php.ini file

but if you optimize your code. and use object oriented aproach then you memory consumption will be very less. because in that every object has its own scope and out of that scope it is destroyed.


The size of the used memory depends on the dimension and the color bit depth. I also ran in to that problem a few years ago, while building a portfolio-website for photographers. The only way to properly solve this is to switch your image library from GD to imagick. Imagick consumes far less memory, and is not tied to the PHP memory limit.

I have to say that the images the photographers uploaded were up to 30MP. And setting the memory limit to over 1024MB makes no sense in my eyes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜