【问题标题】:imagecolorallocate(): Red component is out of rangeimagecolorallocate():红色分量超出范围
【发布时间】:2020-06-07 16:38:03
【问题描述】:

我对 PHP 7.4.5 和 GD 有疑问。我有一个图像处理脚本,有时我会收到此错误:

imagecolorallocate(): 红色分量超出范围

有人知道如何避免这种情况吗?

$image = imagecreatetruecolor($calcWidth, $calcHeight);
for ($y = 0; $y < $height; ++$y) {
       for ($x = 0; $x < $width; ++$x) {
            [$r, $g, $b] = $pixels[$y][$x];
            $allocate = imagecolorallocate($image, $r, $g, $b);
            imagesetpixel($image, $x, $y, $allocate);
        }
  }

【问题讨论】:

  • 是的,这是我在整个网络上找到的关于此错误的唯一答案。但这并没有帮助,因为我没有透明图像。
  • 即使没有透明度,这个答案也应该有所帮助.. 因为使用 imagecolorstotal() 应该可以让您检测总颜色是否在范围内?

标签: php gd


【解决方案1】:

我用这个条件修复了它,因为有时 r/g/b 值为 256,而 rgb 最多只能有 255:

[$r, $g, $b] = $pixels[$y][$x];
if($r > 255) { $r = 255; }
if($g > 255) { $g = 255; }
if($b > 255) { $b = 255; }
if($r < 0) { $r = 0; }
if($g < 0) { $g = 0; }
if($b < 0) { $b = 0; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多