Webcam in PHP Using Jquery | Save Webcam Image in Database


Hello friends after seeing many issues and great response at our previous post about How to Use Webcam in PHP. We have changed the code and now we have use the PHP Class for making it more simple to use.  In this article we will tell you how you can use this class in your application. You can now download the zip content from 2 locations



1. PHP Classes

2. Github

We have changed the database part and now you can save the image to your local storage or change it to base64 and save it to the Database or do both. So lets start.

Files we will use in this example

  • Index File
  • Webcam Class
  • Connection Class
  • Webcam JS
  • Webcam SWF
  • Shutter MP3
  • Jquery

In the article i will talk about the only first 3 files from the list.

Index File

This file contains the code for showing the webcam in the page and using the buttons to capture the image. For showing the webcam window inside your page use the below code



Now we will show the buttons so we will make it to work.

In the example we have used the onClick event inside the button. You can also use the jquery Click functions using the button Id. There are two buttons Configure Button will open the configuration window in the webcam. Another button is Take Snapshot Button  it will do the task of capturing the image from the webcam.




Now here is the onClick event and initialization of Webcam JS.

After setting the html part for viewing and clicking we will use the webcam Class to save the image to the local storage.

We have used the $imageFolder for defining the path of image to be saved.

ShowImage function will save the image data to local storage to the given path. If it will save the data correctly than it will return the name of the image with the path. Else it will give you the error.

Under showImage function you will see a code which is commented if you will uncomment this line it will help you to save the image name to the database table.

SaveImageToDatabase function is used to save the Image name to the database after saving the Image to the local storage. If you want to store the image directly to the database than you can uncomment the base64 image encode method line than image will be saved as base64.

Hope you like the tutorial about How to Use Webcam In PHP and Save Image to the Database. In the index file we have used the Bootstrap and Jquery library.  For making it easy to understand.

If you have any comment or issues regarding the tutorial please comment below.



3 Comments

  1. Thanks for the great script. Instead of INSERT into the database, I want to update the uploaded image path into the database. So I need to pass the database key as the parameter to the function saveImageToDatabase. Can you please suggest, how can I pass database key to saveImageToDatabase function?

  2. thanku sir for ur updates…..plz keep update

Leave a Reply

Your email address will not be published.

*

Shares