Use whtmltoimage in php

When I use wkhtmltoimage in the terminal, it works well. But this has some problems when used in php. This is the problem: php code:

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

It works. And when I try to use the same command in the terminal, it works well too.

But when I try to use other links, this will not work.

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

It works. But when I try to use the same command in the terminal. He works! Plz help me.

+4
source share
2 answers

I assume it is passthrudisabled in the file php.inithat is used for the web server for security reasons. Try the following code:

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

If it is disabled, you cannot do anything if you cannot edit the php.ini file.

: , , - , . :

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

Edit:

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

, , .

+4

PHP exec .
wkhtmltoimage:
: 1.html 1.bat 1.php 3 htdocs.

1.bat:

cd\
cd c:\program files\wkhtmltopdf\bin
wkhtmltoimage http://localhost/1.html C:\howzit.jpg
rem (you can write to C: but not write to htdocs folder.)

1.php:

<?php
    exec("1.bat");
    echo "done c:\howzit.jpg";
?>

PS - localhost:

<img src = "c:\howzit.jpg">
<img src = "file:///c:/howzit.jpg">
But you will find the new jpg file in your C: directory (WinXP)
+1

All Articles