By 02/04/2016 0 Comments

How To Implement Image Face Detection API In PHP?

  • There are many websites which provide different types of API’s those API’s detect a face of a person in image and return a complete information about that face.
  • In this tutorial we implement image face detection API in PHP.
  • This API is offered by faceplusplus website.
  • Before implement this API you have to take some following steps.



  • You have to log in through same account and go to Dev Center.


  • Create a new app by navigate to My App option, provide some information for your new App e.g App name, App information, API server etc and press submit button.

Final Step:

  • Now your App has been successfully created at faceplusplus platform. Notice there are a API Key and API Secret (this is your App key and Secrete) which is provided to you by using this API in your own application. Now its time to implement this API through following examples.

Example: Create a JavaScript file actions.js and put it in script folder

function displayInformation(){
	 document.getElementById("info").style.visibility = "visible";

function hideInformation(){
	 document.getElementById("info").style.visibility = "hidden";

Example: Create an stylesheet file style.css and put it in styles folder

input[type = 'text'] { height:24px; width:24%; }
button { height:28px; cursor:pointer; }
img {height:340px;}
body { background-color:#E9EAED; }
#white-area { background-color:#FFFFFF; width:67%; padding:15px;}
#info { visibility:hidden; font-size:20px;}

Example: Create a PHP class file class_face_sdk.php and put it in root folder

 class Face_Detect {
 private $Api_Key;
 private $Api_Secret;
 private $Us_server;
 function __construct(){
 $this->Api_Key = "Your API Key"; // these keys have been mentioned in final step.
 $this->Api_Secret = "Your API Secret";
//If you chose Amazon(US) API server (mention in Step-3) use the following URL.
 $this->Us_server = ""; 
 public function execute($image){
 $face= @file_get_contents($this->Us_server.'url='.$image.'&api_secret='.$this->Api_Secret.'&api_key='.$this->Api_Key.'&attribute=glass,pose,gender,age,race,smiling') or die("fail");
 $json = json_decode($face, true);

 @$width = number_format($json['face'][0]['position']['width'],2);
 @$height = number_format($json['face'][0]['position']['height'],2);
 @$nosex = number_format($json['face'][0]['position']['nose']['x'],2);
 @$nosey = number_format($json['face'][0]['position']['nose']['y'],2);
 @$age = $json['face'][0]['attribute']['age']['value'];
 @$age_range = $json['face'][0]['attribute']['age']['range'];
 @$race = $json['face'][0]['attribute']['race']['value'];
 @$race_confidence = number_format($json['face'][0]['attribute']['race']['confidence'],2);
 @$gender = $json['face'][0]['attribute']['gender']['value']; 
 @$gender_confidence = number_format($json['face'][0]['attribute']['gender']['confidence'],2); 
 @$glass = $json['face'][0]['attribute']['glass']['value']; 
 @$glass_confidence = number_format($json['face'][0]['attribute']['glass']['confidence'],2); 
 @$smiling = number_format($json['face'][0]['attribute']['smiling']['value'],2);

 echo "<table border = '0px' cellpadding='3px'>";
 echo "<tr><td>";
 echo "<img src = '".$image."' onmouseover='displayInformation()' onmouseout='hideInformation()'>";
 echo "</td>";
 echo "<td id='info'>width &nbsp; &nbsp; &nbsp; &nbsp; $width% <br> 
 height &nbsp; &nbsp; &nbsp; &nbsp; $height% <br> 
 nose &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ($nosex% , $nosey%) <br> 
 age &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $age ($age_range) <br> 
 race &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $race ($race_confidence%) <br> 
 gender &nbsp; &nbsp; &nbsp; &nbsp;$gender ($gender_confidence%) <br> 
 glass &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$glass ($glass_confidence%) <br> 
 smiling &nbsp; &nbsp; &nbsp;&nbsp;$smiling%
 } ?>

Final example: Create a PHP file index.php and put it in root folder

<!Doctype html>
 <meta charset="uft8">
 <link rel="stylesheet" href="styles/style.css"/>
 <script src="script/actions.js"></script>
 <div id = "white-area">
 <center><u> Your heading </u></center>
 $face_detect = new Face_Detect();
 if($_POST['url'] != ""){
 $image = $_POST["url"];
 else { echo "<span style='color:red;'>A picture URL required!</span>"; }
 else { echo "Please enter a picture URL !"; } ?>

 <form method="POST" action = "<?php htmlspecialchars($_SERVER['PHP_SELF'])?>">
 <input type="text" name="url" placeholder = "Enter Picture URL" title="Enter URL"/>
 <button type="submit" name="btn_pressed"> Load URL</button><br>
 <h3>Developed by:</h3>


  • If you are using localhost e.g: Wamp or Xamp server make sure that the option allow_url_fopen is set to On if it it is not On set it as allow_url_fopen = On in php.ini file.
  • If you are using live IIS server for this example it will work fine and if you are using another server such as Linux or non-window platform that it may possible this program does not work because some server has allow_url_fopen = Off due to security reasons.

Posted in: PHP, Solutions

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .