首页  >  文章  >  后端开发  >  GIFDecoder的排错以及修改另附完整代码和demo

GIFDecoder的排错以及修改另附完整代码和demo

WBOY
WBOY原创
2016-08-08 09:23:541276浏览

前言

好久没有写技术类的博客了,今天有些小的收获,记录下来,留作备份

Gif图片的处理

由于业务需求,需要对gif动图的第一帧进行截取,然后我就搜索,发现了GIFDecoder这样的一个类,是做gif图片的处理的,怎奈国内人博客环境还是那么差,各种网站博客到处抄抄抄,没有一个完整的内容,经过多个站的资料整理,终于能用了。

出现了异常

在运行demo的时候,遇到了显示错误的问题

Notice: Undefined offset: 4 in /Applications/XAMPP/xamppfiles/htdocs/giftest/gifdecoder.class.php on line 83

查看源码发现83行是这样的

<code><span><span>function</span><span>GIFReadExtensions</span><span>()</span> {</span>
            GIFDecoder::GIFGetByte(<span>1</span>);
            <span>if</span> (<span>$this</span>->GIF_buffer [<span>0</span>] == <span>0xff</span>) {
                <span>for</span> (;;) {
                    GIFDecoder::GIFGetByte(<span>1</span>);
                    <span>if</span> (( <span>$u</span> = <span>$this</span>->GIF_buffer [<span>0</span>] ) == <span>0x00</span>) {
                        <span>break</span>;
                    }
                    GIFDecoder::GIFGetByte(<span>$u</span>);
                    <span>if</span> (<span>$u</span> == <span>0x03</span>) {
                        <span>$this</span>->GIF_anloop = ( <span>$this</span>->GIF_buffer [<span>1</span>] | <span>$this</span>->GIF_buffer [<span>2</span>] 8 );
                    }
                }
            } <span>else</span> {
                <span>for</span> (;;) {
                    GIFDecoder::GIFGetByte(<span>1</span>);
                    <span>if</span> (( <span>$u</span> = <span>$this</span>->GIF_buffer [<span>0</span>] ) == <span>0x00</span>) {
                        <span>break</span>;
                    }
                    GIFDecoder::GIFGetByte(<span>$u</span>);
                    <span>if</span> (<span>$u</span> == <span>0x04</span>) {
                        <span>if</span> (<span>$this</span>->GIF_buffer [<span>4</span>] & <span>0x80</span>) {   <span>//这里是第83行</span><span>$this</span>->GIF_dispos [] = ( <span>$this</span>->GIF_buffer [<span>0</span>] >> <span>2</span> ) - <span>1</span>;
                        } <span>else</span> {
                            <span>$this</span>->GIF_dispos [] = ( <span>$this</span>->GIF_buffer [<span>0</span>] >> <span>2</span> ) - <span>0</span>;
                        }
                        <span>$this</span>->GIF_delays [] = ( <span>$this</span>->GIF_buffer [<span>1</span>] | <span>$this</span>->GIF_buffer [<span>2</span>] 8 );
                        <span>if</span> (<span>$this</span>->GIF_buffer [<span>3</span>]) {
                            <span>$this</span>->GIF_TransparentI = <span>$this</span>->GIF_buffer [<span>3</span>];
                        }
                    }
                }
            }
        }</code>

原因

经过搜索引擎的努力,我得到的比较正规的解释是

offset:接下去的数字是出错的数组下标,一般是超出了数组的取值范围,如定义了数组A[]10,A[10]就会出现错误(Notice: Undefined offset: 10 ….),因为数组的下标是从0开始的,所以这个数组的下标就只能是0~9.

所以,原因就是

数组找不到下标为4的元素

解决方案

我们需要判断下标是不是存在

