Is there any way of taking a screenshot of a website in PHP, then saving it to a file?
Since PHP 5.2.2 it is possible, to capture a website with PHP solely!
imagegrabscreen — Captures the whole screen
<?php
$img = imagegrabscreen();
imagepng($img, 'screenshot.png');
?>
imagegrabwindow - Grabs a window or its client area using a windows handle (HWND property in COM instance)
<?php
$Browser = new COM('InternetExplorer.Application');
$Browserhandle = $Browser->HWND;
$Browser->Visible = true;
$Browser->Fullscreen = true;
$Browser->Navigate('http://www.stackoverflow.com');
while($Browser->Busy){
com_message_pump(4000);
}
$img = imagegrabwindow($Browserhandle, 0);
$Browser->Quit();
imagepng($img, 'screenshot.png');
?>
Edit: Note, these functions are available on Windows systems ONLY!
Not directly. Software such as Selenium have features like this and can be controlled by PHP but have other dependencys (such as running their java-based server on the computer with the browser you want to screenshot)
webkit2html works on Mac OS X and Linux, is quite simple to install and to use. See this tutorial.
For Windows, you can go with CutyCapt, which has similar functionality.
I used page2images. It is developed base on the cutycapt which is really fast and stable. If you do not want to spend too much time on the performance and configuration, you should use it. If you go to their website, you can find more details and sample PHP code.
You could do 2 things.
Use Puppeteer
You can use the PHP Puppeteer package to spin up a browser and take a screenshot.
Use an API
There are a lot of screenshot APIs. You could look at ScreenshotAPI.net for example. (Disclaimer I'm the creator of that API)
I set up finally using microweber/screen as proposed by @boksiora.
Initially when trying the mentioned link here what I got:
Please download this script from here https://github.com/microweber/screen
I'm on Linux. So if you want to run it, you may adjust my step follow to your environment.
Here are the step I did on my shell on DOCUMENT_ROOT
folder:
$ sudo wget https://github.com/microweber/screen/archive/master.zip
$ sudo unzip master.zip
$ sudo mv screen-master screen
$ sudo chmod +x screen/bin/phantomjs
$ sudo yum install fontconfig
$ sudo yum install freetype*
$ cd screen
$ sudo curl -sS https://getcomposer.org/installer | php
$ sudo php composer.phar update
$ cd ..
$ sudo chown -R apache screen
$ sudo chgrp -R www screen
$ sudo service httpd restart
Point your browser to screen/demo/shot.php?url=google.com
. When you see the screenshot, you are done. Discussion for more advance setting is available here and here.