【问题标题】:'imagecolorat' and transparency'imagecolorat' 和透明度
【发布时间】:2011-08-07 20:37:48
【问题描述】:

如何获得图像上像素的透明度值?

'imagecolorat' 只选取图像中指定位置的像素颜色索引。使用该索引,我可以获得 RGB 值,但不能获得透明值。

希望您能理解,并在此先感谢您。

【问题讨论】:

    标签: php image transparency pixel


    【解决方案1】:

    根据 PHP 手册 imagecolorat 返回指定 X/Y 坐标处的颜色索引(我假设这是针对 GIF 和/或 PNG-8)。

    如果您知道索引,那么问题是确定文件中的哪个索引是透明的。

    imagecolortransparent 可能值得一看,imagecolorsforindex 也可能有帮助。

    【讨论】:

      【解决方案2】:

      据我所知,透明度值是由函数imagecolorat返回的。你可以试试:

      $color        = imagecolorat($image, $x, $y);
      $transparency = ($color >> 24) & 0x7F;
      

      透明度是 0 到 127 之间的整数,所以我们需要屏蔽 32 位颜色整数的前 8 位。

      【讨论】:

      • 哦,谢谢!我不知道 imagecolorat 实际上包含 alpha 值。
      • 注意第 4 个字节是透明的,不是不透明的。因此0 表示完全不透明,127 表示完全透明。在其他环境中,这可能会相反,例如在 css 中,0.0 是完全透明的,1.0 是完全不透明的。
      • 32 位值有多大?因为imagecolorat 正在返回 16777215。
      【解决方案3】:

      解决办法如下:

      $colorIndex = imagecolorat($img, $x, $y);
      $colorInfo = imagecolorsforindex($img, $colorIndex);
      print_r($colorInfo);
      

      这将打印类似:

      Array
      (
         [red] => 226
         [green] => 222
         [blue] => 252
         [alpha] => 0
      )
      

      其中 [alpha] 是您的透明度值...(从 0 到 127,其中 0 完全不透明,127 完全透明)

      享受吧!

      【讨论】:

      • 是的,我同意。感谢您的回答。
      • hmmm 我认为 alpha 值是从 0 到 255 ,0 表示透明,255 表示不透明
      猜你喜欢
      • 2014-05-20
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 2018-05-18
      • 2018-01-19
      相关资源
      最近更新 更多