From 753868082a971b854cc3d2ed3a8c95eb8e3d9fa8 Mon Sep 17 00:00:00 2001 From: Alexandr Sorochkin Date: Tue, 26 Jan 2016 13:15:15 +0300 Subject: [PATCH] added cropFaceToJpeg method --- Exception/NoFaceException.php | 13 +++++++++++++ FaceDetector.php | 32 ++++++++++++++++++++++++++++++-- composer.json | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 Exception/NoFaceException.php diff --git a/Exception/NoFaceException.php b/Exception/NoFaceException.php new file mode 100644 index 0000000..60d084a --- /dev/null +++ b/Exception/NoFaceException.php @@ -0,0 +1,13 @@ +canvas); } + /** + * Crops the face from the photo. + * Should be called after `faceDetect` function call + * 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 + * + * @throws NoFaceException + */ + public function cropFaceToJpeg($outFileName = null) + { + 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 ($outFileName === null) { + header('Content-type: image/jpeg'); + } + + imagejpeg($canvas, $outFileName); + } + public function toJson() { return json_encode($this->face); diff --git a/composer.json b/composer.json index 5336528..6d0ccd6 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,6 @@ "ext-gd": "*" }, "autoload": { - "classmap": ["FaceDetector.php"] + "classmap": ["FaceDetector.php", "Exception/"] } }