Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

PHP face detection class

Face detection is a computer technology that determines the locations and sizes of human faces in arbitrary (digital) images.

It detects facial features and ignores anything else, such as buildings, trees and bodies.

(source: Wikipedia)

There is so much to say about face detection and all its algorithms… I am planning a step by step tutorial about every branch of object detection in digital images, but first I want to publish Maurice Svay‘s PHP class.

Maurice, in his blog, explains he was looking for a face detection script for PHP, but wasn’t able to find one working without OpenCV (Open Source Computer Vision), an opensource lib that was originally developed by Intel.

OpenCV seems to perform well but you need to be able to install it on your server.

So he coded his own pure PHP solution, that does not require any library to be installed on the server.

And, obviously, that can be easily ported into any language. His class itself has been translated from a javascript code that actually is no longer available online

So this is the class:

Using it is very easy, just write

once you unpacked and included detection.dat and the image in the same path as your class.

Do you want to see a result?

This was generated by the script, just changing the frame color from red to green, to make it more visible.

The class does not work that well on every photo, but it’s a good start for a journey into face detection

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (47 votes, average: 4.72 out of 5)
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 38 comments

  1. Martti Laine

    on July 6, 2010 at 6:47 pm

    Holy sh*t. Never even thought about detecting faces in php. Thumbs up!

  2. Martti Laine

    on July 6, 2010 at 6:48 pm

    Sorry for my bad language.

    This article proves it, we’re on 21st century.

  3. Thomas

    on July 7, 2010 at 1:31 am

    So, what are the logics that make it work?

  4. mario

    on July 7, 2010 at 9:33 am had release a API for face detection, you can even use the code to detect the location of eye, nose and mouth

  5. Mal kurz rundgeschaut… #24 |

    on July 11, 2010 at 9:41 am

    […] PHP face detection class – gut erkannt mit Emanuele Feronato. […]

  6. bossoi

    on July 13, 2010 at 7:56 pm

    I have a question… why are .dat file for?

  7. ammii*

    on August 2, 2010 at 4:44 pm

    Absolutely yes!! I’ve never ever thought about PHP for detecting faces!!!
    Thank you very much :))

  8. Marcius

    on August 9, 2010 at 12:23 pm

    Very good, congratuloations and tks for your contribe

  9. prince

    on August 16, 2010 at 9:39 am

    Amazing might use it in my next project :)

  10. Zeeshan Lalani

    on August 16, 2010 at 10:41 am

    very nice.. thanks..

  11. Face Detection Using PHP |

    on August 16, 2010 at 12:18 pm

    […] Source: […]

  12. Shashi

    on August 16, 2010 at 1:22 pm

    Thanks a lot for this great post… i have been wondering whether it would be possible to do these kind of things with php… hats off php !

  13. datshay

    on August 16, 2010 at 2:24 pm

    Amazing….. No words to say ….
    Thumbs up !

  14. Karl Roos

    on August 16, 2010 at 10:10 pm

    This is amazing! Never thought you’d be able to do this with PHP!

  15. vcrack

    on August 18, 2010 at 2:45 pm

    how long your execution time to do this?

  16. Vincent

    on August 19, 2010 at 1:58 pm

    I’ve never tought that PHP could do this thing too. It’s awesome

  17. harika

    on August 22, 2010 at 4:32 pm

    thank you:D

  18. Proger

    on August 29, 2010 at 9:02 am

    Thanks, cool script.
    However, it is not always working correctly, here’s an example:

  19. Detectar una cara en una foto usando PHP | Blog personal de Brian Urban

    on September 8, 2010 at 2:28 am

    […] Via Emanuele Feronato […]

  20. Face detection using PHP

    on November 20, 2010 at 12:00 pm

    […] Without Open CV you can apply face detection using the PHP class mentioned at this blog […]


    on January 13, 2011 at 6:23 pm

    I saw that happend error in function imagecolorat and can not work

  22. PHP and Jquery face detection scripts «

    on January 21, 2011 at 11:52 pm

    […] Download page […]

  23. Dmitry

    on January 25, 2011 at 5:39 am

    Doesn`t detect multiple and small faces. How’d you tweak the code to do that?

  24. tumaji

    on May 6, 2011 at 2:09 pm

    Thank you, I looking for this script.

  25. tejas tank

    on December 2, 2011 at 7:44 am

    Here detection.dat is which kind of file. is a jpg or movie or other things…

  26. Mohd

    on December 28, 2011 at 3:13 pm

    Hay @Proger..

    At least it detected the face of Putin’s care :) I think PHP can’t recognize Putin.. same of the rest of us, this is logic.

  27. mrtaza

    on January 19, 2012 at 7:10 pm

  28. mrtaza

    on January 19, 2012 at 7:12 pm

    i have used the above class in this sample
    works in most cases.

  29. Dan

    on February 17, 2012 at 10:40 am

    I have two other Haar Xml files, is there any way to convert these to .dat files for use in this PHP implementation?

    Kind regards,


  30. ??????????? ?? ???? – PHP ????? |

    on February 21, 2012 at 4:50 pm

    […] ?????????? ?????: […]

  31. Muthukumar

    on September 13, 2012 at 6:57 am

    This is not working to me

  32. absfrm

    on October 30, 2012 at 10:44 pm

    this is powerful.thanks to share
    but can you answer my question?
    i want to detect a face(save some detail for it as name or family).
    and when detect that face again , system show me his/her detail.
    can i do this action?
    thanks a lot

  33. Lakshmi Narayanan

    on July 17, 2013 at 5:05 pm

    Hi this is great! Working fine with most of the complete and round faces. Thank u for the code! If you tweak the code further please let us know!

  34. Flash AS3 face detection and recognition library - Emanuele Feronato

    on September 25, 2013 at 10:10 pm

    […] which allows face detection and recognition in a photo. I already blogged about a similar topic in PHP face detection class post, but this time we’ll work with AS3 and face […]

  35. Sander

    on November 4, 2013 at 5:43 pm

    Face detection with PHP is not necessary, you can better use jQuery client side script.

    Search on Google for jQuery face detection and you will find it.

  36. suman

    on November 12, 2013 at 8:21 pm

    programm is giving notice at line no 118 imagecolorat() out of bound ,,and also showing fatal error execution time exceed 30 sec in line 119 ,,,what i shoud do

  37. jQuery face detection plugin - Emanuele Feronato

    on November 19, 2013 at 11:56 pm

    […] I like to play with face detection and blog about it, I already showed you a PHP face detection class and a Flash AS3 face detection and recognition library, now it’s time to give a try to Jay […]

  38. devpk

    on March 24, 2014 at 10:01 am

    hye i need to compare thumb impression … is that library useful for matching thumb .thumb is store in mysql blob format. kindly suggest me links

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now