Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imagejpeg memory exhaustion

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 will 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!

like image 897
0plus1 Avatar asked Apr 01 '26 19:04

0plus1


1 Answers

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.

like image 137
Tom Haigh Avatar answered Apr 04 '26 11:04

Tom Haigh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!