我应该使用 ImageMagick 的 PHP 扩展还是只使用 PHP 的 Exec() 函数来运行终端命令?
我需要对我网站上的用户上传的图像进行以下图像处理:
- 调整图像大小(如果大于特定尺寸)
- 将所有图像格式转换为 jpg
- 在所有图像的底部添加水印
我需要使用 MagickWand或 iMagick 扩展,或者我可以在 PHP 的 exec 函数中运行终端命令吗?
是否有理由首选 PHP 扩展?哪个更快、性能更好(该网站可能有很多用户,并且在任何给定时间都会有大量的图像处理)?
I need to do the following image manipulations for images uploaded by users on my site:
- Resize images (if greater than a certain dimension)
- Convert all image formats to jpg's
- 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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想对draw010的回答进行反驳。他在回答中指出:
对于图像处理来说,确实如此。好吧,我知道使用 PHP 的 exec() 函数(或类似函数)调用 ImageMagick 二进制文件会比使用嵌入式 PHP 库带来额外的开销,但是我不清楚有多少开销是有的。
但事情还有另一面。如果您通过扩展将 ImageMagick 代码嵌入到 PHP 中,则每个使用 PHP 的请求都会占用更多内存,无论该请求是否处理图像。如果您使用 Apache 的
mod_php
,那么这将变得更多问题,因为现在对服务器的每个请求都会在内存中包含 ImageMagick 库,即使它正在提供服务HTML 文件!所以这确实取决于。您确实希望将 PHP 的内存占用量保持在尽可能低的水平。如果您正在处理大量需要 ImageMagic 的请求并且您正在使用 FastCGI 或 php_fpm ,那么您可能会发现使用嵌入式 ImageMagick 的好处。但是,如果您只是偶尔使用 ImageMagick 和/或使用 Apache 的
mod_php
处理请求,那么通过exec()
调用 ImageMagick 可能会获得更好的性能。I'd like to make a counterpoint to drew010's answer. In his answer he states:
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'smod_php
then you may get much better performance calling ImageMagick viaexec()
.使用 PHP 扩展而不是使用 exec 或类似函数会让您受益匪浅。内置扩展将更快并且使用更少的内存,因为您不必生成新进程并读回输出。图像对象将在 PHP 中直接可用,而不必读取文件输出,这将使图像更易于使用。
如果您的网站很繁忙,创建大量进程来编辑图像可能会开始减慢速度并消耗额外的内存。
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.
我总是使用 PHP GD http://php.net/manual/en/book.image。 php
您可以完成调整大小、转换为 JPG 以及为图像添加水印。我知道你的帖子说你必须使用 MagickWand 或 iMagick,但我只是想介绍这个选项,以防它对你有用。
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.