我在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);
}
}
我在这种情况下修复了它,因为有时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; }