Convert URL to screenshot (script)

2020-02-09 05:41发布

问题:

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.

回答1:

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.



回答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.



回答3:

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.

*/
?>


回答4:

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



回答5:

<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



回答6:

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



回答7:

There is also http://webshot.mine.sk/ free with no limits, multiple APIs (js, jquery, php)