2

How can I create a screenshot of a website using PHP and the GD library.

Ross
  • 46,186
  • 39
  • 120
  • 173

2 Answers2

5

While you might be able to do something with imagegrabscreen or imagegrabwindow you'd only be able to use it on a Windows box, and even then it would be tricky.

You'd have to open a browser window to the specified url (you could do that with exec) and grab a screenshot using the aforementioned methods.

Here's an example from the manual entry for imagegrabwindow:

<?php
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://www.libgd.org");

/* Still working? */
while ($browser->Busy) {
    com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
imagedestroy($im);
?>
Ross
  • 46,186
  • 39
  • 120
  • 173
  • Hi Ross, the code above gives following error/Warning PHP Warning: Module 'gd' already loaded in Unknown on line 0 PHP Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `InternetExplorer.Application': Access is denied. ' –  Mar 10 '09 at 05:22
  • I'm having trouble even getting imagegrabscreen to work. As I said, I only copied that code from the manual. All I can suggest is that you follow the note on http://php.net/imagegrabscreen (assuming you're using Apache). – Ross Mar 10 '09 at 17:23
  • (Note that on Vista enabling Apache desktop support, restarting the service and the server still does not work) – Ross Mar 10 '09 at 17:24
1

Website is rendered on client side, while PHP and GD are server side. You may also check this website out. Hope it helps.

Stiropor
  • 453
  • 3
  • 7
  • Thanks for the link, it works, expect that HTML5/canvas is not implemented. [This solution](http://stackoverflow.com/a/11736196/903186) works better. – Ruut Apr 13 '14 at 15:15