Take screenshots of sites

    Wandering through the open spaces of the PHP documentation, I accidentally stumbled upon two functions: imagegrabwindow and imagegrabscreen . They can take screenshots in Microsoft Windows.
    Interested in. The end of the matter was that a script was written generating a full screenshot of any site.
    So we need:
    • Internet Explorer ActiveX component and its documentation .
    • PHP 5.2.2+


    The imagegrabwindow function gives a good use case.
    $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);

    * This source code was highlighted with Source Code Highlighter.


    But it has a big drawback - how big the browser window opened, so we get a screenshot .
    In order to fix this, I had to write a script that additionally does the following things:
    • Expands the browser window in full screen in full screen mode
    • Disables the status bar.
    • Scrolling overwrites
    • Takes the required number of page screenshots by scrolling the page and combines them

    Kodjara


    $browser = new COM("InternetExplorer.Application");
    $browser->Visible = true;
    $browser->Fullscreen = true;
    $browser->StatusBar = false;

    $browser->Navigate("http://www.habrahabr.ru");
    while ($browser->Busy)
      com_message_pump(4000);

    $handle = $browser->HWND;  
    $screenWidth = $browser->Width;
    $screenHeight = $browser->Height;
    $documentHeight = $browser->Document->body->scrollHeight;

    $scrollWidth = 20;
    $scrollHeight = 20;
    $testPartSize = 10*1024;

    $im = imagecreatetruecolor($screenWidth - $scrollWidth, $documentHeight);
    for($top = 0; $top < $documentHeight; $top += $screenHeight)
    {
     $browser->Document->documentElement->scrollTop=$top;
     while ($browser->Busy)
      com_message_pump(4000);
     echo $browser->Document->documentElement->scrollTop.PHP_EOL;
     
     for($i = 0; $i < 5; $i++)
     {
      $part = imagegrabwindow($handle, 0);
      $dark = imagecolorallocate($part, 0, 0, 0);
      imagefilledrectangle($part, $screenWidth - $scrollWidth, 0, $screenWidth, $screenHeight, $dark);
      $testFile = sprintf("screenshot_%05d.png", $top);
      imagepng($part, $testFile, 9, PNG_ALL_FILTERS);
      clearstatcache();
      if(filesize($testFile) > $testPartSize)
       break;
       
      echo "Bad part, name: {$testFile}, try to generate again.".PHP_EOL;
     } 
     
     imagecopy(
      $im, $part,
      0, $top,
      0, (!$top || ($top + $screenHeight) < $documentHeight) ? 0 : $screenHeight - $scrollHeight - $documentHeight % $screenHeight,
      $screenWidth - $scrollWidth, $screenHeight);  
     imagedestroy($part);  
    }
    imagepng($im, "screenshot.png", 9, PNG_ALL_FILTERS);
    imagedestroy($im);

    $browser->Quit();

    * This source code was highlighted with Source Code Highlighter.


    Code Notes

    • Sometimes, for some unknown reason, a screenshot is taken, and there is a black screen, in order to eliminate this, a recorded piece is written to a file with zipped scrolling, and then the file size is checked. If the file is small, you probably need to reshoot.
    • If you want to quickly test, you can use the following command line:
      php -d extension_dir=c:\php\ext\ -d extension=php_gd2.dll ie.php
      where ie.php is the script above.
    • You can run it from under Apache, but for this you need to change the security settings. The documentation for these functions says what to do.


    And as in other browsers


    For FireFox I found the Embedded Mozilla section, unfortunately it requires coding, but when writing a full-fledged service it will turn out much more efficiently.
    UPD: Read comments, there are many links on how to do this on other engines and in other systems.

    PS It is checked only in a virtualka on IE7.
    PS IE itself I hate, I use FireFox.

    Also popular now: