PHP + Javascript: Examples of online photo taking and javascript instances
This example describes how to use PHP + Javascript to take online photos. Share it with you for your reference. The details are as follows:
We may encounter this situation in some WEB applications. users need to take photos on site and upload them to the membership system. For example, the fingerprint photo acquisition process for driving schools, and photos taken at the test site. Today we are going to talk about how to use javascript and PHP to implement a simple online photo and upload function.
To achieve this function, you must install a camera on your computer and your browser must support flash.
<! Doctype html>
Add a container id # cam used to call the camera component to the body and a container id # results that displays the upload information.
Javascript
Next, call the camera component. We first load webcam. js, which is used to take photos and upload js libraries.
Copy codeThe Code is as follows: <script type = "text/javascript" src = "webcam. js"> </script>
Then add the following code to the container id # cam:
<Script language = "JavaScript"> webcam. set_api_url ('action. php '); webcam. set_quality (90); // the image quality (1-100) webcam. set_shutter_sound (true); // The audio document is played when the camera is taken. write (webcam. get_html (320,240,160,120); </script>
We called webcam, where webcam. set_api_url is used to set the php path for Image Upload interaction. set_quality can be used to set the image quality. set_shutter_sound can be used to set the sound and get_html output camera component. The parameter is the width, height, width, and height of the uploaded image.
When you click the button to take a photo, you need to execute the following code:
<script language="JavaScript">webcam.set_hook( 'onComplete', 'my_completion_handler' );function take_snapshot() {document.getElementById('results').innerHTML = '
When a photo is taken, the Code interacts with php in the background. If the image is uploaded, the corresponding information is returned.
PHP
Action. php uploads the image taken locally to the server and returns the image path to the front-end. Note that the write permission is required for storing the image path.
$filename = date('YmdHis') . '.jpg';$result = file_put_contents( 'pics/'.$filename, file_get_contents('php://input') );if (!$result) {print "ERROR: Failed to write data to $filename, check permissions\n";exit();}$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/pics/' . $filename;print "$url\n";
Here is just a brief introduction to the online photo taking and uploading functions. In fact, we will go deep into application scenarios such as cropping after uploading and generating multiple images with different proportions.
I hope this article will help you with php programming.