可能导致“颜色索引超出范围"的原因是什么? imagecolorsforindex()错误? [英] What could cause a "color index out of range" error for imagecolorsforindex()?

查看:82
本文介绍了可能导致“颜色索引超出范围"的原因是什么? imagecolorsforindex()错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将修补程序调整为一大堆JPG,PNG和GIF文件的大小时,PHP意外地死机,并显示以下错误消息:

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]: 颜色索引226超出范围

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已返回颜色索引,怎么可能不存在?

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

推荐答案

听起来imagecolortransparent($img)返回的索引大于所讨论图像的托盘尺寸.

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

透明色的索引是图像的属性,而不是托盘的属性,因此有可能使用该索引设置超出托盘大小来创建图像,但是我希望PHP能够在这种情况下已检测到此问题并从imagecolortransparent()返回-1.

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.

您可以通过将调用添加到 imagecolorstotal 到您的代码:

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

这篇关于可能导致“颜色索引超出范围"的原因是什么? imagecolorsforindex()错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