Creating screenshots with PHP

Written by Lucas, on Sat 17 November 2007, 13:15:19

Ever wondered how the site browser shots worked? I bet they're using a sort of custom COM object or PHP extension to create screenshots or maybe an other scripting language, but today I saw two nice PHP functions which can achieve the same result: imagegrabscreen(); and imagegrabwindow();.

They're added in PHP version 5.2.2, and only work on windows. I'm no windows user anymore, so I can't test it. To view an example, click 'Read more'

imagegrabwindow() example

  1.  
  2. <?php
  3. // Start Internet Explorer
  4. $browser = new COM("InternetExplorer.Application");
  5.  
  6. // Get the window handle
  7. $handle = $browser -> HWND;
  8.  
  9. // Be sure the browser is visible
  10. $browser->Visible = true;
  11.  
  12. // Navigate to a site
  13. $browser->Navigate("http://www.return1.net");
  14.  
  15. // Still loading?
  16. while ($browser -> Busy)
  17. {
  18.     com_message_pump(4000);
  19. }
  20.  
  21. // Take a screenshot of the current window
  22. $im = imagegrabwindow($handle);
  23.  
  24. // Quit the browser
  25. $browser->Quit();
  26.  
  27. // And show the image
  28. header("Content-Type: image/png");
  29. imagepng($im);
  30.  


imagegrabscreen() example

  1.  
  2. <?php
  3. $im = imagegrabscreen();
  4.  
  5. header("Content-Type: image/png");
  6. imagepng($im);
  7.  


Tag Tags: GD php
Comments (4)