exec() function within Laravel works wrong, in tinker and console properly

懵懂的女人 提交于 2021-01-28 08:00:48

问题


I have a weird problem with exec function within the Laravel app. Using imagemagick library I have to combine two pdf files - the same shell command works properly in terminal, exec() with this command in tinker works properly too. However, when called from job or controller within the Laravel app it looks like it is not seeing the original file at all. What can be the reason? All files are stored in generated storage subfolders.

  • works properly in tinker
  • works properly in terminal
  • works properly in simple test php file outside Laravel app
  • does not work in job/controller within the Laravel app (looks like it is not reading the input file_1.pdf (output file is generated with overlay file only, no source "background") Already tried Process::class as well, exec used to simplify the code. Permissions are set properly.
exec('convert "/Users/robert/Sites/start-app/storage/documents/file_1.pdf" null: "/Users/robert/Sites/start-app/storage/documents/stamps/1.png" -gravity SouthEast -geometry +150+150 -compose over -layers composite "/Users/robert/Sites/start-app/storage/documents/processed/output_file_1.pdf"');

回答1:


Do you have different php.ini files for servering web requests and handling cli? These are usually stored in the following (or similar) folders

/etc/php/7.3/cli/php.ini
/etc/php/7.3/fpm/php.ini

You can also check this by running phpinfo() from the cli and also from a web request and compare both.

If that's the case you should check out the disable_functions option. More info on that can be found here. Remember to restart your webserver afterwards, so the changes take effect.




回答2:


I have just finally found the solution. The issue was caused by path to GhostScript, even though imagemagick and gs are in the same path, for some reason when executed from within the app by exec() the gs command could not be found. Just in case someone faces similar problem, here is the solution:

exec('export PATH=/usr/local/bin:$PATH; convert "/Users/robert/Sites/start-app/storage/documents/file_1.pdf" null: "/Users/robert/Sites/start-app/storage/documents/stamps/1.png" -gravity SouthEast -geometry +150+150 -compose over -layers composite "/Users/robert/Sites/start-app/storage/documents/processed/output_file_1.pdf"');

Adding the export path made it working properly.



来源:https://stackoverflow.com/questions/56933082/exec-function-within-laravel-works-wrong-in-tinker-and-console-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!