2010-10-06 25 views
4

在做一個補丁的大小調整爲一大束的JPG,PNG和GIF文件,PHP下降死非常意外,出現以下錯誤信息:什麼可能導致imagecolorsforindex()的「顏色索引超出範圍」錯誤?

imagecolorsforindex()[function.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返回的顯色指數不存在?

回答

7

聽起來像imagecolortransparent($img)返回的索引大於所討論圖像的托盤大小。

透明度顏色的索引是圖像的一個屬性,而不是托盤的屬性,因此可以使用該托盤大小以外的索引集創建圖像,但我希望PHP會在這種情況下檢測到這個並從imagecolortransparent()返回-1。

你可以檢查如果這是通過向imagecolorstotal調用你的代碼發生:

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

或... imagecolorsforindex($ IMG,imagecolorstotal($ IMG) - 1); – Johny 2018-01-22 15:15:27