PHP - 如何获取旋转后点的坐标?

PHP - How to get the coordinates of the point after rotation?

我希望图片中的眼睛是水平的

$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.

如何获取旋转后每只眼睛的坐标?

我想要那些变量在顶部

FROM:

右眼Y = 446

rightEyeX = 625

左眼Y = 433

leftEyeX = 733

TO:

右眼Y = 432

rightEyeX = 640

左眼Y = 432

leftEyeX = 749

我尝试了一些东西,但得到了其他坐标。它看起来很适合我。诀窍是旋转平移到中心。我认为不同之处在于 -6.83 的角度是错误的(您的 OP 代码中的眼睛距离)。

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);

然后给我

L: (734.56131177907, 734.56131177907)
R: (628.87375746869, 418.91568508316)

但是图像看起来是这样的,左边是蓝色,右边是红色。下面一对是原点,上面一对旋转了 -6.83 度。

二维旋转矩阵和平移代码

$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];
};

这里是pastebin的完整代码。