代码之家  ›  专栏  ›  技术社区  ›  Alec Smart

php gd旋转图像

  •  2
  • Alec Smart  · 技术社区  · 16 年前

    你好,

    我试着绕着中心旋转一个圆形图像,然后切掉两边。我看到了imagerotate函数,但它似乎没有围绕中心旋转。

    有人有什么建议吗?

    谢谢您。

    更新:因为它是一个圆,我想切断边缘,保持我的圆在相同的尺寸。

    3 回复  |  直到 10 年前
        1
  •  3
  •   Alnitak    16 年前

    这个 documentation 说它 围绕中心旋转。

    不幸的是,它还说,它将缩放图像,使其仍然适合。这意味着无论你做什么,这个函数都会改变你内部圆形图像的大小。

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

    如果您有php“imagemagick”函数 available 你可以用这些来代替-它们显然不能缩放图像。

        2
  •  4
  •   blex    10 年前

    我用下面的代码成功地解决了这个问题

        $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
    

    希望它有帮助。

    拜伊

        3
  •  0
  •   karim79    16 年前

    根据PHP手册 imagerotate() 页:

    旋转中心是中心 图像,旋转的图像是 缩小,使整个旋转 图像适合目标图像- 边缘没有修剪。

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

    推荐文章