Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick cache resource exhausted

I use PHP and Imagick extension for resize a photo (about 5000x7000):

$img = new Imagick($fn);
$img->thumbnailImage($width, 0);

It works for $width is 800, 900 ... 1300, but it crashes for 1400 and 1600: cache resources exhausted /app/photo.jpg' @ error/cache.c/OpenPixelCache/4083.

I assumed it depends on the width and the result image size. And googling for the error text confirmed my guesses. They advise to increase memory limits in ImageMagick config.

But! I tried to create image with the width is 2000 and it worked correctly!

1400, 1600, 1800 - fail. 1900, 2000, 2100... - success.

I'm lost.

like image 343
androkettle Avatar asked Oct 24 '25 11:10

androkettle


1 Answers

Increase the amount of resources that ImageMagick may use. You can control how many resources ImageMagick may use by editing the policy.xml file. See the documentation on security policy.

On my (Debian Linux) system the configuration is located at /etc/ImageMagick-6/policy.xml. See here for different locations on various operating systems.

I changed the following settings to 2GiB to get rid of the error, you may need different ones, depending on your needs and system resources. Take a look at the documentation.

<policy domain="resource" name="memory" value="2GiB"/>
<policy domain="resource" name="map" value="2GiB"/>
like image 188
sebastian Avatar answered Oct 26 '25 00:10

sebastian



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!