AI News Hub Logo

AI News Hub

Develop a Pure PHP Face Recognition Application

DEV Community
Arshid

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. 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"; 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. 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 $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. // 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"; "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"; Later, you can compare faces using: Euclidean distance Cosine similarity To find the closest match from faces.db. File: recognize.php code 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