High CPU load when converting images with ImageMagick

Imagemagick-logoIn 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

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

# cat /var/www/cgi-bin/cgi_wrapper/cgi_wrapper
#! /bin/sh
exec /usr/bin/php5-cgi


  1. G 21/03/2012
    • daniloaz 22/03/2012
  2. KWardle 26/03/2012
  3. KWardle 26/03/2012
  4. Michael 07/07/2012
  5. don bright 10/09/2012
  6. Noah 25/09/2012
  7. Dmitry 12/10/2012
  8. Mikhail 02/11/2012
  9. Sergey 13/12/2012
  10. seraph 05/02/2013
  11. Sumeet Kashyap 10/02/2013
  12. MarkusK 02/04/2013
  13. nikita 05/04/2013
  14. Leon 03/12/2013
  15. Morty 06/03/2014
  16. Steve 21/03/2015
    • Clau 06/04/2015
  17. Bodo 15/08/2016
  18. Bodo 15/08/2016

Leave a Reply

Your email address will not be published. Required fields are marked *