Php Gd 旋转图像

发布于 2024-07-17 03:52:50 字数 149 浏览 5 评论 0原文

你好,

我正在尝试围绕中心旋转圆形图像,然后剪掉两侧。 我看到 imagerotate 函数,但它似乎没有绕中心旋转。

有人有什么建议吗?

谢谢。

更新:由于它是一个圆,我想剪掉边缘并使圆保持相同的尺寸。

HEllo,

I am trying to rotate a circular image around the center and then cut off the sides. I see the imagerotate function, but it does not seem to rotate about centre.

Anyone have any suggestions?

Thank you.

Update: Since it is a circle, I want to cut off the edges and keep my circle in the same dimensions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红尘作伴 2024-07-24 03:52:50

我使用以下代码成功地解决了这个问题

    $width_before = imagesx($img1);
    $height_before = imagesy($img1);
    $img1 = imagerotate($img1, $angle, $mycolor);

    //but imagerotate scales, so we clip to the original size

    $img2 = @imagecreatetruecolor($width_before, $height_before);
    $new_width = imagesx($img1); // whese dimensions are
    $new_height = imagesy($img1);// the scaled ones (by imagerotate)
    imagecopyresampled(
        $img2, $img1,
        0, 0,
        ($new_width-$width_before)/2,
        ($new_height-$height_before)/2,
        $width_before,
        $height_before,
        $width_before,
        $height_before
    );
    $img1 = $img2;
    // now img1 is center rotated and maintains original size

希望它有帮助。

再见

I faced successfully that problem with the following code

    $width_before = imagesx($img1);
    $height_before = imagesy($img1);
    $img1 = imagerotate($img1, $angle, $mycolor);

    //but imagerotate scales, so we clip to the original size

    $img2 = @imagecreatetruecolor($width_before, $height_before);
    $new_width = imagesx($img1); // whese dimensions are
    $new_height = imagesy($img1);// the scaled ones (by imagerotate)
    imagecopyresampled(
        $img2, $img1,
        0, 0,
        ($new_width-$width_before)/2,
        ($new_height-$height_before)/2,
        $width_before,
        $height_before,
        $width_before,
        $height_before
    );
    $img1 = $img2;
    // now img1 is center rotated and maintains original size

Hope it helps.

Bye

走走停停 2024-07-24 03:52:50

文档说它确实旋转围绕中心。

不幸的是,它还说它将缩放图像以使其仍然适合。 这意味着无论您执行什么操作,此功能都会改变内部圆形图像的大小。

您可以(相对容易地)计算将发生多少缩放,然后预先适当地预先缩放图像。

如果您有 PHP“ImageMagick”函数可用,您可以使用它们来代替 - 它们显然不会缩放图像。

The documentation says that it does rotate around the center.

Unfortunately it also says that it will scale the image so that it still fits. That means that whatever you do this function will change the size of your internal circular image.

You could (relatively easily) calculate how much scaling down will happen and then prescale the image up appropriately beforehand.

If you have the PHP "ImageMagick" functions available you can use those instead - they apparently don't scale the image.

怪我闹别瞎闹 2024-07-24 03:52:50

根据PHP手册imagerotate()页面:

旋转中心为中心
的图像,旋转后的图像是
缩小以便整体旋转
图像适合目标图像 -
边缘未被剪裁。

也许图像的可见中心不是实际中心?

According to the PHP manual imagerotate() page:

The center of rotation is the center
of the image, and the rotated image is
scaled down so that the whole rotated
image fits in the destination image -
the edges are not clipped.

Perhaps the visible center of the image is not the actual center?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文