Take screenshot from external website Take screenshot from external website curl curl

Take screenshot from external website


I love php, but for screenshots I found that using phantomjs provide the best results

Example file screenshot.js

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

Then from the shell:

phantomjs screenshot.js 

Or from php:

exec("phantomjs screenshot.js &");

The goal here is to generate the js file from php.

Result in a file called out.png in the same folder. This is a full height page screenshot.

Example output

We can also take good captures with Firefox from the command line. This require X anyway.

firefox -screenshot test.png  http://www.google.de --window-size=1280,1000

Example output


Not in pure php. Nowadays major number of sites generates content dynamically with js. It can be rendered only by browsers, but good news - there is something called phantomjs - browser without UI. It can do job for You, even they have working example in their tutorials which I succesfully implemented few years ago with small knowledge of javascript. There is alternative library called a nightmarejs - I know this only from friends opinion which says that it's simpler than phantom, but I won't guarantee to You that it won't be a nightmare - personally I hadn't use it.


It is possible, but if you want an screenshot you need something like a browser that render the page for you. The iframe approach go in that way. But iframe is the page itself. If you want a .jpg , .png or something like that, the best way in my opinion is using wkhtmltoimage. https://wkhtmltopdf.org/.The idea is that you install Qt WebKit rendering engine in your server, just as you install a browser in your server, this render the page and save the final result in a file. When some user submit a url, you pass it as argument to wkhtmltopdf then you could have an image of that url. The basic use could be somethig like

wkhtmltoimage http://www.example1.com /var/www/pages/example1.jpg

you should run that statement in bash, from php could be:

 <?phpexec('wkhtmltoimage http://www.example1.com /var/www/pages/example1.jpg');?>

Keep in mind that wkhtmltoimage execute css, javascript.., everything. Just like browser.