Php 什么会导致“a”;“颜色索引超出范围”;imagecolorsforindex()的错误?

Php 什么会导致“a”;“颜色索引超出范围”;imagecolorsforindex()的错误?,php,image-processing,image-manipulation,imagemagick,gd,Php,Image Processing,Image Manipulation,Imagemagick,Gd,当对大量JPG、PNG和GIF文件进行补丁调整时,PHP会意外地死掉,并显示以下错误消息: imagecolorsforindex()[函数.imagecolorsforindex]: 颜色索引226超出范围 相关代码片段为: protected function preserveTransparency($img, $resized, $ftype) { if (($ftype == IMAGETYPE_PNG) || ($ftype == IMAGETYPE_GIF)) {

当对大量JPG、PNG和GIF文件进行补丁调整时,PHP会意外地死掉,并显示以下错误消息:

imagecolorsforindex()[函数.imagecolorsforindex]: 颜色索引226超出范围

相关代码片段为:

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

您可以通过向代码中添加对的调用来检查是否发生了这种情况:

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

或…imagecolorsforindex($img,imagecolorstotal($img)-1);我解决了这个问题,重新保存了图像文件。虽然我可以用
imagecreatepng()
打开它,并用
imagepng()
显示它。在谷歌搜索了很多次之后,它似乎与PHP无关,因为没有关于它的信息。。。