views:

72

answers:

1

When doing a patch resize to a big bunch of JPG, PNG and GIF files, PHP drops dead quite unexpectedly with the following error message:

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

The relevant code fragment is:

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);
        }
    }
}

How could a color index not exist if was already returned by imagecolortransparent?

A: 

It sounds like the index returned by imagecolortransparent($img) is larger than the pallet size of the image in question.

The index of the transparency color is a property of the image, rather than a property of the pallet, so it's possible that an image could be created with this index set outside the pallet size, but I would have hoped that PHP would have detected this and returned -1 from imagecolortransparent() in this situation.

You could check if this is what is happening by adding a call to imagecolorstotal to your code:

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