Skip to content Skip to sidebar Skip to footer

How to Upload a Webcam Photo Through Post Method

Author: Viewers: 6,744 Last month viewers: 2,392 Categories: PHP Tutorials

If you want to accept a film of a user to be used in a PHP Web site, you can practice it with a little help of a JavaScript HTML5 library.

This article is a tutorial that explains how to take picture snapshots using a Webcam to be uploaded to a PHP awarding.


Loaded Article

Contents

How to Take Picture with Webcam using HTML5 Camera API

Webcam Photo Capture Online Solution to Use Webcam to Take Pictures

How Webcam Moving-picture show Capture Works

Requirements for the PHP Webcam Script

Capturing Pictures Using a Webcam PHP Script and HTML5 Camera API JavaScript

SQL for the Paradigm Database PHP Upload Script

PHP Camera Capture Script for Saving the Image to the Database

Download the Consummate PHP Take Photo from Camera Package

How to Take Picture with Webcam using HTML5 Camera API

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 nevertheless pictures.

Nowadays most browsers support capturing Webcam images without whatsoever special platform specific drivers, and so any Web awarding can capture images with some HTML and JavaScript lawmaking and upload the images to the server.

This commodity presents y'all a solution that y'all tin utilize to implement in any PHP Spider web site.

Webcam Photo Capture Online Solution to Use Webcam to Have Pictures

Since Webcams can accept photos of any pictures that a computer or a mobile device can accept, this solution tin can work to take whatever kind of photos online and save them to a server running PHP.

How Webcam Picture Capture Works

Basically Webcams are devices that generate paradigm or video data. When accessed from a browser using an API, they tin can pull image or video data equally if they are reading files.

Accessing video or images like files makes information technology easier to process it because Web pages support file uploading using HTML forms.

Then all information technology is necessary to capture Webcam pictures is to connect Webcam data streams to file uploading forms.

Requirements for the PHP Webcam Script

The outset affair you demand is your PHP editor or IDE, a Webcam, MySQL and a cup of java. The PHP editor is of class for coding your PHP scripts, Webcam to have the image, MySQL for saving information technology in a database and Coffee is for your refreshment. :-)

In this commodity we will hash out how to capture the webcam epitome from our PHP and jQuery and save it to the database. It will use the JavaScript with HTML5 for the webcam screen. So you need JavaScript files that you can download and install every bit explained at the end of this article.

Capturing Pictures Using a Webcam PHP Script and HTML5 Camera API JavaScript

To create our capturing applications we demand to use three files:

  1. Web folio for showing the webcam epitome
  2. Script to handle the image upload request
  3. A PHP course script to have to the actual handling of the image and store it in a database

1. Spider web Page HTML: Have Picture From Camera

With this file we will show our Webcam screen on our PHP page.

index.php

Here we will utilize the JPEG Camera script for the webcam functions to take the snap and save it.

<script blazon="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/ane.10.2/jquery.min.js"></script>
<script src="jpeg_camera/jpeg_camera_with_dependencies.min.js" blazon="text/javascript"></script>

Below the form we will put our webcam window to show the webcam screen capture.

<div id="camera_info"></div>     <div id="photographic camera"></div><br>     <button id="take_snapshots" grade="btn btn-success btn-sm">Have 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()"> </course>            

In the code above we testify the screen and get the image from webcam. But still nosotros have to save it somewhere, so lets save this image in a server binder.

2. PHP Capture Image from Camera Script to Handle the Upload

Afterward showing the Webcam screen in PHP page nosotros have the snap from clicking the button. And so it the image needs to exist saved to a server directory of our project.

For this we can use a PHP course 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 proper name for the uploaded epitome in the images directory.

The $file variable inside the showImage() part is set to the success of the paradigm saving performance. If in that location is whatever mistake, the condition code will show the error. Otherwise the script will return the success message.

At present it's fourth dimension to process the epitome snap. Add this code to alphabetize.php.

<script blazon="text/javascript"><!--      var options = {       shutter_ogg_url: "jpeg_camera/shutter.ogg",       shutter_mp3_url: "jpeg_camera/shutter.mp3",       swf_url: "jpeg_camera/jpeg_camera.swf",     };     var camera = new JpegCamera("#camera", options);      $('#take_snapshots').click(function(){     var snapshot = camera.capture();     snapshot.show();          snapshot.upload({api_url: "action.php"}).done(role(response) { $('#imagelist').prepend("<tr><td><img src='"+response+"' width='100px' height='100px'></td><td>"+response+"</td></tr>"); }).fail(office(response) {   alert("Upload failed with status " + response); }); })  role done(){     $('#snapshots').html("uploaded"); }  // --></script>            

