使用PHP的网站截图

问题描述 投票:0回答:3

正在研究一个由url截取网站截图的小概念。通过引用很多使用wkhtmltoimage的网站。目前正在使用Mac。已成功安装wkhtmltoimage,也试过了

wkhtmltoimage www.google.com ggss.png

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

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

任何帮助,将不胜感激

php screenshot wkhtmltoimage
3个回答
2
投票

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

相同的实现细节在这里:Generating Screenshots of URLs using Google's secret magic API

源代码

<?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" />

1
投票

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

1
投票

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

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

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

用户没有组nogroup

将nobody更改为您要设置为要执行的用户的用户名。对我来说,它是用户的主力

然后执行以下命令。 (为了确保我在终端中将它们作为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

© www.soinside.com 2019 - 2024. All rights reserved.