1

Are there any free utilities that can be used to take screenshots of webpages and websites on centos and that can be run through php.

Thanks

artless noise
  • 21,212
  • 6
  • 68
  • 105
Vish
  • 4,508
  • 10
  • 42
  • 74

2 Answers2

7

There are various commandline utilities available. Most start one of the browser engines in headless X11 and take a screenshot then. A particular common one is khtml2png which can be used from php like this (not sure if there is a precompiled version for CentOS):

exec("khtml2png --width 800 --height 600 http://google.com/ img.png");

A few more are listed here: Command line program to create website screenshots (on Linux)

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • I am unable to run this command through php. It has to be run through terminal. I want to automate the thumbnail creation through PHP. exec is enabled on the php, but it is just this particular command. I have a dedicated server so I can install applications, so you can suggest those to me. But the application should be able to ran through PHP. – Vish Mar 29 '11 at 16:38
  • Do you get a specific error message? (Use `2>/dev/stdout` to capture it). If you cannot start this command from exec(), then the other options (firefox & capturing X11 root window) won't work much better. -- The other commonly recommended alternative would be `wkhtml2pdf` http://code.google.com/p/wkhtmltopdf/ (which then however requires a second step to transform it into a PNG image again..) – mario Mar 29 '11 at 16:50
  • if you dont mind could you please give me steps for installing wkhtml2pdf and its php counterpart. thanks...:) – Vish Mar 29 '11 at 16:52
  • Just tried it out. It was extremely simple. I took this download http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltoimage-0.10.0_rc2-static-amd64.tar.bz2 and extracted it. It's a singe executable, little fuss. Then just invoke it with `exec("./wkhtmltoimage-amd64 http://google.com/ google.png");` and it should generate a PNG. (This is a special version which generates images instead of PDF screenshots. Cool.) – mario Mar 29 '11 at 16:57
0

I don't think that's possible because PHP doesn't render websites like a browser does.

EDIT: However, you could save each page's raw unrendered HTML using a PHP cURL script.

eg:

$websites[] = 'http://google.com'; 
$websites[] = 'http://stackoverflow.com';
$websites[] = 'http://msn.com'; 
$websites[] = 'http://microsoft.com';

foreach ($websites as $site)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $site);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    curl_close($curl);

    if(!empty($data)) 
    {
        savePageToFile($data); //placeholder, not real function
    }
}
PiZzL3
  • 2,092
  • 4
  • 22
  • 30
  • 1
    While this info is accurate, it doesn't have much to do with taking a screen shot of a rendered web page. – Klinky Mar 28 '11 at 23:57