Saturday, 9 June 2018

How to Take Website Screen Shot From URL in PHP



Capturing of Screenshot of Web page is a one type of functionality which can be used for different purposed in our web application. If you want to put this feature in your web application then there are number of third party APIs are available on the web for capture screenshot of the website from URL. But if you want to make your own script which can take a screenshot of website from URL, then you can get this feature by using PHP with Google PageSpeed Insights API.

Mainly, This Google PageSpeed Insights API is used for calculate the speed of a web page, but we can also use this Google PageSpeed Insights API for capture a website screenshot from URL. In this post we will seen you how can we take a screen shot from URL of website by using Google PageSpeed Insights API with PHP script.

Take Screenshot of Website from URL


To take screenshot of Web page, Google PageSpeed Insights API required to pass following parameters.

  • url - In this parameters we have to pass URL of the website.
  • screenshot - This parameters screenshot=true is used for fetch screenshot data.

Below script you can see how to make a form for take website screenshot from URL which has be entered by use and after this it will load webpage screenshot on page.









Source Code - index.php



<?php

//index.php

$screen_shot_image = '';

if(isset($_POST["screen_shot"]))
{
 $url = $_POST["url"];
 $screen_shot_json_data = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$url&screenshot=true");
 $screen_shot_result = json_decode($screen_shot_json_data, true);
 $screen_shot = $screen_shot_result['screenshot']['data'];
 $screen_shot = str_replace(array('_','-'), array('/', '+'), $screen_shot);
 $screen_shot_image = "<img src=\"data:image/jpeg;base64,".$screen_shot."\" class='img-responsive img-thumbnail'/>";
}

?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>How to capture website screen shot from url in php</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <style>
  .box
  {
   width:100%;
   max-width:720px;
   margin:0 auto;
  }
  </style>
 </head>
 <body>
  <div class="container box">
   <br />
   <h2 align="center">How to capture website screen shot from url in php</h2><br />
   <form method="post">
    <div class="form-group">
     <label>Enter URL</label>
     <input type="url" name="url" class="form-control input-lg" required autocomplete="off" />
    </div>
    <br />
    <br />
    <input type="submit" name="screen_shot" value="Take a Screenshot" class="btn btn-info btn-lg" />
   </form>
   <br />
   <?php
   
   echo $screen_shot_image;
   
   ?>
  </div>
  <div style="clear:both"></div>
  <br />
  
  <br />
  <br />
  <br />
 </body>
</html>

16 comments:

  1. wow i really shocked webslesson, it's great thank you very much.

    ReplyDelete
  2. Is it possible to get the original size of a screenshot.

    ReplyDelete
  3. how can i save the screenshot?

    ReplyDelete
  4. Muy Bueno, muchas gracias por la informaciĆ³n, funciona perfecto

    ReplyDelete
  5. Take Website Screen Shot From URL in PHP :-
    where can i use this features ? can you describe this screenshot using URL benefits? we can take screen short on keyboard right than what purpose of this take screen shot using URL ???

    ReplyDelete
  6. Hello, Nice your videos. but how to screenshot fullpage .

    ReplyDelete
  7. Getting Warning: file_get_contents(https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=https://www.avarva.yonola.com&screenshot=true): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found

    ReplyDelete
  8. Getting a crack image even your demo isn't working showing cracked picture.

    ReplyDelete
  9. new get content url:
    https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://yoururl.com/

    ReplyDelete
  10. Replies
    1. You're right! It's v5 now: https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=

      Delete
  11. Hi, it is not working anymore.
    I thing the Array changed. But get it got work yet.

    ReplyDelete
  12. Here is the Change you need:
    $screen_shot = $screen_shot_result['lighthouseResult']['audits']['final-screenshot']['details']['data'];

    ReplyDelete