17

High CPU load when converting images with ImageMagick

Posted March 13th, 2012 in Systems tagged ,

logo-image-magickIn certain versions of ImageMagick there is a problem (probably due to a bug) when trying to convert or manipulate an image either from the command line (convert command) or through one of the many API’s available (for example PHP). The CPU usage suddenly grows beyond 100% (because this problem occurs on multiprocessor systems) and the system becomes extremely slow. Apart from this high CPU load, the conversion process also gets blocked, never reaching to the end. This occurs even with small images of few kilobytes.

After some research, I indeed concluded that it’s a bug present in certain versions of ImageMagick compiled with the –enable-openmp parameter running on machines with multiple processors or multiple cores.

So the first thing to do to know if the solution proposed in this post is valid for us, is to check if the system has multiple processors or multiple cores: How to know how many cores and processors has a Linux box.

Then check if your version of ImageMagick is enabled with OpenMP feature:

# convert --version
Version: ImageMagick 6.5.7-8 2010-12-02 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC
Features: OpenMP

If the above command displays the “Features: OpenMP” line, then everything fits and our problem of excessive CPU load can be fixed in three ways:

  1. Upgrading to a new version of ImageMagick or an earlier one stable enough that there is no bug causing this problem.
  2. Recompiling ImageMagick with the –disable-openmp parameter to not use the multiprocessor feature.
  3. Forcing ImageMagick to use only one of the available cores present in the system to perform the conversion.

The latter is perhaps the best solution to our problem because probably we will have already tried to update ImageMagick to another version, and in many cases we will have ImageMagick installed as a package from one of the Linux distributions available. Thus, compiling ImageMagick again it not a viable choice.

Note that the procedure shown below does not really solve the problem. It’s only a workaround, a “trick” allowing us to use ImageMagick without falling into the CPU freezing mentioned earlier in this article.

Limit the number of cores using MAGICK_THREAD_LIMIT

The way to force ImageMagick to use a single processor or core when performing an image conversion is using the environment variable MAGICK_THREAD_LIMIT. This variable should be set to 1 and be available in the runtime environment of the convert command. So edit the /etc/environment file and add the line MAGICK_THREAD_LIMIT=1 as shown below:

# cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
LANG="es_ES.UTF-8"
LANGUAGE="es_ES.UTF-8"
MAGICK_THREAD_LIMIT=1

Then open a new command shell, since the current one will not load such environment variable until we log into the system again.

Setting the MAGICK_THREAD_LIMIT variable in PHP

If rather than using ImageMagick from the command line we do through libraries available for different programming languages ​​like PHP, the above solution will not work, so we’ll have to add the MAGICK_THREAD_LIMIT=1 variable to the PHP interpreter runtime environment in order to PHP properly load it when calling ImageMagick functions.

I’m always referring to the case of using the ImageMagick native functions in our PHP code, like “$im = new Imagick(‘file.png’);” or “$im->readImageBlob($svgin);”. If conversion is performed using the system command as in “system(‘convert -resize 800×600 file1.png file2.png’);”, we’ll be calling the convert command the same way as in the previous section, in which case that solution is of course valid.

PHP from command line

env MAGICK_THREAD_LIMIT=1 php test.php

PHP from mod_fcgid Apache module

In this case the MAGICK_THREAD_LIMIT variable would be defined in the runtime environment of /usr/bin/php5-cgi command, wich is usually called from an intermediate script set in an Apache virtualhost. This wrapper script is called FCGIWrapper, and we can find the path to it in the corresponding vhosts.conf file:

<Files ~ (\.php)>
   SetHandler fcgid-script
   FCGIWrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper .php
   Options +ExecCGI
   allow from all
</Files>

# cat /var/www/cgi-bin/cgi_wrapper/cgi_wrapper
#! /bin/sh
export MAGICK_THREAD_LIMIT=1
exec /usr/bin/php5-cgi

17 Responses so far.

  1. G says:

    Amazing … down from 20s to 1s … Thanks!

  2. KWardle says:

    Thanks! This also solves the problem that the convert -average function loads all the images into memory at once. Not so good if there are a lot of images. Thanks again!

  3. KWardle says:

    Oops this was not the intended page for my previous comment. Thanks for this though, it is useful. For the other, take a look at http://stephan.paukner.cc/syslog/archives/362-Averaging-an-image-sequence-with-ImageMagick.html.

  4. Michael says:

    As “G” said, amazing performance improving! Use –disable-openmp! :-)

    M.

  5. don bright says:

    Daniel this is brilliant!!! Thank you. I spent a huge amount of time trying to diagnose this.

  6. Noah says:

    Not what I was looking for

  7. Dmitry says:

    That’s great solution, thank you very much man! Instead of 15 sec for creating one simple thumbnail and 20% CPU load, now it takes 0 CPU and < 0.1 sec.

    I'm using virtual server hosting and have PHP php-imagick.

    Unfortunatley, env variable not works in my case with php. But here is solution that works for me:

    // insert this in your php before processing images
    Imagick::setResourceLimit(6, 1);

    (found here: http://stackoverflow.com/questions/2121137/limit-number-of-threads-in-imagick-php )

  8. Mikhail says:

    Just encountered that bug on Debian Lenny with ImageMagick 6.6.0-4. Thank you for the solution!

  9. Sergey says:

    Thanks, dude, this solution saves my balls.

  10. seraph says:

    I used this code to fix the problem inside php-script:

    Imagick::setResourceLimit(6, 1);

  11. Sumeet Kashyap says:

    I have read at some places about using this piece of code for php: putenv(“MAGICK_THREAD_LIMIT=1″);
    Is this correct and will produce the same results?

  12. MarkusK says:

    Thank you, this helped me a lot!

  13. nikita says:

    Thank you!

  14. Leon says:

    Thank you! You helped a lot with this post.

    For PHP+Apache it is also possible to add the variable in /etc/apache2/envvars, so a whole web server and all vhost etc. can be “fixed” at once. Took me a while to figure out, perhaps useful for somebody else trying to solve this problem.

  15. Morty says:

    Amazing! So simple yet so cool. Saved my project :D

Leave your comments...