您好,欢迎访问一九零五行业门户网

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

前言
好久没有写技术类的博客了,今天有些小的收获,记录下来,留作备份
gif图片的处理
由于业务需求,需要对gif动图的第一帧进行截取,然后我就搜索,发现了gifdecoder这样的一个类,是做gif图片的处理的,怎奈国内人博客环境还是那么差,各种网站博客到处抄抄抄,没有一个完整的内容,经过多个站的资料整理,终于能用了。
出现了异常
在运行demo的时候,遇到了显示错误的问题
notice: undefined offset: 4 in /applications/xampp/xamppfiles/htdocs/giftest/gifdecoder.class.php on line 83
查看源码发现83行是这样的
functiongifreadextensions() { gifdecoder::gifgetbyte(1); if ($this->gif_buffer [0] == 0xff) { for (;;) { gifdecoder::gifgetbyte(1); if (( $u = $this->gif_buffer [0] ) == 0x00) { break; } gifdecoder::gifgetbyte($u); if ($u == 0x03) { $this->gif_anloop = ( $this->gif_buffer [1] | $this->gif_buffer [2] 8 ); } } } else { for (;;) { gifdecoder::gifgetbyte(1); if (( $u = $this->gif_buffer [0] ) == 0x00) { break; } gifdecoder::gifgetbyte($u); if ($u == 0x04) { if ($this->gif_buffer [4] & 0x80) { //这里是第83行$this->gif_dispos [] = ( $this->gif_buffer [0] >> 2 ) - 1; } else { $this->gif_dispos [] = ( $this->gif_buffer [0] >> 2 ) - 0; } $this->gif_delays [] = ( $this->gif_buffer [1] | $this->gif_buffer [2] 8 ); if ($this->gif_buffer [3]) { $this->gif_transparenti = $this->gif_buffer [3]; } } } } }
原因
经过搜索引擎的努力,我得到的比较正规的解释是
offset:接下去的数字是出错的数组下标,一般是超出了数组的取值范围,如定义了数组a[]有10个元数,如果出现了a[10]就会出现错误(notice: undefined offset: 10 ….),因为数组的下标是从0开始的,所以这个数组的下标就只能是0~9.
所以,原因就是
数组找不到下标为4的元素
解决方案
我们需要判断下标是不是存在
if (isset($this->gif_buffer [4]) & 0x80) {
酱紫就搞定啦,啦啦啦~
源码
gifgetframes(); * $i = 0; * $fname = rand(1000, 9999). $fic . _0$i.png; * $hfic = fopen( . $fname, wb); * fwrite($hfic, $frames [$i]); * fclose($hfic); * } * * * * @copyright (c) 2015, calvin lee * @author calvin lee */classgifdecoder {public$gif_transparentr = -1; public$gif_transparentg = -1; public$gif_transparentb = -1; public$gif_transparenti = 0; public$gif_buffer = array(); public$gif_arrays = array(); public$gif_delays = array(); public$gif_dispos = array(); public$gif_stream = ; public$gif_string = ; public$gif_bfseek = 0; public$gif_anloop = 0; public$gif_screen = array(); public$gif_global = array(); public$gif_sorted; public$gif_colors; public$gif_colorc; public$gif_colorf; functiongifdecoder($gif_pointer) {$this->gif_stream = $gif_pointer; gifdecoder::gifgetbyte(6); gifdecoder::gifgetbyte(7); $this->gif_screen = $this->gif_buffer; $this->gif_colorf = $this->gif_buffer [4] & 0x80 ? 1 : 0; $this->gif_sorted = $this->gif_buffer [4] & 0x08 ? 1 : 0; $this->gif_colorc = $this->gif_buffer [4] & 0x07; $this->gif_colors = 2 $this->gif_colorc; if ($this->gif_colorf == 1) { gifdecoder::gifgetbyte(3 * $this->gif_colors); $this->gif_global = $this->gif_buffer; } for ($cycle = 1; $cycle;) { if (gifdecoder::gifgetbyte(1)) { switch ($this->gif_buffer [0]) { case0x21: gifdecoder::gifreadextensions(); break; case0x2c: gifdecoder::gifreaddescriptor(); break; case0x3b: $cycle = 0; break; } } else { $cycle = 0; } } } functiongifreadextensions() { gifdecoder::gifgetbyte(1); if ($this->gif_buffer [0] == 0xff) { for (;;) { gifdecoder::gifgetbyte(1); if (( $u = $this->gif_buffer [0] ) == 0x00) { break; } gifdecoder::gifgetbyte($u); if ($u == 0x03) { $this->gif_anloop = ( $this->gif_buffer [1] | $this->gif_buffer [2] 8 ); } } } else { for (;;) { gifdecoder::gifgetbyte(1); if (( $u = $this->gif_buffer [0] ) == 0x00) { break; } gifdecoder::gifgetbyte($u); if ($u == 0x04) { if (isset($this->gif_buffer [4]) & 0x80) { $this->gif_dispos [] = ( $this->gif_buffer [0] >> 2 ) - 1; } else { $this->gif_dispos [] = ( $this->gif_buffer [0] >> 2 ) - 0; } $this->gif_delays [] = ( $this->gif_buffer [1] | $this->gif_buffer [2] 8 ); if ($this->gif_buffer [3]) { $this->gif_transparenti = $this->gif_buffer [3]; } } } } } functiongifreaddescriptor() {$gif_screen = array(); gifdecoder::gifgetbyte(9); $gif_screen = $this->gif_buffer; $gif_colorf = $this->gif_buffer [8] & 0x80 ? 1 : 0; if ($gif_colorf) { $gif_code = $this->gif_buffer [8] & 0x07; $gif_sort = $this->gif_buffer [8] & 0x20 ? 1 : 0; } else { $gif_code = $this->gif_colorc; $gif_sort = $this->gif_sorted; } $gif_size = 2 $gif_code; $this->gif_screen [4] &= 0x70; $this->gif_screen [4] |= 0x80; $this->gif_screen [4] |= $gif_code; if ($gif_sort) { $this->gif_screen [4] |= 0x08; } //gif data beginif ($this->gif_transparenti) { $this->gif_string = gif89a; } else { $this->gif_string = gif87a; } gifdecoder::gifputbyte($this->gif_screen); if ($gif_colorf == 1) { gifdecoder::gifgetbyte(3 * $gif_size); if ($this->gif_transparenti) { $this->gif_transparentr = $this->gif_buffer [3 * $this->gif_transparenti + 0]; $this->gif_transparentg = $this->gif_buffer [3 * $this->gif_transparenti + 1]; $this->gif_transparentb = $this->gif_buffer [3 * $this->gif_transparenti + 2]; } gifdecoder::gifputbyte($this->gif_buffer); } else { if ($this->gif_transparenti) { $this->gif_transparentr = $this->gif_global [3 * $this->gif_transparenti + 0]; $this->gif_transparentg = $this->gif_global [3 * $this->gif_transparenti + 1]; $this->gif_transparentb = $this->gif_global [3 * $this->gif_transparenti + 2]; } gifdecoder::gifputbyte($this->gif_global); } if ($this->gif_transparenti) { $this->gif_string .= !\xf9\x04\x1\x0\x0 . chr($this->gif_transparenti) . \x0; } $this->gif_string .= chr(0x2c); $gif_screen [8] &= 0x40; gifdecoder::gifputbyte($gif_screen); gifdecoder::gifgetbyte(1); gifdecoder::gifputbyte($this->gif_buffer); for (;;) { gifdecoder::gifgetbyte(1); gifdecoder::gifputbyte($this->gif_buffer); if (( $u = $this->gif_buffer [0] ) == 0x00) { break; } gifdecoder::gifgetbyte($u); gifdecoder::gifputbyte($this->gif_buffer); } $this->gif_string .= chr(0x3b); //gif data end$gif_array = &$this->gif_arrays; $gif_array[] = $this->gif_string; } functiongifgetbyte($len) {$this->gif_buffer = array(); for ($i = 0; $i $len; $i++) { if ($this->gif_bfseek > strlen($this->gif_stream)) { return0; } $this->gif_buffer[] = ord($this->gif_stream { $this->gif_bfseek++}); } return1; } functiongifputbyte($bytes) {foreach ($bytesas$byte) { $this->gif_string .= chr($byte); } } functiongifgetframes() {return ( $this->gif_arrays ); } functiongifgetdelays() {return ( $this->gif_delays ); } functiongifgetloop() {return ( $this->gif_anloop ); } functiongifgetdisposal() {return ( $this->gif_dispos ); } functiongifgettransparentr() {return ( $this->gif_transparentr ); } functiongifgettransparentg() {return ( $this->gif_transparentg ); } functiongifgettransparentb() {return ( $this->gif_transparentb ); }}?>
后记
小的知识积累最后会有质变善于总结永远没有坏处一个功能写三遍就会变得不一样,如果还一样的话那就是自己的问题了 以上就介绍了gifdecoder的排错以及修改另附完整代码和demo,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息