Imagick hitting memory limit


#1

On DreamHost I cannot initialize an Imagick object with an image file (JPG, etc.) greater than ca. 8MB. When I run my site on my localhost, either on XAMPP or WAMP, image files up to 150MB may be initialized in an Imagick object. On DreamHost, it is not a time-out issue, as the routine crashes instantly when my code attempts to create an Imagick object greater than ca. 8MB, yet it does not crash and works perfectly on smaller files. (This is not an upload issue, either, as the files in question are already uploaded to my DreamHost server.)

I’ve corresponded with the folks at ImageMagick, and they suspect it may have to do with a low value for the MAGICK_MEMORY_LIMIT and/or MAGICK_MAP_LIMIT.

See: http://www.imagemagick.org/script/resources.php

I’ve located the policy.xml file for my site at DreamHost in the folder etc/ImageMagick off the root, and that is apparently where those values may be increased. Apparently their default values are 8MB when not explicitly provided in the policy.xml file, which would seem to correlate with the ceiling I keep hitting. I’ve tried to reset those directives to a higher value (by sftp’ing to my site and its upper directories in order to view and edit the policy.xml file) via FileZilla, but I apparently do not have sufficient permission to edit the policy file.

Could this be investigated, or, alternately, could you explain how I can log in such that I can modify the policy.xml file so that my Imagick routines do not hit this memory limt? I’m unclear whether that policy file is specific to my domain or to every domain on the server. (If the former is the case, I’d like to be able to edit the values in question to see if I can eliminate the memory limitation.)


#2

Likely you should try the custom installation for shared hosting, that should get a policy.xml for just your own installation.

It is highly unlikely dreamhost will adjust this for all on shared hosting, instead more than likely they would suggest upgrade to dreamcompute. Adjusting the value globally would effect not 1 but hundreds of shared servers, which could create server load problems for 1000’s of users that didn’t want or need the change in the first place. The shared hosting environment rarely changes, if more power is needed then it’s time to upgrade to a different product. Shared hosting is a low cost entry point that is not designed to meet everyone’s needs.


#3

I’ve apparently found the solution.

I hit on this solution when I discovered that at certain times of the day I could process fairly large files with Imagick on the live server, but then could not just a few minutes later. This suggested to me that memory was being consumed by others on the shared server at fully expected erratic and unpredictable intervals, and therefore if there were a way to allocate memory to the Imagick object before initializing a file in it, then it might work.

The trick is to explicitly allocate memory to the Imagick object after creating it using the setResourceLimit() function of the object, calling the 5 “RESOURCETYPE” values as shown below. What follows that is simply the resizing routine I perform on the image file, which was never the source of the problem.

[php]$im = new Imagick();

$im->setResourceLimit(imagick::RESOURCETYPE_MEMORY, 256);
$im->setResourceLimit(imagick::RESOURCETYPE_MAP, 256);
$im->setResourceLimit(imagick::RESOURCETYPE_AREA, 1512);
$im->setResourceLimit(imagick::RESOURCETYPE_FILE, 768);
$im->setResourceLimit(imagick::RESOURCETYPE_DISK, -1);

$im->readImage($source_image_path);
$im->resizeImage(528, null, Imagick::FILTER_LANCZOS2SHARP, 1);
$im->stripImage();
$im->writeImage($destination_image_path);
$im->clear();
[/php]

I hope this is helpful to someone else, because I was banging my head against the wall trying to find a solution; Imagick/ImageMagic is indispensable to my site.