In 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 https://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:
- Upgrading to a new version of ImageMagick or an earlier one stable enough that there is no bug causing this problem.
- Recompiling ImageMagick with the –disable-openmp parameter to not use the multiprocessor feature.
- 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