Today in this tut i will explain you how we can capture and save image with help of HTML5  and PHP without any help of flash or any other plugin

capture image using html5


HTML5 is very powerfull tool in web today. And its realy help you to save your important time by giving you lots of feature which is not easy to made if you want to create by your own.

HTML5 is now supported by all majour browser like Google Chrome, Firefox, IE 9+, Opera, Safari etc.

Now we start creating this small piece of application.

For creating this app we are using html5 getUserMedia().

Currently chrome, mozilla, opera supports getUsermedia API. So this is a limitation of this application we never know from which browser user access this application.

GetUserMedia() tag use different prefix for chrome and Mozilla corresponding to web engine i.e., for chrome we have to use“webkitGetUserMedia” function and for Mozilla we use “mozGetUserMedia” function and for For Opera no need to add any prefix simply use “getUserMedia”.

By default getUserMedia disabled in chrome and mozilla.

To enable getUserMedia in Chrome type ‘chrome://flags/’ in URL bar and enable “Enable screen capture support in getUserMedia(). Mac, Windows, Linux, Chrome OS” option.

To enable getUserMedia in Mozilla type ‘about:config’ in URL bar search for ”media.navigator.enabled” and set it True.

Safari and InternetExplorer does not support getUserMedia.

Now we code to access our webcam with HTML5

Code: index.html

Above code is the main code we write inside the body. We simply use <video> and <canvas> tag of HTML5 to display your data capture by your webcam.

And next we will write javascript code to make thing work.

Now understanding getUserMedia().

HTML5 getUserMedia function take three arguments like constraints, successCallback, FailureCallback. The syntax is shown below.


getUserMedia constraints are nothing but which media streams to use i.e., audio or video.And it is required field


successcallback is the function we call when media stream is loaded successfully in our case put it into video tag.success callback is required field.


Failure callback is the function we call when media stream cannot be loaded it display error. It is a required field in Mozila

Save capture Image:

Now we ready to cature image but we also save the capture image to our server. We use PHP for backend here.

First we take a look to javascript code which pass this image to server


We first get data from canvas data is in base64encoded format and save it in dataUrl variable above. And then we simply use ajax function and pass data to server.

PHP Code:

In above we access data and save it into our server

Click on below link for demo and download of this code