Last active
August 21, 2024 17:48
-
-
Save symisc/d54808915093e5375fdcb841e436580d to your computer and use it in GitHub Desktop.
Apply a blur filter automatically for each detected face using the PixLab Rest API (PHP Sample)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* PixLab PHP Client which is just a single class PHP file without any dependency that you can get from Github | |
* https://github.com/symisc/pixlab-php | |
*/ | |
require_once "pixlab.php"; | |
# Detect all human faces in a given image via `facedetect` and blur all of them via `mogrify`. | |
# https://pixlab.io/cmd?id=facedetect & https://pixlab.io/cmd?id=mogrify for additional information. | |
# Target Image we want to blur face(s) on | |
$img = 'https://pixlab.io/images/m3.jpg'; | |
# Your PixLab API key | |
$key = 'PIXLAB_API_KEY'; | |
$pix = new Pixlab($key); | |
echo "Detecting faces first...\n"; | |
/* Invoke facedetect first */ | |
if( !$pix->get('facedetect',array('img' => $img)) ){ | |
echo $pix->get_error_message(); | |
die; | |
} | |
/* Grab the total number of detected faces */ | |
$faces = $pix->json->faces; | |
echo "Total number of detected faces: ".count($faces)."\n"; | |
if( count($faces) < 1 ){ | |
echo "No human faces were were detected on this picture\n"; | |
}else{ | |
echo "Blurring faces...\n"; | |
/* Call mogrify via POST and pass the facial coordinates extracted earlier */ | |
if( !$pix->post('mogrify', ['img' => $img,'cord' => $faces]) ){ | |
echo $pix->get_error_message(); | |
}else{ | |
echo "Blurred faces URL: ".$pix->json->link."\n"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment