In this post I will show you how easy it is to set up a simple PHP scripted webcam that returns an image from the Raspberry PI camera on demand.
I will assume you have the Raspberry PI, Camera and Debian with a web server running with PHP. (There are many tutorials out there offering this basic setup!)
1. Create a simple PHP script to return the image from the camera every time it is called: (Create this as “embed.php” in your public www folder:
<?PHP system('sudo raspistill -hf -w 512 -h 320 -o /var/www/imageembed.jpg -t 0'); $filename = "imageembed.jpg"; $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); fclose($handle); echo $contents; ?>
2. Now we can use this very simple code to embed the image into our webpage:
<html> <body> <center><h3><u>Live Webcam</u></h3></center><br> <img src="http://insert-your-web-address.com/embed.php"> </body> </html>
You can embed this code directly on the PI or on some remotely hosted webspace!
If you find your apache web service is unable to run the raspistill command you may need to modify the www-data apache user to allow it to run the command:
echo 'SUBSYSTEM=="vchiq",GROUP="video",MODE="0660"' > /etc/udev/rules.d/10-vchiq-permissions.rules
usermod -a -G video www-data