0
I’m trying to perform the face Rop of a photo but I still can’t find a mathematical formula that will help me to make Crop square.
The information I have is:
- Photo
- Photo faces position (width, height, top, left)
Although the information of the positions of the faces is really only of the face of each person, I managed to enlarge the sides to appear more the complete head of the person. So if I have a one-sided Crop with the dimensions 80x120 I double up 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 make them equal but repositioning the top and left for Crop to be "centered".
Still considering the above example, I needed to get width and height at 120px (largest side) and top and left to be recalculated for the person’s face to have Crop centralized.
I’m not finding any formula to solve this issue.
I tried to find the biggest side and recalculated the top or 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 on Crop I did:
$image->crop($sizes[$bigger], $sizes[$bigger], $sizes['left'], $sizes['top']);
Unfortunately Crop is not set. Any suggestions?
Thank you.
If I understand, is the goal to make Crop centralize both horizontally and vertically? Also you want to make the width and height equal. Then Crop would be square?
– Sam
Exactly, but I can’t lose face centered on that.
– rodrigoum
I changed the question above where I try to recalculate the top or left according to the bigger side so the question becomes clearer the way I tried to do.
– rodrigoum