Crop square an image

0

I'm trying to crop the faces of a photo but I still can not find a mathematical formula to help me crop the crop.

The information I have is:

  • Photo
  • Position of faces of people in the photo (width, height, top, left)

Although the information on the positions of the faces is really only from the face of each person, I was able to increase the sides to appear more the person's full head. So if I have a crop of a face with the dimensions 80x120 I increase by twice that size and recalculate the top and left as follows:

$sizes['width'] = $sizes['width'] * 2; // Ganhando margem no crop
$sizes['height'] = $sizes['height'] * 2; // Ganhando margem no crop

// Recalculando top e left
$sizes['left'] = $sizes['left'] - ($sizes['width'] / 4);
$sizes['top'] = $sizes['top'] - ($sizes['height'] / 4);

It turns out that these positions do not have equal width and height. I needed to leave them equal but repositioning the top and left for the crop to be "centered."

Still considering the example above, I needed to get the width and height with 120px (top side) and top and left recalculated so the person's face would have the crop centered.

I'm not finding any formula to solve this.

I tried to find the largest side and recalculated the top or the left as follows:

if ($sizes['width'] >= $sizes['height']){
    $bigger = 'width';
    $sizes['left'] = $sizes['left'] - ($sizes['width'] - $sizes['height'] / 2);
} else {
    $bigger = 'height';
    $sizes['top'] = $sizes['top'] - ($sizes['height'] - $sizes['width'] / 2);
}

And in the crop I did:

$image->crop($sizes[$bigger], $sizes[$bigger], $sizes['left'], $sizes['top']);

Unfortunately the crop is not adjusted. Any suggestions?

Thank you.

    
asked by anonymous 28.11.2018 / 14:31

0 answers