Frontend Development 9 min read

Using PHP and JavaScript to Capture Camera Images with Real-Time Filters

This tutorial explains how to install required components, create a web page that previews the camera using getUserMedia, and apply real‑time filter effects with PHP‑GD by sending captured frames to the server for processing.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP and JavaScript to Capture Camera Images with Real-Time Filters

Photography technology is constantly evolving, and now you can use PHP to access the camera and add real‑time filter effects to your photos. This quick‑start guide shows how to capture images with PHP and apply desired filters.

1. Install Required Components and Libraries

First, install the necessary components and libraries. You need the PHP‑GD library for image processing and Video4Linux for video capture on Linux. Install them on Ubuntu with:

sudo apt-get install php-gd
sudo apt-get install v4l-utils

2. Create a Camera Real‑Time Preview Page

Next, create a PHP page that displays the camera’s live preview. The following HTML/JavaScript code creates a simple page that captures video, draws each frame onto a canvas, converts it to a Base64 JPEG, and shows it in an img element.

<!DOCTYPE html>
<html>
<head>
<title>Camera Preview</title>
</head>
<body>
<h1>Camera Preview</h1>
<img id="preview" src="" width="640" height="480" />
<script>
var videoElem = document.createElement('video');
var canvasElem = document.createElement('canvas');
var context = canvasElem.getContext('2d');
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
videoElem.srcObject = stream;
videoElem.play();
setInterval(function() {
context.drawImage(videoElem, 0, 0, canvasElem.width, canvasElem.height);
var imgData = canvasElem.toDataURL('image/jpeg');
document.getElementById('preview').src = imgData;
}, 1000);
}).catch(function(error) {
console.log('Error: ' + error.message);
});
</script>
</body>
</html>

The script uses the getUserMedia API to access the camera, draws the video onto a canvas, and updates the preview image every second.

3. Add Real‑Time Filter Effects

After the preview works, add a filter selection box and use PHP‑GD to apply filters such as grayscale, sepia, or invert. The page now includes a select element for filter choice and sends the selected filter and image data to the server.

<!DOCTYPE html>
<html>
<head>
<title>Camera Preview with Filters</title>
</head>
<body>
<h1>Camera Preview with Filters</h1>
<img id="preview" src="" width="640" height="480" />
<select id="filter">
<option value="none">None</option>
<option value="grayscale">Grayscale</option>
<option value="sepia">Sepia</option>
<option value="invert">Invert</option>
</select>
<script>
// ... JavaScript code for camera preview ...
document.getElementById('filter').addEventListener('change', function() {
var filter = this.value;
var imgData = canvasElem.toDataURL('image/jpeg');
// Send imgData and filter to server-side PHP code for processing
});
</script>
</body>
</html>

On the server side, the following PHP script receives the posted image data and filter name, creates a GD image resource from the Base64 data, applies the chosen filter, and outputs the processed JPEG.

<?php
// Process the image based on the selected filter
if(isset($_POST['filter']) && isset($_POST['imgData'])) {
$imgData = $_POST['imgData'];
$filter = $_POST['filter'];
// Create GD image resource from Base64 image data
$imgResource = imagecreatefromstring(base64_decode(str_replace('data:image/jpeg;base64,', '', $imgData)));
// Apply filters based on the selected option
switch($filter) {
case 'none':
break;
case 'grayscale':
imagefilter($imgResource, IMG_FILTER_GRAYSCALE);
break;
case 'sepia':
imagefilter($imgResource, IMG_FILTER_GRAYSCALE);
imagefilter($imgResource, IMG_FILTER_COLORIZE, 90, 60, 40);
break;
case 'invert':
imagefilter($imgResource, IMG_FILTER_NEGATE);
break;
}
// Output the filtered image
header('Content-Type: image/jpeg');
imagejpeg($imgResource);
// Clean up resources
imagedestroy($imgResource);
}
?>

The JavaScript uses XMLHttpRequest to POST the filter and image data to the PHP script, then replaces the preview image with the filtered result returned from the server.

// ... JavaScript code for camera preview ...
document.getElementById('filter').addEventListener('change', function() {
var filter = this.value;
var imgData = canvasElem.toDataURL('image/jpeg');
var xhr = new XMLHttpRequest();
xhr.open('POST', 'filter.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if(xhr.readyState === 4 && xhr.status === 200) {
document.getElementById('preview').src = 'data:image/jpeg;base64,' + xhr.responseText;
}
};
var data = 'filter=' + encodeURIComponent(filter) + '&imgData=' + encodeURIComponent(imgData);
xhr.send(data);
});

Open the page in a browser, select different filters, and see the live preview change. When you capture a photo, the PHP code adds the selected filter and outputs the final image.

This simple guide demonstrates the basic steps to use PHP, the PHP‑GD library, and the Video4Linux interface to capture camera images and apply real‑time filters, which you can further extend for more creative effects.

JavaScriptWeb DevelopmentPHPCameraGD LibraryReal-time Filters
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.