使用PHP的网站截图



现在正在研究通过 url 截取网站屏幕截图的小概念。通过引用很多网站使用wkhtmltoimage。当前正在使用Mac。 已成功安装 wkhtmltoimage,也尝试过

wkhtmltoimage www.google.com ggss.png

在终端中。它成功输出网站的屏幕截图。但是当我尝试使用 PHP 执行上述命令时,我没有看到输出图像或任何错误。下面是我尝试的代码

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

任何帮助将不胜感激

使用PHP截取屏幕截图而无需任何额外的服务器资源的另一种方法是使用Google的PageSpeed Insights API,它不需要任何类型的身份验证。它现在是免费的和开放的,所以要利用它。

相同的实现细节在这里:使用谷歌的秘密魔术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" />

尝试指定命令 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');
?>

Ok 最终通过浏览器通过 php 执行了 shell 命令。所以我想我可以分享可能对某人有用。所以真正的问题是许可。

因此,当我在终端输出上使用whoami命令时,是macuser。但是当我尝试在 php 输出中使用 shell_exec 执行命令时,没有人。这是因为 apache 没有许可。所以我做了以下操作来通过 PHP 执行 shell 命令

在/etc 中找到 httpd.conf 文件并找到

用户无人组无组

将"无"更改为要设置为要执行的用户的用户名。对我来说,它的用户 macuser

然后执行以下命令。(为了确保我在终端中将它们作为 su 执行)

  • cd/directory/of/htdocs(for me cd/Applications/XAMPP/xamppfiles/htdocs)
  • 查找 . -exec chown MacUser:MacUser {} \;
  • 光盘..
  • Chown Macuser HTDOCS

现在当我执行以下代码时,它可以工作

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

感谢巨石应用!

我建议使用像这样的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);

有关详细信息,请查看文档。

最新更新