Face detection in images using PHP

In a previous post we saw how to detect human faces in images using JavaScript. In the current post we will look into a PHP port of a JavaScript face detection algorithm without using openCV. Since the code is completed written in PHP it cannot attain the raw speed that C,C++ provides, but for many trivial application a PHP version can do.


Before reading further you can download the code below. The original JavaScript port is located here, the following includes the complete code with a crop method added along with a few example files. As the code relies on the GD library, make sure that it is installed and activated on your system.

Download Source
Downloads : 7153 / File size : 499.3 kB

Running our sample code

The sample code to show the detected face is show below. Note that in images having multiple faces only the first one is detected. Although this might seem like a limitation, this can be useful in web applications that need to create a user profile picture from a raw image. So that if a user uploads a full length picture of him/her, the given code can detect the face and automatically create a profile picture for the user.

include "FaceDetector.php";
$face_detect = new Face_Detector('detection.dat');

Flickr :By modelxing

The above code displays a border around the detected face, instead we can directly crop the resulting face area and output the resulting image.

include "FaceDetector.php";
$face_detect = new Face_Detector('detection.dat');

Face coordinates

If you need the coordinates of the detected face for further processing, you can use the following.

// Return coordinates in JSON
// {'x':56.375, 'y':45.1, 'w':227.55}
// OR
// Return coordinates as an Array

Extending the class

You can extend the Face_Detector class using your own methods. For example I’ve added three methods – Rotate(), resizeFace() and toGrayScale() to the class, which lets you modify the final face area further.

include "FaceDetector.php";
/* We now extend the above class so we can add our own methods */
class FaceModify extends Face_Detector {
  public function Rotate() {
    $canvas = imagecreatetruecolor($this->face['w'], $this->face['w']);
    imagecopy($canvas, $this->canvas, 0, 0, $this->face['x'], 
              $this->face['x'], $this->face['w'], $this->face['w']);
    $canvas = imagerotate($canvas, 180, 0);
  public function toGrayScale() {
    $canvas = imagecreatetruecolor($this->face['w'], $this->face['w']);
    imagecopy($canvas, $this->canvas, 0, 0, $this->face['x'], 
              $this->face['x'], $this->face['w'], $this->face['w']);
    imagefilter ($canvas, IMG_FILTER_GRAYSCALE);
  public function resizeFace($width, $height) {
    $canvas = imagecreatetruecolor($width, $width);
    imagecopyresized($canvas, $this->canvas, 0, 0, $this->face['x'], 
                     $this->face['y'], $width, $height, 
                     $this->face['w'], $this->face['w']);
  private function _outImage($canvas) {
    header('Content-type: image/jpeg');
/* Using the extended class */
$face_detect = new FaceModify('detection.dat');

NB: If for some reason the code returns a error, turn off error reporting in php.ini

Further Links

1. Automatic Face Detection in Photos with PHP
2. http://www.facedetection.com/

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

9 Responses


Juan C. Monestel

July 7th, 2011 at 5:14 am

Great article… better if was multiple detection hehe


Gesichtserkennung mit PHP | tagdocs.de

July 8th, 2011 at 4:18 am

[...] beruht (und daher ohne jegliche Hilfsbibliotheken wie OpenCV, etc. auskommt).Die Klasse von Sameer Borate bietet eine Reihe nützlicher Features wobei die gleiche Einschränkung wie schon damals gilt: die [...]


m a r c o

July 19th, 2011 at 2:13 am

Nice idea, but it doesn’t seem to find any face on the image from your previous javascript demo :-/ …any idea?
I modified your code to get multiple faces, and it gives very uncertain results…
m a r c o


La détection des visages en PHP | MakeMonSite

July 23rd, 2011 at 9:55 pm

[...] Source :: Codediesel [...]


Linkdump #55: PHP – Personal Home Page. « Tomasz Kowalczyk

August 24th, 2011 at 1:48 pm

[...] Face detection in images using PHP. Wykrywanie twarzy w PHP. [...]



November 15th, 2012 at 8:35 am

Hello, thank you for this tutorial. We successfully added this code in our application for android to handle remote images. Can you update this code to get multiple faces detection? Please.
Best regards



March 11th, 2013 at 2:41 am

[...] képeket, vagy az arcot. A codediesel.com oldalon éppen egy ilyen cikkre akadtam (Face detection in images using PHP), amiben PHP használatával, JPG képből egy osztály és némi adat [...]



June 6th, 2014 at 12:42 pm

it’s very googd script. but…

how i can save the face witch detect?



July 29th, 2014 at 4:55 am

Do it like this :

public function cropFace($path) {
$canvas = imagecreatetruecolor($this->face['w'], $this->face['w']);
imagecopy($canvas, $this->canvas, 0, 0, $this->face['x'], $this->face['x'], $this->face['w'], $this->face['w']);
//header(‘Content-type: image/jpeg’);
imagejpeg($canvas, $path);

Your thoughts