简体   繁体   中英

Capture image from webcam and save in folder using PHP and JavaScript

Kindly checkout code given below I want to save image in folder in JPG format using PHP and AJAX. Kindly help me I am not able to do it.

function base64_toimage() 
{
  $('#image').attr("src","data:image/png;base64,"+$.scriptcam.getFrameAsBase64());
};
function base64_tofield_and_image(b64) 
{
    $('#formfield').val(b64);
    $('#image').attr("src","data:image/png;base64,"+b64);
};
function changeCamera()
{
    $.scriptcam.changeCamera($('#cameraNames').val());
}       
function onError(errorId,errorMsg) {
    $( "#btn1" ).attr( "disabled", true );
    $( "#btn2" ).attr( "disabled", true );
    alert(errorMsg);
}           
function onWebcamReady(cameraNames,camera,microphoneNames,microphone,volume) {
    $.each(cameraNames, function(index, text) {
    $('#cameraNames').append( $('<option></option>').val(index).html(text) )
      }); 
    $('#cameraNames').val(camera);
}
</script> 
<br />
<div id="webcam" ></div>
<div  style="width:250px;float:left;" ><img src="webcamlogo.png" alt="" style="vertical-align:text-top"  /> 
<select name="cameraNames" size="1" id="cameraNames" style="width:205px;font-size:10px;height:25px;" onChange="changeCamera()"></select></div>

Check this on MDN , and it is pretty clear how to create a simple photo booth using WebRTC .

The second part of question is how to save the image data to disk :

  1. if you inspect the generated image you will find something like :

 < img id = "photo" alt = "The screen capture will appear in this box." src = "data:image/png;base64 ...... " > 

  1. you need to get the src attribute of the img using Javascript

 var myImg = document.getElementById("yourImgId").src; 

  1. then use php to save your file :

 $data = myImg; list($type, $data) = explode(';', $data); list(, $data) = explode(',', $data); $data = base64_decode($data); file_put_contents('path_to_your_directory/tmp/image.png', $data); 

Please note that this page will a php file .

php code can be inserted directly using <? ?> <? ?> .

If you want to do this via jquery , just post your Imgdata tothe remote php file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM