>  기사  >  백엔드 개발  >  GIFDecoder의 문제 해결 및 수정은 전체 코드 및 데모와 함께 첨부됩니다.

GIFDecoder의 문제 해결 및 수정은 전체 코드 및 데모와 함께 첨부됩니다.

WBOY
WBOY원래의
2016-08-08 09:23:541276검색

서문

오랫동안 기술 블로그를 작성하지 않았습니다. 오늘 소소한 이득이 있어서 기록해서 백업해두겠습니다.

Gif 이미지 처리

업무상 gif 애니메이션의 첫 번째 프레임을 가로채야 하는데, 검색해보니 gif 이미지를 처리하는 데 사용되는 GIFDecoder와 같은 클래스가 있습니다. 국내 블로그 환경이 아직 너무 열악해서 여러 사이트와 블로그에서 여기저기 복사해 놓았는데, 여러 사이트에서 정보를 정리하다보니 완성된 내용이 없네요.

예외가 발생했습니다

데모를 실행할 때 오류가 표시되었습니다

주의: 정의되지 않은 오프셋: 4 in /Applications/XAMPP/xamppfiles/ htdocs/giftest /gifdecoder.class.php 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>] << <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>$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>] << <span>8</span> );
                        <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]은 오류를 발생시킵니다(주의사항: 정의되지 않은 오프셋: 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_@live.cn>

 */</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> << <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>] << <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>] << <span>8</span> );
                    <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> << <span>$GIF_code</span>;
        <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> < <span>$len</span>; <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></span></span></code>

작은 지식 축적이 결국 질적 변화로 이어진다
  • 요약을 잘하면 나쁠 게 없다
  • 함수를 세 번 작성하면 결과가 달라집니다. 여전히 동일하다면 문제입니다.
위 내용은 GIFDecoder의 문제 해결 및 수정 사항을 소개합니다. 내용을 포함한 전체 코드와 데모가 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.