GNU/Linux, Open Source, Cloud Computing, DevOps and more...

High CPU load when converting images with ImageMagick

23 comments

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.


Error: Your Requested widget " ai_widget-6" is not in the widget list.
  • [do_widget_area above-nav-left]
    • [do_widget_area above-nav-right]
      • [do_widget_area footer-1]
        • [do_widget id="wpp-4"]
      • [do_widget_area footer-2]
        • [do_widget id="recent-posts-4"]
      • [do_widget_area footer-3]
        • [do_widget id="recent-comments-3"]
      • [do_widget_area footer-4]
        • [do_widget id="archives-4"]
      • [do_widget_area logo-bar]
        • [do_widget id="oxywidgetwpml-3"]
      • [do_widget_area menu-bar]
        • [do_widget id="search-3"]
      • [do_widget_area sidebar]
        • [do_widget id="search-4"]
        • [do_widget id="ai_widget-2"]
        • [do_widget id="categories-5"]
        • [do_widget id="ai_widget-3"]
        • [do_widget id="ai_widget-4"]
        • [do_widget id="ai_widget-5"]
      • [do_widget_area sub-footer-1]
        • [do_widget id="text-4"]
      • [do_widget_area sub-footer-2]
        • [do_widget_area sub-footer-3]
          • [do_widget_area sub-footer-4]
            • [do_widget_area upper-footer-1]
              • [do_widget id="search-2"]
              • [do_widget id="recent-posts-2"]
              • [do_widget id="recent-comments-2"]
              • [do_widget id="archives-2"]
              • [do_widget id="categories-2"]
              • [do_widget id="meta-2"]
            • [do_widget_area upper-footer-2]
              • [do_widget_area upper-footer-3]
                • [do_widget_area upper-footer-4]
                  • [do_widget_area widgets_for_shortcodes]
                    • [do_widget id="search-5"]
                    • [do_widget id="ai_widget-6"]
                  • [do_widget_area wp_inactive_widgets]
                    • [do_widget id="wpp-2"]
                    • [do_widget id="text-1"]
                    • [do_widget id="recent-posts-3"]
                    • [do_widget id="categories-3"]
                    • [do_widget id="archives-3"]
                    • [do_widget id="icl_lang_sel_widget-3"]

                  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:

                  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.


                  Error: Your Requested widget " ai_widget-6" is not in the widget list.
                  • [do_widget_area above-nav-left]
                    • [do_widget_area above-nav-right]
                      • [do_widget_area footer-1]
                        • [do_widget id="wpp-4"]
                      • [do_widget_area footer-2]
                        • [do_widget id="recent-posts-4"]
                      • [do_widget_area footer-3]
                        • [do_widget id="recent-comments-3"]
                      • [do_widget_area footer-4]
                        • [do_widget id="archives-4"]
                      • [do_widget_area logo-bar]
                        • [do_widget id="oxywidgetwpml-3"]
                      • [do_widget_area menu-bar]
                        • [do_widget id="search-3"]
                      • [do_widget_area sidebar]
                        • [do_widget id="search-4"]
                        • [do_widget id="ai_widget-2"]
                        • [do_widget id="categories-5"]
                        • [do_widget id="ai_widget-3"]
                        • [do_widget id="ai_widget-4"]
                        • [do_widget id="ai_widget-5"]
                      • [do_widget_area sub-footer-1]
                        • [do_widget id="text-4"]
                      • [do_widget_area sub-footer-2]
                        • [do_widget_area sub-footer-3]
                          • [do_widget_area sub-footer-4]
                            • [do_widget_area upper-footer-1]
                              • [do_widget id="search-2"]
                              • [do_widget id="recent-posts-2"]
                              • [do_widget id="recent-comments-2"]
                              • [do_widget id="archives-2"]
                              • [do_widget id="categories-2"]
                              • [do_widget id="meta-2"]
                            • [do_widget_area upper-footer-2]
                              • [do_widget_area upper-footer-3]
                                • [do_widget_area upper-footer-4]
                                  • [do_widget_area widgets_for_shortcodes]
                                    • [do_widget id="search-5"]
                                    • [do_widget id="ai_widget-6"]
                                  • [do_widget_area wp_inactive_widgets]
                                    • [do_widget id="wpp-2"]
                                    • [do_widget id="text-1"]
                                    • [do_widget id="recent-posts-3"]
                                    • [do_widget id="categories-3"]
                                    • [do_widget id="archives-3"]
                                    • [do_widget id="icl_lang_sel_widget-3"]

                                  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
                                  
                                   

                                  About the author

                                  Daniel Lรณpez Azaรฑa
                                  Freelance AWS Cloud Solution Architect & Linux Sysadmin

                                  Entrepreneur, a generator of ideas and restless mind. Passionate about new technologies, especially Linux systems and Open Source Software. I also like to write about Technology News, Cloud Computing, AWS, DevOps, DevSecOps, System Security, Web Development and Programming, SEO, Science, Innovation, Entrepreneurship, etc.

                                  DanielHigh CPU load when converting images with ImageMagick

                                  Related Posts

                                  23 comments

                                  Join the conversation
                                  • G - 21/03/2012 reply

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

                                    Daniel - 22/03/2012 reply

                                    You’re welcome! I’m glad that helped you.

                                  • KWardle - 26/03/2012 reply

                                    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!

                                  • KWardle - 26/03/2012 reply

                                    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.

                                  • Michael - 07/07/2012 reply

                                    As “G” said, amazing performance improving! Use โ€“disable-openmp! ๐Ÿ™‚

                                    M.

                                  • don bright - 10/09/2012 reply

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

                                  • Noah - 25/09/2012 reply

                                    Not what I was looking for

                                  • Dmitry - 12/10/2012 reply

                                    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 )

                                    Daniel - 18/10/2012 reply

                                    Thanks for the feedback Dmitry!

                                  • Mikhail - 02/11/2012 reply

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

                                  • Sergey - 13/12/2012 reply

                                    Thanks, dude, this solution saves my balls.

                                  • seraph - 05/02/2013 reply

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

                                    Imagick::setResourceLimit(6, 1);

                                  • Sumeet Kashyap - 10/02/2013 reply

                                    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?

                                  • MarkusK - 02/04/2013 reply

                                    Thank you, this helped me a lot!

                                  • nikita - 05/04/2013 reply

                                    Thank you!

                                  • Leon - 03/12/2013 reply

                                    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.

                                  • Morty - 06/03/2014 reply

                                    Amazing! So simple yet so cool. Saved my project ๐Ÿ˜€

                                  • Steve - 21/03/2015 reply

                                    I tried using ‘Imagick::setResourceLimit (6, 1); in my php script, but ran into an issue. The thumb is created successfully, but it outputs ‘Strict Standards: Non-static method Imagick::setresourcelimit() should not be called statically in…’ to the page similar to an error message. Outputting that message makes it unusable for me. Any ideas?

                                    Clau - 06/04/2015 reply

                                    Yes, try to call the imagick method on an instance of the class, not on the class itself:
                                    $my_imagick_object->setResourceLimit(6,1);

                                    Cheers!

                                  • Bodo - 15/08/2016 reply

                                    Just want to let you know that on my system the ->setResourceLimit(6,1); does not help.
                                    Still takes around 250(!) seconds to convert around 4 images from PDF. Horrible.

                                  • Bodo - 15/08/2016 reply

                                    Hello

                                    I am lost with anything here and would appreciate any help.
                                    What is the solution today (2016) with that ImageMagick problem on virtual Servers ?

                                    I am simply calling it in PHP. I do not want to change all my php scripts now. It takes around 30-60 seconds for one image to convert from PDF to JPG, with some filters an re-sizing.

                                    How do I install a new ImageMagick Version without multi-core? How do I tell PHP to use this new version? The new Version 7.x.x. does not use “convert”, it uses “magick” now…so I do not know how to change that. phpInfo() shows me it is still using the old version.

                                    setResourceLimit(6,1) – did not change anything on my System. No solution.

                                    I do not understand the last part of this tutorial: “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 ”

                                    I do not have a vhost.conf
                                    I do not have a cgi-wrapper file
                                    Nothing…
                                    (I have virtual hosts, but they are in the directory sites-enabled)

                                    I am so lost with that thing! ๐Ÿ™ It seems none of the things I do can solve the problem.
                                    Why do the people who programm ImageMagick not solve it and make a single-thread option. It is so useless at the moment. Crazy.

                                    Daniel - 16/08/2016 reply

                                    Maybe you installed the new version of ImageMagick command line tool, but didn’t upgrade php-imagick package (sudo apt-get install php-imagick), wich is the native PHP extension to create and modify images using the ImageMagick API. Note that they both have different versions.

                                    Sorry, but MAGICK_THREAD_LIMIT=1 and setResourceLimit(6,1) are the only methods I know to force 1 thread execution. And they shouldn’t be necessary in recent versions of ImageMagick, as the bug mentioned in this article is probably already solved.

                                    You can write me an email and attach the files you’re trying to convert and I’ll try to give you additional support, as the problem could be related to something else.

                                    Otherwise, you can try with the GD graphics library to perform the same task, maybe you’re luckier.

                                  • xia - 24/05/2019 reply

                                    i have a problem;I used ‘ env MAGICK_THREAD_LIMIT=1’ but the cpu is still too high;

                                  Leave a Reply

                                  Your email address will not be published.