<code><span>if</span> (<span>isset</span>(<span>$this</span>->GIF_buffer [<span>4</span>]) & <span>0x80</span>) { </code>

酱紫就搞定啦,啦啦啦~

源码

<code><span><span><span><?php </span>/**
 * GIFDecoder  <br>
 * 
 * 日期: 2015年05月21日   <br>
 * 原作者: 不详           <br>
 * 修改: CalvinLee       <br>
 * 
 * 
 * <code>
 * require_once("gifdecoder.class.php");
 *
 * $FIC = "test.gif";
 * //获取gif的第一帧进行保存
 * if (file_exists($FIC)) {
 *      $GIF_frame = fread(fopen($FIC, 'rb'), filesize($FIC));
 *      $decoder = new GIFDecoder($GIF_frame);
 *      $frames = $decoder->GIFGetFrames();
 *      $i = 0; 
 *      $fname =  rand(1000, 9999). $FIC . "_0$i.png"; 
 *      $hfic = fopen("" . $fname, "wb");
 *      fwrite($hfic, $frames [$i]);
 *      fclose($hfic);
 * } 
 *  </code>
 * 
 * 
 *<span> @copyright</span> (c) 2015, Calvin Lee
 *<span> @author</span> Calvin Lee <diandianxiyu_>

 */</diandianxiyu_></span><span><span>Class</span><span>GIFDecoder</span> {</span><span>public</span><span>$GIF_TransparentR</span> = -<span>1</span>;
    <span>public</span><span>$GIF_TransparentG</span> = -<span>1</span>;
    <span>public</span><span>$GIF_TransparentB</span> = -<span>1</span>;
    <span>public</span><span>$GIF_TransparentI</span> = <span>0</span>;
    <span>public</span><span>$GIF_buffer</span> = <span>array</span>();
    <span>public</span><span>$GIF_arrays</span> = <span>array</span>();
    <span>public</span><span>$GIF_delays</span> = <span>array</span>();
    <span>public</span><span>$GIF_dispos</span> = <span>array</span>();
    <span>public</span><span>$GIF_stream</span> = <span>""</span>;
    <span>public</span><span>$GIF_string</span> = <span>""</span>;
    <span>public</span><span>$GIF_bfseek</span> = <span>0</span>;
    <span>public</span><span>$GIF_anloop</span> = <span>0</span>;
    <span>public</span><span>$GIF_screen</span> = <span>array</span>();
    <span>public</span><span>$GIF_global</span> = <span>array</span>();
    <span>public</span><span>$GIF_sorted</span>;
    <span>public</span><span>$GIF_colorS</span>;
    <span>public</span><span>$GIF_colorC</span>;
    <span>public</span><span>$GIF_colorF</span>;

    <span><span>function</span><span>GIFDecoder</span><span>(<span>$GIF_pointer</span>)</span> {</span><span>$this</span>->GIF_stream = <span>$GIF_pointer</span>;

        GIFDecoder::GIFGetByte(<span>6</span>);
        GIFDecoder::GIFGetByte(<span>7</span>);

        <span>$this</span>->GIF_screen = <span>$this</span>->GIF_buffer;
        <span>$this</span>->GIF_colorF = <span>$this</span>->GIF_buffer [<span>4</span>] & <span>0x80</span> ? <span>1</span> : <span>0</span>;
        <span>$this</span>->GIF_sorted = <span>$this</span>->GIF_buffer [<span>4</span>] & <span>0x08</span> ? <span>1</span> : <span>0</span>;
        <span>$this</span>->GIF_colorC = <span>$this</span>->GIF_buffer [<span>4</span>] & <span>0x07</span>;
        <span>$this</span>->GIF_colorS = <span>2</span> $this</span>->GIF_colorC;

        <span>if</span> (<span>$this</span>->GIF_colorF == <span>1</span>) {
            GIFDecoder::GIFGetByte(<span>3</span> * <span>$this</span>->GIF_colorS);
            <span>$this</span>->GIF_global = <span>$this</span>->GIF_buffer;
        }
        <span>for</span> (<span>$cycle</span> = <span>1</span>; <span>$cycle</span>;) {
            <span>if</span> (GIFDecoder::GIFGetByte(<span>1</span>)) {
                <span>switch</span> (<span>$this</span>->GIF_buffer [<span>0</span>]) {
                    <span>case</span><span>0x21</span>:
                        GIFDecoder::GIFReadExtensions();
                        <span>break</span>;
                    <span>case</span><span>0x2C</span>:
                        GIFDecoder::GIFReadDescriptor();
                        <span>break</span>;
                    <span>case</span><span>0x3B</span>:
                        <span>$cycle</span> = <span>0</span>;
                        <span>break</span>;
                }
            } <span>else</span> {
                <span>$cycle</span> = <span>0</span>;
            }
        }
    }

    <span><span>function</span><span>GIFReadExtensions</span><span>()</span> {</span>
        GIFDecoder::GIFGetByte(<span>1</span>);
        <span>if</span> (<span>$this</span>->GIF_buffer [<span>0</span>] == <span>0xff</span>) {
            <span>for</span> (;;) {
                GIFDecoder::GIFGetByte(<span>1</span>);
                <span>if</span> (( <span>$u</span> = <span>$this</span>->GIF_buffer [<span>0</span>] ) == <span>0x00</span>) {
                    <span>break</span>;
                }
                GIFDecoder::GIFGetByte(<span>$u</span>);
                <span>if</span> (<span>$u</span> == <span>0x03</span>) {
                    <span>$this</span>->GIF_anloop = ( <span>$this</span>->GIF_buffer [<span>1</span>] | <span>$this</span>->GIF_buffer [<span>2</span>] 8</span> );
                }
            }
        } <span>else</span> {
            <span>for</span> (;;) {
                GIFDecoder::GIFGetByte(<span>1</span>);
                <span>if</span> (( <span>$u</span> = <span>$this</span>->GIF_buffer [<span>0</span>] ) == <span>0x00</span>) {
                    <span>break</span>;
                }
                GIFDecoder::GIFGetByte(<span>$u</span>);
                <span>if</span> (<span>$u</span> == <span>0x04</span>) {
                    <span>if</span> (<span>isset</span>(<span>$this</span>->GIF_buffer [<span>4</span>]) & <span>0x80</span>) {
                        <span>$this</span>->GIF_dispos [] = ( <span>$this</span>->GIF_buffer [<span>0</span>] >> <span>2</span> ) - <span>1</span>;
                    } <span>else</span> {
                        <span>$this</span>->GIF_dispos [] = ( <span>$this</span>->GIF_buffer [<span>0</span>] >> <span>2</span> ) - <span>0</span>;
                    }
                    <span>$this</span>->GIF_delays [] = ( <span>$this</span>->GIF_buffer [<span>1</span>] | <span>$this</span>->GIF_buffer [<span>2</span>] 8 );
                    <span>if</span> (<span>$this</span>->GIF_buffer [<span>3</span>]) {
                        <span>$this</span>->GIF_TransparentI = <span>$this</span>->GIF_buffer [<span>3</span>];
                    }
                }
            }
        }
    }

    <span><span>function</span><span>GIFReadDescriptor</span><span>()</span> {</span><span>$GIF_screen</span> = <span>Array</span>();

        GIFDecoder::GIFGetByte(<span>9</span>);
        <span>$GIF_screen</span> = <span>$this</span>->GIF_buffer;
        <span>$GIF_colorF</span> = <span>$this</span>->GIF_buffer [<span>8</span>] & <span>0x80</span> ? <span>1</span> : <span>0</span>;
        <span>if</span> (<span>$GIF_colorF</span>) {
            <span>$GIF_code</span> = <span>$this</span>->GIF_buffer [<span>8</span>] & <span>0x07</span>;
            <span>$GIF_sort</span> = <span>$this</span>->GIF_buffer [<span>8</span>] & <span>0x20</span> ? <span>1</span> : <span>0</span>;
        } <span>else</span> {
            <span>$GIF_code</span> = <span>$this</span>->GIF_colorC;
            <span>$GIF_sort</span> = <span>$this</span>->GIF_sorted;
        }
        <span>$GIF_size</span> = <span>2</span> $GIF_code;
        <span>$this</span>->GIF_screen [<span>4</span>] &= <span>0x70</span>;
        <span>$this</span>->GIF_screen [<span>4</span>] |= <span>0x80</span>;
        <span>$this</span>->GIF_screen [<span>4</span>] |= <span>$GIF_code</span>;
        <span>if</span> (<span>$GIF_sort</span>) {
            <span>$this</span>->GIF_screen [<span>4</span>] |= <span>0x08</span>;
        }

        <span>//GIF Data Begin</span><span>if</span> (<span>$this</span>->GIF_TransparentI) {
            <span>$this</span>->GIF_string = <span>"GIF89a"</span>;
        } <span>else</span> {
            <span>$this</span>->GIF_string = <span>"GIF87a"</span>;
        }
        GIFDecoder::GIFPutByte(<span>$this</span>->GIF_screen);
        <span>if</span> (<span>$GIF_colorF</span> == <span>1</span>) {
            GIFDecoder::GIFGetByte(<span>3</span> * <span>$GIF_size</span>);
            <span>if</span> (<span>$this</span>->GIF_TransparentI) {
                <span>$this</span>->GIF_TransparentR = <span>$this</span>->GIF_buffer [<span>3</span> * <span>$this</span>->GIF_TransparentI + <span>0</span>];
                <span>$this</span>->GIF_TransparentG = <span>$this</span>->GIF_buffer [<span>3</span> * <span>$this</span>->GIF_TransparentI + <span>1</span>];
                <span>$this</span>->GIF_TransparentB = <span>$this</span>->GIF_buffer [<span>3</span> * <span>$this</span>->GIF_TransparentI + <span>2</span>];
            }
            GIFDecoder::GIFPutByte(<span>$this</span>->GIF_buffer);
        } <span>else</span> {
            <span>if</span> (<span>$this</span>->GIF_TransparentI) {
                <span>$this</span>->GIF_TransparentR = <span>$this</span>->GIF_global [<span>3</span> * <span>$this</span>->GIF_TransparentI + <span>0</span>];
                <span>$this</span>->GIF_TransparentG = <span>$this</span>->GIF_global [<span>3</span> * <span>$this</span>->GIF_TransparentI + <span>1</span>];
                <span>$this</span>->GIF_TransparentB = <span>$this</span>->GIF_global [<span>3</span> * <span>$this</span>->GIF_TransparentI + <span>2</span>];
            }
            GIFDecoder::GIFPutByte(<span>$this</span>->GIF_global);
        }
        <span>if</span> (<span>$this</span>->GIF_TransparentI) {
            <span>$this</span>->GIF_string .= <span>"!\xF9\x04\x1\x0\x0"</span> . chr(<span>$this</span>->GIF_TransparentI) . <span>"\x0"</span>;
        }
        <span>$this</span>->GIF_string .= chr(<span>0x2C</span>);
        <span>$GIF_screen</span> [<span>8</span>] &= <span>0x40</span>;
        GIFDecoder::GIFPutByte(<span>$GIF_screen</span>);
        GIFDecoder::GIFGetByte(<span>1</span>);
        GIFDecoder::GIFPutByte(<span>$this</span>->GIF_buffer);
        <span>for</span> (;;) {
            GIFDecoder::GIFGetByte(<span>1</span>);
            GIFDecoder::GIFPutByte(<span>$this</span>->GIF_buffer);
            <span>if</span> (( <span>$u</span> = <span>$this</span>->GIF_buffer [<span>0</span>] ) == <span>0x00</span>) {
                <span>break</span>;
            }
            GIFDecoder::GIFGetByte(<span>$u</span>);
            GIFDecoder::GIFPutByte(<span>$this</span>->GIF_buffer);
        }
        <span>$this</span>->GIF_string .= chr(<span>0x3B</span>);
        <span>//GIF Data End</span><span>$gif_array</span> = &<span>$this</span>->GIF_arrays;
        <span>$gif_array</span>[] = <span>$this</span>->GIF_string;
    }

    <span><span>function</span><span>GIFGetByte</span><span>(<span>$len</span>)</span> {</span><span>$this</span>->GIF_buffer = <span>Array</span>();
        <span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> $len; <span>$i</span>++) {
            <span>if</span> (<span>$this</span>->GIF_bfseek > strlen(<span>$this</span>->GIF_stream)) {
                <span>return</span><span>0</span>;
            }
            <span>$this</span>->GIF_buffer[] = ord(<span>$this</span>->GIF_stream { <span>$this</span>->GIF_bfseek++});
        }
        <span>return</span><span>1</span>;
    }

    <span><span>function</span><span>GIFPutByte</span><span>(<span>$bytes</span>)</span> {</span><span>foreach</span> (<span>$bytes</span><span>as</span><span>$byte</span>) {
            <span>$this</span>->GIF_string .= chr(<span>$byte</span>);
        }
    }

    <span><span>function</span><span>GIFGetFrames</span><span>()</span> {</span><span>return</span> ( <span>$this</span>->GIF_arrays );
    }

    <span><span>function</span><span>GIFGetDelays</span><span>()</span> {</span><span>return</span> ( <span>$this</span>->GIF_delays );
    }

    <span><span>function</span><span>GIFGetLoop</span><span>()</span> {</span><span>return</span> ( <span>$this</span>->GIF_anloop );
    }

    <span><span>function</span><span>GIFGetDisposal</span><span>()</span> {</span><span>return</span> ( <span>$this</span>->GIF_dispos );
    }

    <span><span>function</span><span>GIFGetTransparentR</span><span>()</span> {</span><span>return</span> ( <span>$this</span>->GIF_TransparentR );
    }

    <span><span>function</span><span>GIFGetTransparentG</span><span>()</span> {</span><span>return</span> ( <span>$this</span>->GIF_TransparentG );
    }

    <span><span>function</span><span>GIFGetTransparentB</span><span>()</span> {</span><span>return</span> ( <span>$this</span>->GIF_TransparentB );
    }

}

<span>?></span></code>

后记

  • 小的知识积累最后会有质变
  • 善于总结永远没有坏处
  • 一个功能写三遍就会变得不一样,如果还一样的话那就是自己的问题了

以上就介绍了GIFDecoder的排错以及修改另附完整代码和demo,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn