As part of a project I started a month or so ago, I wanted the ability to SSH into my iMac and take a photo from the iSight camera. To do this I came across an open-source project called Imagesnap.
Thirst for Power
This satisfied my problem for a little while, but before long I wanted more functionality. This curiosity led me to creating a PHP script to call Imagesnap and thus give me a web interface to the iSight camera. Everything seemed to work, my form was submitting and there were no PHP errors except there was no file being created. I checked permissions, everything seemed fine. I jumped into the terminal in and ran imagesnap manually, everything worked fine. I Googled for days but couldn't find anything, I even scoured the source code of imagesnap to try to get some insight to what was going wrong however to no avail.
The next piece of the puzzle came whilst playing around Imagesnap itself. When Imagesnap is executed as anyone but a regular user it does not find any devices (cameras). This means when apache was running the PHP script, that in turn was executing Imagesnap, it was getting run as the apache user and so couldn't find any devices to take the photo with.
Rise from the Ashes
The fix is to call Imagesnap as a regular user. The easiest way to do this is to set its owner to be a regular user and then set its UNIX permissions to have setUID on execution using chmod.
setuid and setgid are Unix access rights flags that allow users to run an executable with the permissions of the executable's owner or group.Wikipedia
$ chown your_username imagesnap $ chmod 4755 imagesnap
Hopefully this will be useful for anyone else trying something similar.