Search code examples
phplinuxapachebashscreenshot

PHP Executing Bash Script returning just black screenshots


You are probably familliar with the website thumbnail services out there like websnapr.com e.g.

I'm currently working on something like it with different purposes, so obivously I needed a server which would launch a webbrowser with an url take a screenshot and go to the next one.

This is the bash script that I execute by bash script.sh 'http://google.nl' /var/www/images/google.png

#! /bin/bash
export DISPLAY=":4"
killall firefox-bin
/usr/bin/firefox --display :4 "$1" &
/bin/sleep 20
xte "key F11"
/bin/sleep 3
/usr/bin/import -window root -display :4 "$2"
/bin/sleep 3
killall firefox-bin

and I have an Xvfb running by 'Xvfb :4 -screen 0 1024x768x24 &'

The script works fine while running via ssh or just the terminal on the standard user, but when I try to execute it through php like:

exec('bash /var/scripts/test.sh "http://google.nl" /var/www/char.png');

all I get is a fully black screenshot of 1024x768, when looking in apache's error logs I see that it can't killall firefox when I have it running under the main user (permission not permitted) but even when it's closed it can't start one (not giving an error).

Any idea what's wrong? Ideally since it works on my main user it would just want to be able to execute it as that user instead of www-data user, security isn't an issue since there will be no direct connection to that server (only via another server on the same network).


Solution

  • It's not surprising that an application running from Apache cannot access the X11 as nicely. I'd use xvfb + xvfb-run.

    On the other hand, while playing with xte in extreme cases is fine, it's a bit hacky when you have clean solutions; try projects like cutycapt or similar.