7

There is the URL of page on the Internet. I need to get a screenshot of this page (no matter in which browser).

I need a script (PHP, Python (even Django framework)) that receives the URL (string) and output screenshot-file at the exit (file gif, png, jpg).

UPD:

I need dynamically create a page where opposite to URL will be placed screenshot of the page with the same URL.

Kalinin
  • 2,489
  • 8
  • 37
  • 49

6 Answers6

6

Why do you need a script when you can use a service from another site?
Check for example what I am using: WebSnapr http://www.websnapr.com/
Or check http://www.google.ro/search?ie=UTF-8&q=website+thumbnail if something else fits your request.

CristiC
  • 22,068
  • 12
  • 57
  • 89
  • I need dynamically create a page where opposite to URL will be placed screenshot of the page with the same URL. – Kalinin Sep 28 '10 at 10:39
  • 1
    Those services ussualy returns a thumbnail of the corresponding URL. You can request such a job in the background (or on saving data) and you can store it in a database (to reuse it latter) or just display it (I would go for the first one). – CristiC Sep 28 '10 at 10:44
  • Can this be done automatically (I have about 1000 sites (url)) – Kalinin Sep 28 '10 at 11:28
  • 1
    Do you mean bulk? Choose a service, signup if necessary, and using a loop cycle thru all your urls, make the service request and store the generated thumb. – CristiC Sep 28 '10 at 11:32
  • You can run a script any number of times you like, without paying anyone anything. There's no third party in between to aggregate metadata with a script. The script keeps working years after the service dies (as is the case with websnapr.com). Lots of reasons to use a script over a service. This looks like a rather young, but useful tool for the job: https://github.com/maaaaz/webscreenshot – Tomislav Nakic-Alfirevic Jun 04 '21 at 21:12
2

PhantomJS is a better option for generating screenshot from URL. The following script demonstrates the simplest use of page capture. It loads the Github homepage and then saves it as an image, github.png. Code

var page = require('webpage').create();
page.open('http://github.com/', function() {
  page.render('github.png');
  phantom.exit();
});

To run this example create a new file called github.js. Copy and paste the above code into the github.js file. In the commandline, run this newly created script with PhantomJS:

phantomjs github.js

There a lot of projects for generating screenshots using PhantomJS. Pageres generates reliable screenshots and its based on NodeJS and PhantomJS.

Ashish Gupta
  • 2,574
  • 2
  • 29
  • 58
  • Using the same for linux but not working, Can you specify what changes need to be made to make it working in Linux server. – zulfi Jul 11 '16 at 20:41
  • @zulfi It should work on linux. What is not working ? Try Pageres, that's simple. – Ashish Gupta Jul 17 '16 at 20:45
1

Solution using Google Page Speed - tested & working.

//SOLUTION 1

<?php
$link = "http://example.com";
$googlePagespeedData = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$link&screenshot=true");
$googlePagespeedData = json_decode($googlePagespeedData, true);
$screenshot = $googlePagespeedData['screenshot']['data'];
$screenshot = str_replace(array('_','-'),array('/','+'),$screenshot); 
$show_link = "<a href='$link'><img src=\"data:image/jpeg;base64,".$screenshot."\" /></a>";
echo $show_link;

//SOLUTION 2

$name = 'test';
$googlePagespeedData = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=$link&screenshot=true");
$googlePagespeedData = json_decode($googlePagespeedData, true);
$screenshot = base64_decode($googlePagespeedData['screenshot']['data']);
$data = str_replace('_','/',$googlePagespeedData['screenshot']['data']);
$data = str_replace('-','+',$data);
$decoded = base64_decode($data);
file_put_contents('myfolder/'.$name.'.jpg',$decoded);
$file_name = "$name.jpg";

/*
-- IMPORTANT INFORMATION -- READ BELOW --

Choose how to proceed!
1. Use the above to display screenshots of links = longer processing time for multiple links.
2. Save image to a file, reference the saved image = more disk space needed if multiple links.

Note the trade off between processing time and disk space, if you're on a shared hosting platform with a small disk space limit and envisage or already have a lot of users (forums beware) you may want to consider a bigger hosting plan or even a dedicated server.

*/
?>
0
<img src='http://zenithwebtechnologies.com.au/thumbnail.php?url=www.subway.com.au'>

Pass the url as argument and you'll get the image for more details check this link http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html

Mee
  • 59
  • 1
  • 1
0

If you are family with Python, you can use PyQt4. This library supports to get screenshot from a url.

0

You can use, as I do, the shotbox API

It's in french, but still, quickly:

  • Use h t t p://add.shotbot.net/k=key/url where key is your API key and url... the page you want as a screenshot
  • Use h t t p://static.shotbot.net/md5url/format.jpg or h t t p://cache.shotbot.net/s=format/url where format can be 80 (80x60), 92 (92x69), 120 (ascreen 120x90), 160 (160x120), 240 (240x180), 320 (320x240), 1024 (1024x768)

To get your API key : http://translate.google.fr/translate?hl=fr&sl=fr&tl=en&u=http%3A%2F%2Fwww.shotbot.net%2Fcreer-un-compte-webmaster.php

Shikiryu
  • 10,180
  • 8
  • 49
  • 75