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

问题描述 投票:0回答:1

我在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);
        }
  }
php gd
1个回答
0
投票

我在这种情况下修复了它,因为有时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; }
© www.soinside.com 2019 - 2024. All rights reserved.