时间:2021-07-01 10:21:17 帮助过:25人阅读
好久没有写技术类的博客了,今天有些小的收获,记录下来,留作备份
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) {
酱紫就搞定啦,啦啦啦~
源码
/**
* GIFDecoder
*
* 日期: 2015年05月21日
* 原作者: 不详
* 修改: CalvinLee
*
*
*
* 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);
* }
*
*
*
* @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教程有兴趣的朋友有所帮助。