Author: Vivek moyal
Last month viewers: 929
Categories: PHP Tutorials
This article is a tutorial that explains how to take picture snapshots using a Webcam to be uploaded to a PHP application.
Webcams are devices that are present in most computers and mobile devices. Usually they are used to capture video of the user but they can also be used to capture still pictures.
This article presents you a solution that you can use to implement in any PHP Web site.
Since Webcams can take photos of any pictures that a computer or a mobile device can take, this solution can work to take any kind of photos online and save them to a server running PHP.
Basically Webcams are devices that generate image or video data. When accessed from a browser using an API, they can pull image or video data as if they are reading files.
Accessing video or images like files makes it easier to process it because Web pages support file uploading using HTML forms.
So all it is necessary to capture Webcam pictures is to connect Webcam data streams to file uploading forms.
The first thing you need is your PHP editor or IDE, a Webcam, MySQL and a cup of coffee. The PHP editor is of course for coding your PHP scripts, Webcam to take the image, MySQL for saving it in a database and Coffee is for your refreshment. :-)
To create our capturing applications we need to use three files:
- Web page for showing the webcam image
- Script to handle the image upload request
- A PHP class script to take to the actual handling of the image and store it in a database
1. Web Page HTML: Take Picture From Camera
With this file we will show our Webcam screen on our PHP page.
Here we will use the JPEG Camera script for the webcam functions to take the snap and save it.
Below the form we will put our webcam window to show the webcam screen capture.
<div id="camera_info"></div> <div id="camera"></div><br> <button id="take_snapshots" class="btn btn-success btn-sm">Take Snapshots</button>
Now below the webcam screen we will use the buttons to take the snapshot.
<form> <input type="button" value="Take Snapshot" onClick="take_snapshot()"> </form>
In the code above we show the screen and get the image from webcam. But still we have to save it somewhere, so lets save this image in a server folder.
2. PHP Capture Image from Camera Script to Handle the Upload
After showing the Webcam screen in PHP page we take the snap from clicking the button. Then it the image needs to be saved to a server directory of our project.
For this we can use a PHP class that I wrote in a script named webcamClass.php to save the snapshot image and return success. The action.php script is used to call the class.
The function getNameWithPath() defines the image path and name for the uploaded image in the images directory.
The $file variable inside the showImage() function is set to the success of the image saving operation. If there is any error, the condition code will show the error. Otherwise the script will return the success message.
Now it's time to process the image snap. Add this code to index.php.
Now when we receive the success message, we will show it in a table.
<table class="table table-bordered"> <thead> <tr> <th>Image</th><th>Image Name</th> </tr> </thead> <tbody id="imagelist"> </tbody> </table>
So now we have the code for taking the snapshot image and saving it to our images directory, but it is still not in database. Now we need to save the image to a database table record.
- id with auto-increment
- Image data
The table may be created in a MySQL database with SQL statement like this:
CREATE TABLE IF NOT EXISTS `snapshot` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Image` varchar(20000) NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The database access is done using a class in the connectionClass.php script. In this class we create a connection string with the database.
Now lets back to our save image class script: webcamClass.php .
In this class we have a function for saving to the database the image after saving it to our local system folder. This function will take the image URL as parameter. It will save the image to the database.
In the class we have a function for saving the image in our database named changeImagetoBase64 to save the image in your database using base64 encoding to store it in text field.
Download the Complete PHP Take Photo from Camera Package
We created the index.php script file to show the webcam screen to save the image in our project directory.
When the user clicks in the button to take a snapshot image it will upload it to the action.php script that calls a class in the webcamClass.php script to save the image in the images directory.
When the image name is saved to the database the connectionClass.php script establishes the database connection. A database table record is created with the image name using the function saveImageToDatabase.
In this tutorial we use the JpegCam Library. The whole list of used files consists of: action.php, webcam.js, webcam.swf, webcamClass.php, action.php, index.php and connectionClass.php.
You can download the whole list of files for this package in a ZIP archive or install it with the PHP composer tool following instructions from the same page.
If you liked this article or have questions, please post a comment here so it encourages me to write more useful articles.
You need to be a registered user or login to post a comment
Login Immediately with your account on: