简体   繁体   English

PHP Gd旋转图像

[英]Php Gd rotate image

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. 我看到了imagerotate函数,但是它似乎没有绕中心旋转。

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. 更新:因为它是一个圆,所以我想剪掉边缘,使圆保持相同的尺寸。

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 再见

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. 如果您有可用的PHP“ ImageMagick”函数,则可以改用这些函数-它们显然不会缩放图像。

According to the PHP manual imagerotate() page: 根据PHP手册imagerotate()页面:

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? 也许图像的可见中心不是实际中心?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM