Should I use a PHP extension for ImageMagick or just use PHP's Exec() function to run the terminal commands?

10,023

Solution 1

You would benefit a lot using the PHP extensions instead of using exec or similar functions. Built in extensions will be faster and use less memory as you will not have to spawn new processes and read the output back. The image objects will be directly available in PHP instead of having to read file output, which should make the images easier to work with.

If you have a busy site, creating lots of processes to edit images may start to slow things down and consume additional memory.

Solution 2

I'd like to make a counterpoint to drew010's answer. In his answer he states:

You would benefit a lot using the PHP extensions instead of using exec or similar functions. Built in extensions will be faster and use less memory as you will not have to spawn new processes and read the output back

For processing images, this is true. Well, I know for a fact that calling on ImageMagick binaries using PHP's exec() function (or similar functions) carries additional overhead above using embedded PHP libraries however I'm not clear on how much overhead there is.

But there's another side of the coin. If you embed the ImageMagick code into PHP via an extension, then every request using PHP takes more memory whether the request processes an image or not. If you're using Apache's mod_php then this becomes even more of an issue because now every request to your server has the ImageMagick libraries in memory even if it's serving an HTML file!

So it really depends. You really want to keep PHP's memory footprint as low as possible. If you're processing a large number of requests which require ImageMagic and you're using FastCGI or php_fpm, then you'll probably see a benefit to using embedded ImageMagick. But if you're only occasionally processing requests using ImageMagick and/or using Apache's mod_php then you may get much better performance calling ImageMagick via exec().

Solution 3

I always use PHP GD http://php.net/manual/en/book.image.php

You can accomplish resizing, converting to JPG and watermarking your images. I know your post said you have to use MagickWand or iMagick, but I just wanted to present this option in case it would work for you.

Share:
10,023
Admin
Author by

Admin

Updated on June 23, 2022

Comments

  • Admin
    Admin almost 2 years

    I need to do the following image manipulations for images uploaded by users on my site:

    1. Resize images (if greater than a certain dimension)
    2. Convert all image formats to jpg's
    3. Add a watermark to the bottom of all images

    Do I need to use either the MagickWand or iMagick extensions or can I just get away with running the terminal commands inside PHP's exec function?

    Is there a reason why the PHP extensions would be preferred? Which would be faster and better for performance (this site may have lots of users and there would be a ton of image processing at any given time)?

  • Bailey Parker
    Bailey Parker over 12 years
    You also might want to include a bit about how most (shared) hosts block exec() for security reasons.
  • Josh
    Josh over 12 years
    Built in extensions will be faster and use less memory Do you have any references for this? I'd love to see some benchmarks to see if it's significantly faster. I know calling on the convert binary spawns a new process and that's slower than using an embedded library, however I manage a number of busy sites which operate in this way and they seem to hold up fine. I'd love to know what the actual overhead is.
  • drew010
    drew010 over 12 years
    I don't have any direct benchmarks, I only made the statement based on the fact that spawned processes will consume at least some memory, possibly a lot depending on what they are doing. The php extensions should be fast because they are compiled into the php binary, and directly call library files to do their work. In this particular case, I figure the memory usage would less because the author may have had imagemagick manipulate the image, output it to a file, which he would then have to read back into memory using php to possibly serve the image back to the browser.
  • Salman A
    Salman A over 10 years
    GD is most commonly used "extension" for processing images; and it is slow and eats memory like crazy. Generally speaking, an execed binary (e.g. imagemagick) does not run into speed or memory issues.
  • TheCarver
    TheCarver over 10 years
    GD is an option, but more of a last resort if you ask me. My personal opinion of GD is that it is slow, memory consuming and basic. GD also ignores color profiles in photos, which is a major flaw. So if you ever deal with professional photos, stay away from GD unless you want color saturated photos (predominantly reds). For performance, memory usage, code reduction and image quality - Imagick wind hands down.
  • Chris Seufert
    Chris Seufert about 3 years
    Also would like to mention that GD's image scaling algorithms leave a bit to be desired compared to the power image imagemagick
  • Robin
    Robin over 2 years
    This is.. perfect!