ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – 什么可能导致imagecolorsforindex()的“颜色索引超出范围”错误?

2019-06-10 14:18:32  阅读:235  来源: 互联网

标签:php image-processing imagemagick image-manipulation gd


当修补程序调整大量的JPG,PNG和GIF文件时,PHP会出现意外丢失,并显示以下错误消息:

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

解决方法:

听起来像imagecolortransparent($img)返回的索引大于相关图像的托盘大小.

透明度颜色的索引是图像的属性,而不是托盘的属性,因此有可能在托盘大小之外使用此索引集创建图像,但我希望PHP会检测到这个并在这种情况下从imagecolortransparent()返回-1.

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

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

标签:php,image-processing,imagemagick,image-manipulation,gd
来源: https://codeday.me/bug/20190610/1212557.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有