php - Script to take screenshot of webpage running on localhost
Get the solution ↓↓↓Is there any script which enables automatic screenshot of this webpage running on a server on a daily basis and store the captured images ?
Answer
Solution:
First of all lets define the task and understand its boundaries. Because there is no simple and easy solution to address your question.
To capture the screenshot of the web page, first it must be rendered. This is quite complicated process. You tagged a PHP, so shortly you can not do it simply using only PHP. I would recommend you to read briefly how the rendering of web pages work. This article is a great source to learn about it: https://www.html5rocks.com/en/tutorials/internals/howbrowserswork/
Hence, first it is needed some combination of "services" which will render the page, then capture it as a bitmap (it does not matter which graphic format). Then you can obtain it using PHP (REST or any other suitable way). Roughly saying you need some browser-like system (or exactly the browser itself) which will render the page and return bitmap.
If you are looking for some simple practical solution without burden you have several solutions:
For getting screenshot from any remote page you can use paid API https://thumbnail.ws/ . It has a Free option with limits.
For getting screenshot and other related thumbnail data you can use Google's Page Speed API. The example code can be found at https://gist.github.com/ajdruff/e6b69e3eb5a3bc1dc081
Use some available extensions for Google Chrome or Firefox (you can make your own using JavaScript) then use the data from it.
Answer
Solution:
There are many packages for that purpose out there one of them for example is Screen
Here's an example of how to use it:
Assuming you've already installed it
require './vendor/autoload.php';
use Screen\Capture;
$url = 'https://example.com'; // webpage you want to capture
$screenCapture = new Capture($url);
$screenCapture->save('./test'); // test is the name of the screenshot (default type is 'jpg')
Answer
Solution:
In order to correctly take screenshots of webpages, you need to first make sure that the page rendered correctly and the most convenient way is to use real browsers. As others mentioned, there are different options:
you can use a paid API to do that (wasting money IMHO).
You can write your own code to do that (which is not necessarily easy and safe).
You can use ready-to-use tools (mostly command-line tools)
- cutycapt (cutycapt --url= < URL > --out= < filename >)
- firefox (firefox -screenshot < filename > < URL >)
- wkhtmltoimage (as part of wkhtmltopdf tool)
- Chrome browser ( google-chrome --headless --disable-gpu --screenshot < URL>)
- You can run your own server if you are doing screenshot in bulk (for example, https://github.com/filovirid/screenshot_server)
Answer
Solution:
There is no such script which automatically takes you the screenshot unless you are developing a software for it. The alternative thing which you can do is to write a python script to scrape the data in the webpage daily and store it in a file or else you can use selenium tool for this purpose.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: videoxxx
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.