【问题标题】:How can i take screenshot of URL or URL to PNG on Share Hosting server using PHP如何使用 PHP 在共享托管服务器上截取 URL 或 URL 到 PNG
【发布时间】:2015-09-30 16:38:41
【问题描述】:

我在使用 PHP 截取 URL 时遇到问题

我用以下代码尝试了 ImageMagick。 但是不知道怎么用。

import -window root images/my1.png

我将 root 替换为“我的网址”

我在 PHP 中使用 exec 运行上述命令,但它没有给我任何输出。

谁能给我任何建议。

我会非常感谢你。

谢谢。

【问题讨论】:

  • 你说的截取网址是什么意思?
  • 嗨 @Alan Machado,我想创建 URL 的 jpg 或 png 格式
  • 你可以同时使用curlimagecreate函数。
  • @AlanMachado,我使用 CURL 来检索内容或 HTML。但不知道如何将 CURL 用于 PNG 或 JPG。

标签: php imagemagick shared-hosting snapshot


【解决方案1】:

尝试使用webkit2png 或类似的:

webkit2png http:/google.com -o grab

这会给你这些文件:

-rw-r--r--   1 mark  staff     14754 30 Sep 18:46 grab-thumb.png
-rw-r--r--   1 mark  staff     68830 30 Sep 18:46 grab-full.png
-rw-r--r--   1 mark  staff     13552 30 Sep 18:46 grab-clipped.png

【讨论】:

  • 感谢@Mark,但它是一个命令行工具。不适用于 PHP。这是为python制作的
  • @SunnyPatial 尝试了另一种方法。一探究竟。 :)
【解决方案2】:

您现在不需要使用 ImageMagick。另一种使用 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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-28
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2013-07-18
    • 2023-03-10
    相关资源
    最近更新 更多