How can i take screenshot of URL or URL to PNG on Share Hosting server using PHP

I am getting an issue taking screenshot of the URL using PHP

I tried ImageMagick with the following code. But no idea how can i use it.

import -window root images/my1.png

I replaced root with "my url"

I run above command using exec in PHP, but it not give me any output.

Can anyone give me any suggestion on this.

I would be very thankfull to you for this.

Thanks.

Answers


Try using webkit2png or similar like this:

webkit2png http:/google.com -o grab

which will give you these files:

-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


You don't need to use ImageMagick now. The other way for taking a screenshot using PHP without any additional server resource is by using Google's PageSpeed Insights API, which doesn't need any authentication of any kind. It's free and open right now, so make use of it.

Implementation details for the same are here: Generating Screenshots of URLs using Google's secret magic API.

Source Code

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

You can also do it using Client Side:

$(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" />

Need Your Help

Title Font doesn't change in Navigation Bar

ios uinavigationcontroller storyboard

I have a controller that is embedded in a Navigation Controller and I want to change the font of the title in the navigation bar. I want to use the storyboard, so it changes across the app (instead...

C# serial port driver wrapper class code and concept quality

c# .net serial-port driver wrapper

Would like to know from all you guys what do you think about my Serial Wrapper class. Had been a while I've been working with serial ports but never shared the code what somekind make me closed to my