Face recognition isn’t just for Python or complex AI stacks anymore. You can now implement it in PHP to create efficient web-based solutions for attendance, security, and user authentication, even integrating seamlessly with Laravel or WordPress.
This guide walks you through using Dlib models in PHP to perform face detection, identify facial landmarks, and generate face embeddings.
Install the php-dlib Extension
Download the correct file from: https://github.com/mailmug/php-dlib/releases
Find php.ini, then add the extension path.
Example:
extension="/path/to/dlib.dll"
More details: https://github.com/mailmug/php-dlib/
Create a data directory, then download the required files and extract their contents into that folder.
http://dlib.net/files/mmod_human_face_detector.dat.bz2,
http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2, http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2
$detectionModel = "data/mmod_human_face_detector.dat";
$landmarkModel = "data/shape_predictor_5_face_landmarks.dat";
$recognitionModel = "data/dlib_face_recognition_resnet_model_v1.dat";
Model Roles:
- Face Detector → Finds faces in images
- Landmark Detector → Identifies eyes, nose, and mouth positions
- Recognition Model → Generates 128D face embeddings
PHP Face Recognition Code Explained
1. Initialize Models
$fd = new CnnFaceDetection($detectionModel);
$fld = new FaceLandmarkDetection($landmarkModel);
$fr = new FaceRecognition($recognitionModel);
These objects load your AI models into PHP for processing images.
2. Define Known People Dataset
$people = [
"Arshid" => "Photo-1.jpeg", //correct file path
"Jhon" => "Photo-2.png",
];
This acts as your training dataset of known faces.
3. Face Detection + Processing Loop
foreach ($people as $name => $img) {
echo "Processing: $name\n";
$faces = $fd->detect($img);
if (count($faces) == 0) {
echo "No face found in $img\n";
continue;
}
$face = $faces[0];
✔ Detects faces in the image
✔ Skips images without faces
✔ Selects the first detected face
4. Facial Landmark Detection
$landmarks = $fld->detect($img, $face);
This step improves accuracy by locating:
- Eyes
- Nose
- Mouth
- Jawline This is crucial for alignment before recognition. ### 5. Generate Face Embedding
$descriptor = $fr->computeDescriptor($img, $landmarks);
This generates a 128-dimensional face embedding a unique numerical signature representing a person’s face. It forms the foundation of modern face recognition systems.
6. Store Face Database
// 4. store
$database[$name] = $descriptor;
echo "Saved: $name\n";
}
// save to file
file_put_contents("faces.db", serialize($database)); // save as file. You can store to db also
echo "Database created\n";
Complete Enroll Code (Train to Machine)
<?php
$detectionModel = "data/mmod_human_face_detector.dat";
$landmarkModel = "data/shape_predictor_5_face_landmarks.dat";
$recognitionModel = "data/dlib_face_recognition_resnet_model_v1.dat";
$fd = new CnnFaceDetection($detectionModel);
$fld = new FaceLandmarkDetection($landmarkModel);
$fr = new FaceRecognition($recognitionModel);
$people = [
"Arshid" => "Photo-1.jpeg",
"Jhon" => "Photo-2.jpg",
];
$database = [];
foreach ($people as $name => $img) {
echo "Processing: $name\n";
// 1. detect face first
$faces = $fd->detect($img);
if (count($faces) == 0) {
echo "No face found in $img\n";
continue;
}
// take first face
$face = $faces[0];
// 2. landmark detection (✔ needs image + face box)
$landmarks = $fld->detect($img, $face);
// 3. face encoding
$descriptor = $fr->computeDescriptor($img, $landmarks);
// 4. store
$database[$name] = $descriptor;
echo "Saved: $name\n";
}
// save to file
file_put_contents("faces.db", serialize($database));
echo "Database created\n";
Understanding Face Matching: The Next Step
Later, you can compare faces using:
- Euclidean distance
- Cosine similarity
- To find the closest match from faces.db.
File: recognize.php code
<?php
$detectionModel = "data/mmod_human_face_detector.dat";
$landmarkModel = "data/shape_predictor_5_face_landmarks.dat";
$recognitionModel = "data/dlib_face_recognition_resnet_model_v1.dat";
$fd = new CnnFaceDetection($detectionModel);
$fld = new FaceLandmarkDetection($landmarkModel);
$fr = new FaceRecognition($recognitionModel);
$database = unserialize(file_get_contents("faces.db"));
$image = "test.jpeg";
$faces = $fd->detect($image);
foreach ($faces as $face) {
$landmarks = $fld->detect($image, $face);
$descriptor = $fr->computeDescriptor($image, $landmarks);
$bestName = "Unknown";
$bestDist = 999;
foreach ($database as $name => $dbDescriptor) {
$dist = 0;
for ($i = 0; $i < 128; $i++) {
$diff = $descriptor[$i] - $dbDescriptor[$i];
$dist += $diff * $diff;
}
$dist = sqrt($dist);
if ($dist < $bestDist) {
$bestDist = $dist;
$bestName = $name;
}
}
// threshold (important)
if ($bestDist < 0.6) {
echo "MATCH: $bestName (distance $bestDist)\n";
} else {
echo "Unknown face\n";
}
}
Download source code: https://ciphercoin.com/wp-content/uploads/2026/04/photo-rec.zip
Top comments (1)
I’m looking for your feedback.