当我使用 inkscape
在终端中将 eps
转换为 svg
时,它工作正常。
但是当我使用 php 的 shell_exec 执行相同的命令时,它不起作用。 (我也尝试了 exec
和 system
但没有成功)
示例代码:
<?php
unlink('./sample.svg');
$file_path = realpath('./sample.eps');
$dest_path = getcwd() . '/sample.svg';
//# inkscape --file=sample.eps --export-plain-svg=sample.svg
// command works fine in terminal but not in php
$command = "inkscape --file=$file_path --export-plain-svg=$dest_path";
// command fails with no output (null)
$output = shell_exec($command);
var_dump($output);
var_dump(is_file('./sample.svg'));
相同的命令在 php shell 中也可以正常工作!
我无法确定原因,因为我无法检查输出(它始终为空)
使用 ai -> svg
和 pdf -> svg
进行转换效果良好
我怀疑这与command works fine through terminal but not shell_exec php类似。但这种情况的解决方案是什么?
PS:我正在使用this sample eps file用于测试
编辑:
我将 2>&1
添加到 shell_exec
命令并得到此输出
/srv/www/git/presta17_designer/eps/sample.eps:1: parser error : Start tag expected, '<' not found
%!PS-Adobe-3.0 EPSF-3.0
^
/srv/www/git/presta17_designer/eps/sample.eps:1: parser error : Start tag expected, '<' not found
%!PS-Adobe-3.0 EPSF-3.0
^
** (inkscape:717): WARNING **: 14:16:21.492: Specified document /srv/www/git/presta17_designer/eps/sample.eps cannot be opened (does not exist or not a valid SVG file)
更多信息:
在评论者的帮助下我找到了问题的原因
在 shell_exec
中,PATH 环境为空
(var_dump(shell_exec("printenv PATH"));
)
但用于eps
的ghostscript
位于/usr/bin/ghostscript
因此 PATH=/usr/bin
应放在命令前面才能正常工作
最佳答案
Inkscape 使用 ghostscript
进行 .eps
转换,您的情况中抛出的错误并不表明这一点,但如果 inkscape 找不到 Ghostscript,则确实会引发 - 因此我建议您检查修改后的 PATH
的哪一部分确实解决了问题。
您发现命令前面的最小 PATH=/usr/bin
足以解决您的情况下的问题,在我看来,这确实是更好的解决方案。
关于PHP:使用inkscape将eps转换为svg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251449/