简体   繁体   中英

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

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)?

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.

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() .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM