Php 更改Imagerotate的旋转中心

Php 更改Imagerotate的旋转中心,php,Php,使用给定角度(以度为单位)旋转图像 旋转中心是图像的中心,旋转后的图像可能与原始图像具有不同的尺寸 如何将旋转中心更改为坐标x_new和y_new,并避免自动调整大小 示例:围绕红点旋转 想到的第一个想法是移动图像,使其新中心位于x_new,y_new旋转图像并向后移动 假设: 0 < x_new < w 0 < y_new < h 现在,您只需从中剪切出感兴趣的零件。正确的方法是旋转,然后使用正确的变换参数进行裁剪 另一种方法是移动,旋转,然后再次移动(更简单的数学,

使用给定角度(以度为单位)旋转图像

旋转中心是图像的中心,旋转后的图像可能与原始图像具有不同的尺寸

如何将旋转中心更改为坐标x_new和y_new,并避免自动调整大小

示例:围绕红点旋转


想到的第一个想法是移动图像,使其新中心位于x_new,y_new旋转图像并向后移动

假设:

0 < x_new < w
0 < y_new < h

现在,您只需从中剪切出感兴趣的零件。

正确的方法是旋转,然后使用正确的变换参数进行裁剪

另一种方法是移动,旋转,然后再次移动(更简单的数学,但更多的代码)

$x和$y是红点的坐标

private function rotateImage($image, $x, $y, $angle)
{
    $widthOrig = imagesx($image);
    $heightOrig = imagesy($image);
    $rotatedImage = $this->createLayer($widthOrig * 2, $heightOrig * 2);
    imagecopyresampled($rotatedImage, $image, $widthOrig - $x, $heightOrig - $y, 0, 0, $widthOrig, $heightOrig, $widthOrig, $heightOrig);
    $rotatedImage = imagerotate($rotatedImage, $angle, imageColorAllocateAlpha($rotatedImage, 0, 0, 0, 127));
    $width = imagesx($rotatedImage);
    $height = imagesy($rotatedImage);
    $image = $this->createLayer();
    imagecopyresampled($image, $rotatedImage, 0, 0, $width / 2 - $x, $height / 2 - $y, $widthOrig, $heightOrig, $widthOrig, $heightOrig);
    return $image;
}

private function createLayer($width = 1080, $height = 1080)
{
    $image = imagecreatetruecolor($width, $height);
    $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
    imagefill($image, 0, 0, $color);
    return $image;
}

老实说,我没有明白。你能添加一个例子吗?除非你旋转180度的倍数(如果图像是正方形,则旋转90倍),否则无论你选择哪个旋转中心,图像总是需要重新调整大小。如果不想重新调整尺寸,则应在旋转后裁剪出所需的零件。
private function rotateImage($image, $x, $y, $angle)
{
    $widthOrig = imagesx($image);
    $heightOrig = imagesy($image);
    $rotatedImage = $this->createLayer($widthOrig * 2, $heightOrig * 2);
    imagecopyresampled($rotatedImage, $image, $widthOrig - $x, $heightOrig - $y, 0, 0, $widthOrig, $heightOrig, $widthOrig, $heightOrig);
    $rotatedImage = imagerotate($rotatedImage, $angle, imageColorAllocateAlpha($rotatedImage, 0, 0, 0, 127));
    $width = imagesx($rotatedImage);
    $height = imagesy($rotatedImage);
    $image = $this->createLayer();
    imagecopyresampled($image, $rotatedImage, 0, 0, $width / 2 - $x, $height / 2 - $y, $widthOrig, $heightOrig, $widthOrig, $heightOrig);
    return $image;
}

private function createLayer($width = 1080, $height = 1080)
{
    $image = imagecreatetruecolor($width, $height);
    $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
    imagefill($image, 0, 0, $color);
    return $image;
}