当对一大堆 JPG、PNG 和 GIF 文件进行补丁大小调整时,PHP 意外地死机,并显示以下错误消息:
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
?