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

allow resize image on cropFaceToJpeg #34

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 18 additions & 9 deletions FaceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ class FaceDetector
*
* @throws Exception
*/
public function __construct($detection_data = 'detection.dat')
public function __construct($detection_data = 'detection.json')
{
if (is_array($detection_data)) {
$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));

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

public function faceDetect($file)
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 @@ -159,17 +159,26 @@ public function toJpeg()
* If file is provided, the face will be stored in file, other way it will be output to standard output.
*
* @param string|null $outFileName file name to store. If null, will be printed to output
* @param boolean|false $resize resize crop image.
* @param int $width widht of new crop image. $resize value must 'true'. default to 200
* @param int $height height of new crop image. $resize value must 'true'. default to 200
*
* @throws NoFaceException
*/
public function cropFaceToJpeg($outFileName = null)
public function cropFaceToJpeg($outFileName = null, $resize = false, $width = 200, $height = 200)
{
if (empty($this->face)) {
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']);

if (!$resize) {
$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']);
} else {
$canvas = imagecreatetruecolor($width, $height);
imagecopyresized($canvas , $this->canvas, 0, 0, $this->face['x'], $this->face['y'], $width, $height, $this->face['w'], $this->face['w']);
}

if ($outFileName === null) {
header('Content-type: image/jpeg');
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "mauricesvay/php-facedetection",
"name": "yosiazwan/php-facedetection",
"description": "PHP class to detect one face in images. A pure PHP port of an existing JS code from Karthik Tharavad.",
"license": "GPL-2.0",
"homepage": "https://github.com/mauricesvay/php-facedetection",
Expand Down
1 change: 1 addition & 0 deletions detection.json

Large diffs are not rendered by default.