在php中使用wkhtmltoimage

2024-04-10

当我在终端中使用 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;
?>

它有效。当我在终端中尝试相同的命令时,它也运行良好。

但是当我尝试其他链接时,它无法正常工作。

<?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;
?>

它确实有效。但是当我在终端中尝试相同的命令时。它有效! 请帮助我。


我猜passthru被禁用于php.ini出于安全原因用于 Web 服务器的文件。尝试执行以下代码:

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";
}

如果它被禁用,那么除非您可以编辑 php.ini 文件,否则您实际上无能为力。

编辑:另外,请确保在代码中启用错误报告,如果您尝试使用禁用的功能,代码还应该显示某种警告。将其放入您的代码中:

error_reporting(-1);
ini_set('display_errors', 'On');

Edit:

If passthru启用,那么我能想到命令应该由命令行而不是 PHP 正确执行的唯一原因是因为它没有正确传递到命令行。尝试在参数周围添加引号转义shellarg http://www.php.net/manual/en/function.escapeshellarg.php.

$url = escapeshellarg('http://codante.org/linux-php-screenshot');
$command = "./wkhtmltoimage --width 164 --height 105 --quality 100 --zoom 0.2 $url file/test.jpg";

您可能还想利用第二个参数passthru,它返回命令的退出状态。非零值表示存在错误。

passthru($command, $status);
if ($status != 0) {
    echo "There was an error executing the command. Died with exit code: $status";
}

有关这些退出代码的列表可帮助您调试正在发生的情况,请参阅具有特殊含义的退出代码 http://tldp.org/LDP/abs/html/exitcodes.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在php中使用wkhtmltoimage 的相关文章

随机推荐