Taking a PNG Screenshot of a Webpage from PHP + Apache on Ubuntu Server

While it may not be terrible useful, I decided that I had to know how to generate a screenshot of a webpage from my own webpage. Please note that THIS OPENS EXTRA SECURITY RISKS and SHOULD ONLY BE DONE ON AN ISOLATED SERVER where NOTHING OF VALUE CAN BE LOST

** If this is running on Ubuntu Server, obviously you need to install gnome to make this work:
sudo apt-get ubuntu-desktop
sudo shutdown -r now
export DISPLAY=:0.0

Install gnome-web-photo
sudo apt-get install gnome-web-photo

Login with www-data
sudo su www-data

** If you receive this error:
This account is currently not available.
You need to recreate user www-data:
service apache2 stop
deluser www-data
delgroup www-data
adduser www-data
service apache2 restart

Try to run gnome-web-photo
gnome-web-photo http://google.com ~/test.png
No protocol specified

** (gnome-web-photo:13000): WARNING **: Could not open X display

No protocol specified
Cannot open display: 
Usage: gnome-web-photo [--mode=photo|thumbnail|print] [...]

It looks like our little friend apache cannot connect to our xserver. That's an easy fix, we just need to add user www-data to the privileged users who can access the active display.

With an account that can access the display:
xhost +SI:localuser:www-data

With www-data account:
export DISPLAY=":0.0"
gnome-web-photo http://google.com ~/test.png

It should work now! You can use exec() and shell_exec() to now take browser shots from a PHP page. Go ahead, run your own browershots website.
exec("export DISPLAY=:0.0;gnome-web-photo http://google.com $dir/fuck.png");


1 comment:

  1. I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing. UC mini for android

    ReplyDelete