gpt4 book ai didi

php - 在 php 中使用 wkhtmltoimage

转载 作者:可可西里 更新时间:2023-10-31 22:12:58 25 4
gpt4 key购买 nike

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

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

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

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

编辑:

如果启用了 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";

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

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

有关这些退出代码的列表以帮助您调试正在发生的事情,请参阅 Exit codes with special meanings

关于php - 在 php 中使用 wkhtmltoimage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14617720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com