Skip to content

Instantly share code, notes, and snippets.

@symisc
Last active March 11, 2021 01:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symisc/d54808915093e5375fdcb841e436580d to your computer and use it in GitHub Desktop.
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)
<?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