⬆️ ⬇️

We make screenshots of sites

Wandering through the PHP documentation spaces, I stumbled upon two functions: imagegrabwindow and imagegrabscreen . They know how to take screenshots in Microsoft Windows.

Interested. It ended with the fact that the script was written generating a full screenshot of any site.

So we need:





The imagegrabwindow function provides a good example for use.

$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 - what size the browser window has opened, so we will get a screenshot .

In order to fix this, I had to write a script that additionally does the following things:



Coder



<?php

$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




')

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 efficient.

UPD: Read komenty, there are many links to how to do it on other engines and other systems.



PS It is checked only in virtualka on IE7.

PS IE itself I hate, I use FireFox.

Source: https://habr.com/ru/post/62965/



All Articles