user3778390
user3778390

Reputation: 395

PHP ImageMagick use 10gb~ memory when resizing a big image

I have problem when I try to resize a very big image like a 30,000px x 20,000px with ImageMagick software. But I do not have enought RAM. I have 8GB, but the software requires about 10GB~. I need to use all data in RAM. I tried two options:

1 using convert, writes to a different image file:

convert -resize 90% source.jpg destination.jpg
  1. using mogrify overwrites the original image file

    mogrify -resize 90% source.jpg

Maybe someone knows how I can use less memory or some other tricks.

More information about Imagemagick: http://www.imagemagick.org/script/mogrify.php

Upvotes: 0

Views: 1699

Answers (3)

Tanvir Ahmad
Tanvir Ahmad

Reputation: 273

Edit the policy.xml which resides under /etc/ImageMagick/policy.xml

In my case it was using more than 2GiB, but here you can limit so that script should be running under the provided limit. I set it for 512MiB for map and memory.

Upvotes: 0

emcconville
emcconville

Reputation: 24419

If your attempting to configure ImageMagick's memory polices to use more RAM, before caching to disk, define the AREA limit by modifying policy.xml or set an environment variable.

Setting Memory limit in Environment

This is probably the quickest way for one-off process.

MAGICK_AREA_LIMIT=8GB convert -resize 90% source.jpg destination.jpg

Also attempt adjusting other environment vars

  • MAGICK_MAP_LIMIT - Maximum amount of memory map in megabytes to allocate for the pixel cache.
  • MAGICK_MEMORY_LIMIT - Maximum amount of memory in megabytes to allocate for the pixel cache from the heap.

Setting Memory limit with Policy.xml

Edit the policy.xml under your {$PREFIX}/lib/ImageMagic-X.X.X/config directory.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
<!ELEMENT policymap (policy)+>
<!ELEMENT policy (#PCDATA)>
<!ATTLIST policy domain (delegate|coder|filter|path|resource) #IMPLIED>
<!ATTLIST policy name CDATA #IMPLIED>
<!ATTLIST policy rights CDATA #IMPLIED>
<!ATTLIST policy pattern CDATA #IMPLIED>
<!ATTLIST policy value CDATA #IMPLIED>
]>
<policymap>
  <policy domain="resource" name="memory" value="8GB"/>
  <policy domain="resource" name="map" value="8GB"/>
  <policy domain="resource" name="area" value="8GB"/>
</policymap>

Verification

Whatever method you choose, you can verify by running the identify utility.

$ identify -list resource

  File       Area     Memory        Map       Disk   Thread  Throttle       Time
--------------------------------------------------------------------------------
  1920        8GB        8GB        8GB  unlimited        1         0  unlimited

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207355

It's not clear what you are trying to achieve overall, but you can resize an image while reading it like this and it should take less memory:

convert source.jpg'[15000x10000]' destination.jpg

Upvotes: 1

Related Questions