【问题标题】:Php Gd rotate imagePHP Gd 旋转图像
【发布时间】:2015-10-23 16:35:28
【问题描述】:

你好,

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

大家有什么建议吗?

谢谢。

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

【问题讨论】:

  • 顺便说一句,PHP 最近发布了一个更新,修复了 GD imagerotate 的一个安全漏洞......只是觉得这是一个有趣的花絮。

标签: php gd


【解决方案1】:

documentation 表示它确实围绕中心旋转。

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

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

如果你有 PHP "ImageMagick" 函数 available 你可以使用它们——它们显然不会缩放图像。

【讨论】:

  • 先调整大小然后然后旋转应该会产生质量更好的图像。
  • 确实 - 是的。 GD 图像旋转总是按比例缩小,所以最好先按比例放大。将编辑修复。
【解决方案2】:

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

    $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】:

    根据PHP手册imagerotate()页面:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 2010-12-19
      • 2015-02-18
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      相关资源
      最近更新 更多