开发者

imagejpeg memory exhaustion

开发者 https://www.devze.com 2022-12-27 04:22 出处:网络
I\'m creating thumbnails cycling through a lot of images, when I find a large image I get: Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 13056 bytes)

I'm creating thumbnails cycling through a lot of images, when I find a large image I get:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 13056 bytes)

Now I already know how to circumvent this with:

ini_set('memory_limit', '-1');

What I want to know is why it exhaust the memory! Is there some debug tools that will show me exactly when memory is exhausting? And specifically that wil开发者_JAVA技巧l show me if there are variables/arrays that are killing my memory?

OR, are there better way to resize images other then:

$thumb=imagecreatetruecolor($newwidth,$newheight); 
$source=imagecreatefromjpeg($imgfile);
imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($thumb,$destinationfile,85);

?

Thank you very much!


You might need to call imagedestroy() once you have finished with each image (i.e. $source and $thumb), especially if you are processing loads of images. I'm not sure that PHP's garbage collection will clear GD image resources.


Is there some debug tools that will show me exactly when memory is exhausting?

You can use memory_get_usage and memory_get_peak_usage

And specifically that will show me if there are variables/arrays that are killing my memory?

Sometimes variables are just resource holders that, in essence, point to data structures that may be created in other languages. PHP cannot measure these. Just use the memory usage functions after suspect lines in your code to narrow down the culprit.

Certain setups (like shared hosting) restrict your ability to modify memory requirements. These restrictions can be implemented at the PHP, web server and/or OS level. You'll want to ensure that your memory_limit request is successful by calling phpinfo().

0

精彩评论

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