2016-09-12 93 views
1

我有一个脚本,可以从网络摄像头捕获图像并将其存储在使用php的目录中。使用PHP从网络摄像头或浏览文件上传图像

这里是我的html代码

<script type="text/javascript" src="scripts/webcam.js"></script> 
<script> 
    $(function(){ 
     //give the php file path 
     webcam.set_api_url('saveimage.php'); 
     webcam.set_swf_url('scripts/webcam.swf');//flash file (SWF) file path 
     webcam.set_quality(100); // Image quality (1 - 100) 
     webcam.set_shutter_sound(true); // play shutter click sound 

     var camera = $('#camera'); 
     camera.html(webcam.get_html(300, 200)); //generate and put the flash embed code on page 

     $('#capture_btn').click(function(){ 
      //take snap 
      webcam.snap(); 
      $('#show_saved_img').html('<h3>Please Wait...</h3>'); 
     }); 


     //after taking snap call show image 
     webcam.set_hook('onComplete', function(img){ 
      $('#camera_wrapper').html('<img src="' + img + '">'); 
      //reset camera for the next shot 
      //$("#camera_wrapper").html('&nbsp;'); 
      //webcam.hide(); 
     }); 

    }); 
</script> 
<!-- camera screen --> 
<div id="camera_wrapper"> 
<div id="camera"></div> 
<br /> 
<button id="capture_btn">Capture</button> 
</div> 
<!-- show captured image --> 
<div id="show_saved_img" ></div> 

PHP代码,用于存储图像

$filename = time() . '.jpg'; 
$filepath = 'saved_images/'; 
$result = file_put_contents($filepath.$filename,  file_get_contents('php://input')); 
if (!$result) { 
print "ERROR: Failed to write data to $filename, check permissions\n"; 
exit(); 
} 
echo $filepath.$filename; 
?> 

这个脚本工作正常。但是我想补充连同浏览文件选项这个。即用户可以使用其摄像头捕获图像,或者可以从设备上浏览文件。

我是javascript的新手。是否可以添加两个选项

回答

1

首先,您需要确保PHP已配置为允许文件上载。 在你的“php.ini”文件中,搜索file_uploads指令,并将其设置为On。

file_uploads = On 

在HTML表单中添加代码,允许用户选择从W3Schools的浏览文件

<!DOCTYPE html> 
<html> 
<body> 

<form action="file_uploader.php" method="post" enctype="multipart/form-data"> 
    Browse file to upload: 
    <input type="file" name="fileToUpload" id="fileToUpload"> 
    <input type="submit" value="Upload File" name="submit"> 
</form> 

</body> 
</html> 

在file_uploader.php

<?php 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
?> 

使用此tutorial作为参考

+0

我需要两个选项**网络摄像头**和**选择文件** –

+0

因此,编辑代码并添加您的文档r代码已经适用于摄像头。 – mayorsanmayor