0

現在、URL で Web サイトのスクリーンショットを撮るという小さなコンセプトに取り組んでいます。wkhtmltoimage を使用している多くの Web サイトを参照してください。現在マック使用。wkhtmltoimage のインストールに成功しました。また、試してみました

wkhtmltoimage www.google.com ggss.png

ターミナルで。Webサイトのスクリーンショットを正常に出力します。しかし、PHP を使用して上記のコマンドを実行しようとすると、出力画像やエラーが表示されません。以下は私が試したコードです

<?php
$output = shell_exec('wkhtmltoimage http://www.bbc.com bbc.jpg');
?>

どんな助けでもいただければ幸いです

4

4 に答える 4

2

サーバー リソースを追加せずに PHP を使用してスクリーンショットを撮るもう 1 つの方法は、Google のPageSpeed Insights APIを使用することです。この APIは、いかなる種類の認証も必要としません。無料で公開中ですので、ぜひご活用ください。

同じ実装の詳細は、Google のシークレット マジック API を使用して URL のスクリーンショットを生成する にあります

ソースコード

<?php
  // Creating a proxy to use GET request to hit the Google Page Speed API and receive a screenshot.
  // Check if the URL parameter for our proxy is set.
  if (!empty($_GET['url'])) {
    // Make sure the given value is a URL.
    if (filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
      // Hit the Google PageSpeed Insights API.
      // Catch: Your server needs to allow file_get_contents() to make this run. Or you need to use cURL.
      $googlePagespeedResponse = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?screenshot=true&url={$_GET['url']}");

      // Convert the JSON response into an array.
      $googlePagespeedObject = json_decode($googlePagespeedResponse, true);

      // Grab the Screenshot data.
      $screenshot = $googlePagespeedObject['screenshot']['data'];
      // Replace Google's anamolies.
      $screenshot = str_replace(array('_','-'), array('/','+'), $screenshot);

      // Build the Data URI scheme and spit out an <img /> Tag.
      echo "<img src=\"data:image/jpeg;base64,{$screenshot}\" alt=\"Screenshot\" />";
    } else {
      // If not a valid URL.
      echo "Given URL is not valid.";
    }
  } else {
    // URL not set.
    echo "You need to specify the URL.";
  }
?>

クライアント側を使用して行うこともできます。

$(function () {
  // Get the URL.
  var url = "https://praveen.science/";
  // Prepare the URL.
  url = encodeURIComponent(url);
  // Hit the Google Page Speed API.
  $.get("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?screenshot=true&strategy=mobile&url=" + url, function (data) {
    // Get the screenshot data.
    var screenshot = data.screenshot;
    // Convert the Google's Data to Data URI scheme.
    var imageData = screenshot.data.replace(/_/g, "/").replace(/-/g, "+");
    // Build the Data URI.
    var dataURI = "data:" + screenshot.mime_type + ";base64," + imageData;
    // Set the image's source.
    $("img").attr("src", dataURI);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Hard Coded Screenshot of my Website:</h1>
<img src="//placehold.it/300x50?text=Loading+Screenshot..." alt="Screenshot" />

于 2018-07-16T13:02:43.883 に答える
1

最後に、ブラウザ経由でphpを介してシェルコマンドを実行しました。だから私は共有できると思ったので、誰かに役立つかもしれません. したがって、本当の問題は許可です。

したがって、端末出力でwhoamiコマンドを使用すると、macuserでした。しかし、php出力でshell_execを使用してコマンドを実行しようとすると、誰もいませんでした。それは、apacheに許可がなかったからです。そこで、PHP経由でシェルコマンドを実行するために次のことを行いました

/etcでhttpd.confファイルを見つけて見つけます

ユーザー nobody グループ nogroup

nobody を、実行したいユーザーとして設定したいユーザー名に変更します。私にとっては、そのユーザー macuser

次に、次のコマンドを実行します。(ターミナルでsuとして実行したことを確認するため)

  • cd /directory/of/htdocs (私にとっては cd /Applications/XAMPP/xamppfiles/htdocs)
  • 探す 。-exec chown macuser:macuser {} \;
  • CD ..
  • chown macuser htdocs

次のコードを実行すると動作するようになりました

<?php
$output = shell_exec('/usr/local/bin/wkhtmltoimage http://www.google.com /Applications/XAMPP/xamppfiles/htdocs/demotasks/google.jpg');
?>

boulderappsに感謝し ます。

于 2015-02-05T05:40:46.487 に答える
1

command へのフルパスを指定してみてくださいwkhtmltoimage

編集

コマンドwkhtmltoimageのフル パスを取得するには、次のコマンドを実行します。whereis wkhtmltoimage

したがって、次のようにする必要があります。

<?php
$output = shell_exec('/full_path_to_wkhtmltoimage_here/wkhtmltoimage http://www.bbc.com /full_path_to_img_here/bbc.jpg');
?>
于 2015-02-04T09:47:27.287 に答える
0

このようなAPIを使用することをお勧めします

たとえば、アカウントを作成すると、API を呼び出すことができます。

// The parameters.
$token = 'YOUR_TOKEN';
$url = urlencode('https://github.com');
$width = 1920;
$height = 1080;
$output = 'image';

// Create the query URL.
$query = "https://screenshotapi.net/api/v1/screenshot";
$query .= "?token=$token&url=$url&width=$width&height=$height&output=$output";

// Call the API.
$image = file_get_contents($query);

// Store the screenshot image.
file_put_contents('./screenshot.png', $image);

詳細については、ドキュメントを参照してください。

于 2019-10-28T21:58:40.407 に答える