Skip to content
This repository has been archived by the owner on Jan 18, 2020. It is now read-only.

Expand the crop area such that the full face is cropped #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions FaceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public function __construct($detection_data = 'detection.dat')
$this->detection_data = $detection_data;
return;
}

if (!is_file($detection_data)) {
// fallback to same file in this class's directory
$detection_data = dirname(__FILE__) . DIRECTORY_SEPARATOR . $detection_data;

if (!is_file($detection_data)) {
throw new \Exception("Couldn't load detection data");
}
}

$this->detection_data = unserialize(file_get_contents($detection_data));
}

Expand All @@ -74,7 +74,7 @@ public function faceDetect($file)
} elseif (is_string($file)) {

$this->canvas = imagecreatefromstring($file);

} else {

throw new Exception("Can not load $file");
Expand Down Expand Up @@ -168,8 +168,15 @@ public function cropFaceToJpeg($outFileName = null)
throw new NoFaceException('No face detected');
}

$canvas = imagecreatetruecolor($this->face['w'], $this->face['w']);
imagecopy($canvas, $this->canvas, 0, 0, $this->face['x'], $this->face['y'], $this->face['w'], $this->face['w']);
$x = ($a = $this->face['x'] - $this->face['w']/2) > 0 ? $a : 0;
$y = ($b = $this->face['y'] - $this->face['w']/2) > 0 ? $b : 0;
$im_width = imagesx($this->canvas);
$im_height = imagesy($this->canvas);
$w = ($w = $this->face['w']*2) > $im_width ? $im_width : $w;
$h = ($h = $w) > $im_height ? $im_height : $h;

$canvas = imagecreatetruecolor($w, $h);
imagecopy($canvas, $this->canvas, 0, 0, $x, $y, $w, $h);

if ($outFileName === null) {
header('Content-type: image/jpeg');
Expand Down