Use screencapture with PHP exec
All credit goes to patrix.

The problem stems from OSX preventing php script from executing screencapture command. OSX has no problem if you run your script manually, but if it is triggered as a response to a GET or POST request, than it raises the red flag.

To solve this 3 steps are needed:


Add permission to the user that is running httpd (and php) to execute sudo launchctl without password. In my case it is daemon. I got it from Activity Monitor. Edit /etc/sudoers with

daemon ALL=(ALL) NOPASSWD: /bin/launchctl

get the PID of loginWindow. you can use ActivityMonitor for that. PID is subjected to changes so to bullet proof yourself use:

pgrep loginwindow

update your screencapture command:

sudo launchctl bsexec PIDNUMBERHERE screencapture [options] path/image.png





It might be worth mentioning that step 1 adds a slight security risk and probably shouldn't be used in an internet-facing environment (especially with a PHP solution where everybody can easily read the source code).