PHP - How to get the coordinates of the point after rotation?
I want the eyes in the image to be horizontal
$rightEyeY = 446;
$rightEyeX = 625;
$leftEyeY = 433;
$leftEyeX = 733;
// Get middle point of two eyes
$y = $rightEyeY - $leftEyeY;
$x = $rightEyeX - $leftEyeX;
$angle = rad2deg(atan2($y, $x)) - 180; // -6.8 degrees
$manager = new ImageManager(['driver' => 'imagick']);
$image = $manager->make('image.jpg')->rotate($angle);
$a = $angle * pi() / 180.0;
$cosa = cos($a);
$sina = sin($a);
$x = $x * $cosa - $y * $sina; // This one calculates x of the middle point not each eye.
$y = $x * $sina + $y * $cosa; // This one calculates y of the middle point not each eye.
How can I get the coordinates of each eye after rotation?
I want those variables at the top
FROM:
rightEyeY = 446
rightEyeX = 625
leftEyeY = 433
leftEyeX = 733
TO:
rightEyeY = 432
rightEyeX = 640
leftEyeY = 432
leftEyeX = 749
Answer
Solution:
I tried something, but got other coordinates. It looks right for me. The trick is the rotation translating to the center. I think the difference comes from the angle of -6.83 to be wrong (distance of eyes in your OP code).
If you do not translate, the rotation will be done at (0,0) the origin of the coordinate system which is then top left corner in image space, but you want the center.
$angle = deg2rad(-6.83);
list($leftX, $leftY) = $rotateEye($leftEyeX, $leftEyeY, $angle);
list($rightX, $rightY) = $rotateEye($rightEyeX, $rightEyeY, $angle);
and gives me
L: (734.56131177907, 734.56131177907)
R: (628.87375746869, 418.91568508316)
But the image looks like that, blue left, red right. The bottom pair is the origin, the top one rotated by -6.83 degree.
2D rotation matrix and translation code
$rotateEye = function ($x, $y, $angle) use ($centerX, $centerY): array {
$tx = $x - $centerX;
$ty = $y - $centerY;
$rx = cos($angle) * $tx - sin($angle) * $ty;
$ry = sin($angle) * $tx + cos($angle) * $ty;
return [$rx + $centerX, $ry + $centerY];
};
Here a pastebin of the complete code.
Source