【问题标题】:Why cant I make the background of a png transparent after rotating it with php?为什么我不能在用 php 旋转 png 后使其背景透明?
【发布时间】:2011-05-08 16:08:42
【问题描述】:

我昨天一整天都在尝试解决这个问题。我通过 imagerotate() 旋转图像。我得到一个黑色背景,图像不再覆盖。我已经尝试了所有我想不到的方法来使背景透明..

这是我当前的代码..

   function rotate($degrees) {
       $image = $this->image;
       imagealphablending($image, false);
       $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
       $rotate = imagerotate($image, $degrees, $color);
       imagecolortransparent($rotate, $color);
       imagesavealpha($image, true);
       $this->image = $rotate;
   }

我真的开始被打勾了。有人可以给我看一些工作代码吗?请问?

我的 WAMP 服务器和 Dreamweaver 有问题吗?因为我什至试过这个..http://www.exorithm.com/algorithm/view/rotate_image_alpha 它仍然会显示黑色或白色背景..

【问题讨论】:

  • 似乎与昨天的问题重复。 stackoverflow.com/questions/4148774/…
  • 是的。我知道我真的不应该再发帖了,但你不知道我多么希望这段代码能够正常工作..
  • 请指定您的 PHP 版本。

标签: php png transparency gd


【解决方案1】:

尝试在旋转后的图像上设置 imagesavealpha()。

目前您正在原始图像上运行 imagesavealpha()。 [例如。 imagesavealpha($image, true); ]

相反,您想在旋转后的图像上运行 imagesavealpha(),然后设置 $this->image... 尝试:

   ...
   $rotate = imagerotate($image, $degrees, $color);
   imagecolortransparent($rotate, $color);
   imagesavealpha($rotate, true);  // <- using $rotate instead of $image
   $this->image = $rotate;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 2017-06-30
    • 1970-01-01
    • 2013-12-10
    • 2019-01-14
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多