You are here

public function Face_Detector::face_detect in Image Focus Crop 6

Same name and namespace in other branches
  1. 7 php-facedetection/FaceDetector.php \Face_Detector::face_detect()

File

php-facedetection/FaceDetector.php, line 37

Class

Face_Detector

Code

public function face_detect($file) {
  if (is_resource($file)) {
    $this->canvas = $file;
  }
  elseif (is_file($file)) {
    $this->canvas = imagecreatefromjpeg($file);
  }
  else {
    throw new Exception("Can not load {$file}");
  }
  $im_width = imagesx($this->canvas);
  $im_height = imagesy($this->canvas);

  //Resample before detection?
  $ratio = 0;
  $diff_width = 320 - $im_width;
  $diff_height = 240 - $im_height;
  if ($diff_width > $diff_height) {
    $ratio = $im_width / 320;
  }
  else {
    $ratio = $im_height / 240;
  }
  if ($ratio != 0) {
    $this->reduced_canvas = imagecreatetruecolor($im_width / $ratio, $im_height / $ratio);
    imagecopyresampled($this->reduced_canvas, $this->canvas, 0, 0, 0, 0, $im_width / $ratio, $im_height / $ratio, $im_width, $im_height);
    $stats = $this
      ->get_img_stats($this->reduced_canvas);
    $this->face = $this
      ->do_detect_greedy_big_to_small($stats['ii'], $stats['ii2'], $stats['width'], $stats['height']);
    if ($this->face['w'] > 0) {
      $this->face['x'] *= $ratio;
      $this->face['y'] *= $ratio;
      $this->face['w'] *= $ratio;
    }
  }
  else {
    $stats = $this
      ->get_img_stats($this->canvas);
    $this->face = $this
      ->do_detect_greedy_big_to_small($stats['ii'], $stats['ii2'], $stats['width'], $stats['height']);
  }
  return $this->face['w'] > 0;
}