【发布时间】:2017-07-02 16:08:39
【问题描述】:
我正在 Windows 10 周年更新的 XAMPP (Apache) 下运行 CakePHP 项目。
Apache 正在我的用户帐户下运行。
应用程序通过 shell_exec() 调用多个外部进程:ImageMagick、phantomjs 按预期执行。
我还想调用一个 bash 脚本,然后在 Ubuntu bash 下调用 ImageMagick(通过 apt-get 单独安装)。我不得不将所有路径调整为 bash 可以解决的形式。
bash /mnt/e/Projects/project-name/website/bin/crop_to_aspect.sh 1 1
/mnt/e/Projects/project-name/website/webroot/images/agent_photo/tmp_ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
/mnt/e/Projects/project-name/website/webroot/images/agent_photo/ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg```
通过 shell_exec() 调用该命令失败。同样的命令,写入 CakePHP 的日志,然后从 cmd.exe 提示符调用,运行良好。
认为这可能是路径问题,我将相同的脚本包装在一个 Windows 批处理文件中,包括 bash 的完整路径。我调用了完整的批处理文件路径,即:
@echo off
SET aw=%1
SET ah=%2
SET in=%3
SET out=%4
C:\Windows\System32\bash.exe /mnt/e/Projects/project-path/website/bin/crop_to_aspect.sh %aw% %ah% %in% %out%
然后调用此脚本:
E:\Projects\project-path\website\bin\crop_to_aspect.bat 4 3
/mnt/e/Projects/project-path/website/webroot/images/listing_photo/tmp_ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
/mnt/e/Projects/project-path/website/webroot/images/listing_photo/ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
再一次,该命令在cmd.exe 中正确执行,但在通过 PHP 脚本中的 shell_exec() 运行时什么也不做。
【问题讨论】:
-
可能值得使用 php.net/manual/en/book.imagick.php 将脚本重写为本机 PHP,这样不会有可移植性问题
-
公平的调用,我正在使用其他人的代码库,在 linux 上开发,但是 shell 脚本重写起来非常简单
标签: php bash windows-10 shell-exec