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
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.