Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

prepareFaceImage()

Description

Crops out a selected area of an image and makes it ready to be used by FaceRecognizer.

Note: The selected area should follow the normalized coordinate system.

Parameters

ColumnTypeDefaultDescription
$image_path string Path of the source image
$prepared_image_path string Path of the prepared image
$x float X position
$y float Y position
$width float Width
$height float Height
$overwrite_existing boolean false Set to TRUE to overwrite existing prepared image (if any exists)

Return

boolean

Location

include/facial_recognition_functions.php lines 63 to 112

Definition

 
function prepareFaceImage($image_path$prepared_image_path$x$y$width$height$overwrite_existing false)
    {
    if(!
file_exists($image_path))
        {
        
debug("FACIAL_RECOGNITION: Could not find image at '{$image_path}'");
        return 
false;
        }

    
// Use existing prepared image if one is found
    
if(!$overwrite_existing && file_exists($prepared_image_path))
        {
        return 
true;
        }

    
// X, Y, width and height MUST be numeric
    
if(!is_numeric($x) || !is_numeric($y) || !is_numeric($width) || !is_numeric($height))
        {
        return 
false;
        }

    
$convert_fullpath get_utility_path('im-convert');
    if(
false === $convert_fullpath)
        {
        
debug('FACIAL_RECOGNITION: Could not find ImageMagick "convert" utility!');
        return 
false;
        }

    list(
$image_width$image_height) = getimagesize($image_path);

    
$image_path_escaped          escapeshellarg($image_path);
    
$prepared_image_path_escaped escapeshellarg($prepared_image_path);

    
$x      escapeshellarg(round($x $image_width0));
    
$y      escapeshellarg(round($y $image_height0));
    
$width  escapeshellarg(round($width $image_width0));
    
$height escapeshellarg(round($height $image_height0));

    
$cmd  $convert_fullpath;
    
$cmd .= {$image_path_escaped} -colorspace gray -depth 8";
    
$cmd .= " -crop {$width}x{$height}+{$x}+{$y}";
    
$cmd .= " -resize 90x90\>";
    
$cmd .= " +repage {$prepared_image_path_escaped}";

    if(
'' !== run_command($cmd))
        {
        return 
false;
        }

    return 
true;
    }

This article was last updated 13th July 2020 16:35 Europe/London time based on the source file dated 18th May 2020 17:14 Europe/London time.