Now when we receive the success bulletin, we will show it in a table.

<table class="table tabular array-bordered">     <thead>         <tr>             <thursday>Image</th><th>Image Name</th>         </tr>     </thead>     <tbody id="imagelist">                  </tbody> </table>            

SQL for the Prototype Database PHP Upload Script

And so now we have the code for taking the snapshot image and saving it to our images directory, simply information technology is even so not in database. At present we need to salvage the epitome to a database table tape.

First create a database table with the name snapshot with an entry with 3 fields:

  1. id with auto-increment
  2. Paradigm information

The table may be created in a MySQL database with SQL statement like this:

CREATE Tabular array IF NOT EXISTS `snapshot` (   `Id` int(11) NOT Nothing AUTO_INCREMENT,   `Prototype` varchar(20000) NOT Nada,   Chief Primal (`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 connexion string with the database.

PHP Photographic camera Capture Script for Saving the Image to the Database

Now lets back to our salve epitome course script:webcamClass.php .

In this class we accept a function for saving to the database the image later saving it to our local system folder. This function will take the prototype URL as parameter. It will relieve the epitome to the  database.

saveImageToDatabase($imageurl);            

In the form we have a function for saving the image in our database named changeImagetoBase64 to save the paradigm in your database using base64 encoding to shop it in text field.

Download the Consummate PHP Take Photo from Photographic camera Package

We created the alphabetize.php script file to show the webcam screen to save the paradigm in our projection directory.

When the user clicks in the button to take a snapshot image it will upload information technology to the activity.php script that calls a grade in the webcamClass.php script to save the epitome in the images directory.

When the image name is saved to the database the connectionClass.php script establishes the database connexion. A database table record is created with the epitome 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, activity.php, index.php and connectionClass.php.

You lot can download the whole list of files for this package in a ZIP annal or install information technology with the PHP composer tool following instructions from the aforementioned folio.

If you liked this article or take questions, delight post a comment hither so it encourages me to write more useful articles.


Yous demand to exist a registered user or login to post a comment

1,592,318 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

Facebook

Facebook

Gmail

Gmail

Hotmail

Hotmail

StackOverflow

StackOverflow

GitHub

GitHub

Yahoo

Yahoo


Comments:

17. PHP Webcam Capture - ?????????? ?????? (2021-11-08 06:15)
PHP Webcam Capture... - 0 replies
Read the whole comment and replies

xvi. php class - hardik (2019-11-26 18:51)
dainty grade... - i reply
Read the whole annotate and replies

fifteen. webcam not working - HIMA PAUL (2018-12-30 03:46)
i amgetting an error bulletin to check permission... - 0 replies
Read the whole annotate and replies

14. Great! - Cyrus Cunanan (2018-03-31 xiv:35)
thanks... - 0 replies
Read the whole comment and replies

13. Problems with awarding - Fredrik Brandt (2017-10-25 03:37)
PHP Webcam Capture... - 2 replies
Read the whole comment and replies

12. THis mistake arises when i use this code - Amman (2016-01-05 16:ten)
please tell the reason... - 2 replies
Read the whole comment and replies

12. spider web came - Sher Khan (2015-02-27 09:29)
web cam non workin... - 2 replies
Read the whole comment and replies

eleven. Not working - akber.khowaja (2015-02-27 09:28)
this code is not working... - i reply
Read the whole annotate and replies

3. Use html5; forget well-nigh flash - Jeffrey Kurtock (2015-02-06 08:46)
jpegcam not supported since 2010... - two replies
Read the whole comment and replies

ten. Webcam plugin - Arial (2014-x-xviii 07:54)
Google Chrome Non Support with webcam plugin... - one answer
Read the whole annotate and replies


pattersongived1963.blogspot.com

Source: https://www.phpclasses.org/blog/post/228-How-to-Use-a-Webcam-to-take-Pictures-in-PHP-Application.html

Post a Comment for "How to Upload a Webcam Photo Through Post Method"