gpt4 book ai didi

php - 什么会导致 imagecolorsforindex() 出现 "color index out of range"错误?

转载 作者:可可西里 更新时间:2023-11-01 13:48:36 27 4
gpt4 key购买 nike

当对一大堆 JPG、PNG 和 GIF 文件进行补丁调整时,PHP 意外崩溃并显示以下错误消息:

imagecolorsforindex() [function.imagecolorsforindex]: Color index 226 out of range

相关代码片段为:

protected function preserveTransparency($img, $resized, $ftype) {

if (($ftype == IMAGETYPE_PNG) || ($ftype == IMAGETYPE_GIF)) {
$tidx = imagecolortransparent($img);
if ($tidx >= 0) {
$transColor = imagecolorsforindex($img, $tidx);
$tidx = imagecolorallocate($resized, $transColor['red'], $transColor['green'], $transColor['blue']);
imagefill($resized, 0, 0, $tidx);
imagecolortransparent($resized, $tidx);
} elseif ($ftype == IMAGETYPE_PNG) {
imagealphablending($resized, false);
imagesavealpha($resized, true);
$transparent = imagecolorallocatealpha($resized, 255, 255, 255, 127);
imagefill($resized, 0, 0, $transparent);
}
}
}

如果 imagecolortransparent 已经返回颜色索引,怎么可能不存在?

最佳答案

听起来 imagecolortransparent($img) 返回的索引大于相关图像的托盘大小。

透明度颜色的索引是图像的属性,而不是托盘的属性,因此可以使用托盘大小之外的索引集创建图像,但我希望 PHP 会已检测到这一点,并在这种情况下从 imagecolortransparent() 返回 -1。

您可以通过添加对 imagecolorstotal 的调用来检查这是否是正在发生的事情到你的代码:

    $tidx = imagecolortransparent($img);
$palletsize = imagecolorstotal($img);
if ($tidx >= 0 && $tidx < $palletsize) {
$transColor = imagecolorsforindex($img, $tidx);

关于php - 什么会导致 imagecolorsforindex() 出现 "color index out of range"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3874533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com