Idea: To capture a website screenshot and save it as image file.
Why PHP is so not an option for me?
PHP has imagescreengrab () function that captures the current screen and saves it to a png file but the disadvantage is that it does not accept the url parameter in case if you need to remotely capture the webpage screenshot. One other thing is php will support this function only on Windows Operating system.
Capture webpage screenshot
While searching for the alternate to imagescreengrab () function I found phantomjs library as the perfect option. It supports the remote website screenshot with a simple shell command and saves it to a image file.
Moreover the phantomjs library does not require any browser since the library itself behaves as the headless webkit browser. So initially you need to setup the phantomjs library on your server. After completing the setup of phantomjs you can able to generate the website screenshots by executing a shell command in Linux server. But the purpose of this task to have the ability to generate webpage screenshot based on online request so I need to have webpage that accepts the URL parameter and execute the backend shell command built using phantomjs and display the generated file back to the screen. For this reason I chose PHP for the server side processing for the input requests.
Also Read : Face detection using PHP
Step 1: Build and deploy the PhantomJS
We created a new server instance on amazon ec2 and deployed the phantomjs library.After this you can execute the shell command on the terminal that generates the image file for you.
Step 2: Install Apache and PHP on your server ( if you haven’t done already)
Step 3: Execute shell command from PHP
You can execute the shell command from php with the shell_exec() function.
Executing the shell command through php is a bit tricky step because many servers would block such execution except for the root user.Before executing the shell command you need to make sure it runs as root user.
Login to your server terminal and add the following command to provide shell execution access
This command will let you edit the sudoers file that has the access permission for the shell commands. At the end of the file you need to add the below command and save it.
www-data ALL=NOPASSWD: ALL
This will make sure the apache process has all access to run the shell command.
Step 4: Create a HTML form to accept the webpage url and post it the php file which in turn call another
Since I am using amazon EC2 server for phantomjs I needed two php files. You can use it as per your need.
Demo of the screenshot application
I m trying to capture screenshot on demo, but, it’s not working.
it’s just showing progress bar
It is working now.I had to restart http service.
Oh nice i just check and it’w working.
is source code is available to download
This is using phantomjs which is open source.you can download the source code from github.I just implemented the basic feature.
I configured both casperjs and phantomjs and my other scripts working fine but capture had problem. Thanks for your solution.
Will this work for both Desktop and Web application?
@Mubashar – Configuring casperjs and phantomjs isn’t your solution – it’s something else which is blocking you!!
Do you get any error messages in console?