当我在终端中使用 wkhtmltoimage 时,效果很好。但在php中使用时会出现一些问题。这是问题所在: php代码:
<?php
$command = './wkhtmltoimage --width 164 --height 105 --quality 100 --zoom 0.2 http://www.google.com file/test.jpg';
ob_start();
passthru($command);
$content = ob_get_clean();
echo $command;
echo $content;
?>
Run Code Online (Sandbox Code Playgroud)
它有效。当我在终端中尝试相同的命令时,它也运行良好。
但是当我尝试其他链接时,它无法正常工作。
<?php
$command = './wkhtmltoimage --width 164 --height 105 --quality 100 --zoom 0.2 http://codante.org/linux-php-screenshot file/test.jpg';
ob_start();
passthru($command);
$content = ob_get_clean();
echo $command;
echo $content;
?>
Run Code Online (Sandbox Code Playgroud)
它确实有效。但是当我在终端中尝试相同的命令时。它有效!请帮助我。
我猜测出于安全原因,passthru在用于 Web 服务器的文件中禁用了该功能。php.ini尝试执行以下代码:
function passthru_enabled() {
$disabled = explode(', ', ini_get('disable_functions'));
return !in_array('exec', $disabled);
}
if (passthru_enabled()) {
echo "passthru is enabled";
} else {
echo "passthru is disabled";
}
Run Code Online (Sandbox Code Playgroud)
如果它被禁用,那么除非您可以编辑 php.ini 文件,否则您实际上无能为力。
编辑:另外,请确保在代码中启用错误报告,如果您尝试使用禁用的功能,代码也应该显示某种警告。将其放入您的代码中:
error_reporting(-1);
ini_set('display_errors', 'On');
Run Code Online (Sandbox Code Playgroud)
编辑:
如果passthru启用,那么我认为命令应该由命令行而不是 PHP 正确执行的唯一原因是因为它没有正确传递到命令行。尝试使用escapeshellarg在参数周围添加引号。
$url = escapeshellarg('http://codante.org/linux-php-screenshot');
$command = "./wkhtmltoimage --width 164 --height 105 --quality 100 --zoom 0.2 $url file/test.jpg";
Run Code Online (Sandbox Code Playgroud)
您可能还想利用 的第二个参数passthru,它返回命令的退出状态。非零值表示存在错误。
passthru($command, $status);
if ($status != 0) {
echo "There was an error executing the command. Died with exit code: $status";
}
Run Code Online (Sandbox Code Playgroud)
有关这些退出代码的列表可帮助您调试正在发生的情况,请参阅具有特殊含义的退出代码