views:

313

answers:

1

When using ImageMagick, I can set certain limits for memory usage and maximum number of threads. There are 3 ways to do this, as far as I know:

  1. use a command line options like "convert -limit memory 128mb original.jpg new.jpg"
  2. use environment variables like "MAGICK_THREAD_LIMIT=1"
  3. edit the 'policy.xml' configuration file to change the default value.

I have tested each of these methods using "convert -list resource" and they work.

Now, I need to use the IMagick extension in PHP. There is a function I can use to set limits:

bool Imagick::setResourceLimit (int $type, int $limit)

For the first parameter I can use one of the following:

imagick::RESOURCETYPE_AREA (integer)   //equivalent of MAGICK_AREA_LIMIT
imagick::RESOURCETYPE_DISK (integer)   //equivalent of MAGICK_DISK_LIMIT
imagick::RESOURCETYPE_FILE (integer)   //equivalent of MAGICK_FILE_LIMIT
imagick::RESOURCETYPE_MAP (integer)    //equivalent of MAGICK_MAP_LIMIT
imagick::RESOURCETYPE_MEMORY (integer) //equivalent of MAGICK_MEMORY_LIMIT

The problem is that there is no equivalent for MAGICK_THREAD_LIMIT and IMagick seems to simply ignore the configuration files and the environment variables. How do I know this? I've set all the memory limits to zero and IMagick still functions without any problem when it should report insufficient memory.

I really hope I have made myself clear. The question is: how can I change the thread limit when using IMagick?

Thank you in advance.

EDIT: I've managed to set the thread limit to 1 by compiling ImageMagick with the '--without-threads' option. :P It will have to do until I find a better solution.

+1  A: 

There is no corresponding constant defined for the thread limit in the PHP IMagick extension, but looking at the source the integer value should be 6 so you could try that (see ResourceType in magick/resource_.h, the needed value is ThreadResource). Am using MagickWand for PHP and had the same issue--fix was to enable this constant and recompile. If you are interested in patching MagickWand for PHP 1.0.8 the fix is:

magickwand_inc.h
-#define PRV_IS_ResourceType( x ) (x == AreaResource || x == DiskResource || x == FileResource || x == MapResource || x == MemoryResource)  /* || x == UndefinedResource */
+#define PRV_IS_ResourceType( x ) (x == AreaResource || x == DiskResource || x == FileResource || x == MapResource || x == MemoryResource || x == ThreadResource)  /* || x == UndefinedResource */

magickwand.c
    MW_REGISTER_LONG_CONSTANT( MemoryResource );
+   MW_REGISTER_LONG_CONSTANT( ThreadResource );
Erik Bielefeldt
Thanks! Right now I'm using ImageMagick compiled with the '--without-threads' flag and it also did the trick. I don't know why but ImageMagick runs 2-3 times slower on my VPS if it's compiled to use threads.
bilygates