代码如下所示:
<?<BR>class GifMerge { <BR> var $ver = '1.1'; <BR> var $dly = 50; <BR> var $mod = 'C_FILE'; <BR> var $first = true; <BR> var $use_loop = false; <BR> var $transparent = false; <BR> var $use_global_in = false; <BR> var $x = 0; <BR> var $y = 0; <BR> var $ch = 0; <BR> var $fin = 0; <BR> var $fout = ''; <BR> var $loop = 0; <BR> var $delay = 0; <BR> var $width = 0; <BR> var $height = 0; <BR> var $trans1 = 255; <BR> var $trans2 = 255; <BR> var $trans3 = 255; <BR> var $disposal = 2; <BR> var $out_color_table_size = 0; <BR> var $local_color_table_flag = 0; <BR> var $global_color_table_size = 0; <BR> var $out_color_table_sizecode = 0; <BR> var $global_color_table_sizecode= 0; <BR> var $gif = array(0x47, 0x49, 0x46); <BR> var $buffer = array(); <BR> var $local_in = array(); <BR> var $global_in = array(); <BR> var $global_out = array(); <BR> var $logical_screen_descriptor = array(); <BR> function GifMerge($images, $t1, $t2, $t3, $loop, $dl, $xpos, $ypos, $model) { <BR> if($model) { <BR> $this->mod = $model; <BR> } <BR> if($loop > -1) { <BR> $this->loop = floor($loop - 1); <BR> $this->use_loop = true; <BR> } <BR> if($t1 > -1 && $t2 > -1 && $t3 > -1) { <BR> $this->trans1 = $t1; <BR> $this->trans2 = $t2; <BR> $this->trans3 = $t3; <BR> $this->transparent = true; <BR> } <BR> for($i = 0; $i < count($images); $i++) { <BR> $dl[$i] ? $this->delay = $dl[$i] : $this->delay = $this->dly; <BR> $xpos[$i] ? $this->x = $xpos[$i] : $this->x = 0; <BR> $ypos[$i] ? $this->y = $ypos[$i] : $this->y = 0; <BR> $this->start_gifmerge_process($images[$i]); <BR> } <BR> $this->fout .= "/x3b"; <BR> } <BR> function start_gifmerge_process($fp) { <BR> if($this->mod == 'C_FILE') { <BR> if(!$this->fin = fopen($fp, 'rb')) { <BR> return; <BR> } <BR> } elseif($this->mod == 'C_MEMORY') { <BR> $this->ch = 0; <BR> $this->fin = $fp; <BR> } <BR> $this->getbytes(6); <BR> if(!$this->arrcmp($this->buffer, $this->gif, 3)) { <BR> return; <BR> } <BR> $this->getbytes(7); <BR> if($this->first) $this->logical_screen_descriptor = $this->buffer; <BR> $this->global_color_table_sizecode = $this->buffer[4] & 0x07; <BR> $this->global_color_table_size = 2 <global_color_table_sizecode; <BR> if($this->buffer[4] & 0x80) { <BR> $this->getbytes((3 * $this->global_color_table_size)); <BR> for($i = 0; $i global_color_table_size)); $i++) { <BR> $this->global_in[$i] = $this->buffer[$i]; <BR> } <BR> if($this->out_color_table_size == 0) { <BR> $this->out_color_table_size = $this->global_color_table_size; <BR> $out_color_table_sizecode = $this->global_color_table_sizecode; <BR> $this->global_out = $this->global_in; <BR> } <BR> if($this->global_color_table_size != $this->out_color_table_size || $this->arrcmp($this->global_out, $this->global_in, (3 * $this->global_color_table_size))) { <BR> $this->use_global_in = true; <BR> } <BR> } <BR> for($loop = true; $loop;) { <BR> $this->getbytes(1); <BR> switch($this->buffer[0]) { <BR> case 0x21: <BR> $this->read_extension(); <BR> break; <BR> case 0x2c: <BR> $this->read_image_descriptor(); <BR> break; <BR> case 0x3b: <BR> $loop = false; <BR> break; <BR> default: <BR> $loop = false; <BR> } <BR> } <BR> if($this->mod == 'C_FILE') { <BR> fclose($this->fin); <BR> } <BR> } <BR> function read_image_descriptor() { <BR> $this->getbytes(9); <BR> $head = $this->buffer; <BR> $this->local_color_table_flag = ($this->buffer[8] & 0x80) ? true : false; <BR> if($this->local_color_table_flag) { <BR> $sizecode = $this->buffer[8] & 0x07; <BR> $size = 2 << $sizecode; <BR> $this->getbytes(3 * $size); <BR> for($i = 0; $i < (3 * $size); $i++) { <BR> $this->local_in[$i] = $this->buffer[$i]; <BR> } <BR> if($this->out_color_table_size == 0) { <BR> $this->out_color_table_size = $size; <BR> $out_color_table_sizecode = $sizecode; <BR> <div style="color:transparent">!本文来源gaodai.ma#com搞##代!^码网(</div><sup>搞gaodaima代码</sup> for($i = 0; $i < (3 * $size); $i++) <BR> { <BR> $this->global_out[$i] = $this->local_in[$i]; <BR> } <BR> } <BR> } <BR> if($this->first) { <BR> $this->first = false; <BR> $this->fout .= "/x47/x49/x46/x38/x39/x61"; <BR> if($this->width && $this->height) { <BR> $this->logical_screen_descriptor[0] = $this->width & 0xFF; <BR> $this->logical_screen_descriptor[1] = ($this->width & 0xFF00) >> 8; <BR> $this->logical_screen_descriptor[2] = $this->height & 0xFF; <BR> $this->logical_screen_descriptor[3] = ($this->height & 0xFF00) >> 8; <BR> } <BR> $this->logical_screen_descriptor[4] |= 0x80; <BR> $this->logical_screen_descriptor[5] &= 0xF0; <BR> $this->logical_screen_descriptor[6] |= $this->out_color_table_sizecode; <BR> $this->putbytes($this->logical_screen_descriptor, 7); <BR> $this->putbytes($this->global_out, ($this->out_color_table_size * 3)); <BR> if($this->use_loop) { <BR> $ns[0] = 0x21; <BR> $ns[1] = 0xFF; <BR> $ns[2] = 0x0B; <BR> $ns[3] = 0x4e; <BR> $ns[4] = 0x45; <BR> $ns[5] = 0x54; <BR> $ns[6] = 0x53; <BR> $ns[7] = 0x43; <BR> $ns[8] = 0x41; <BR> $ns[9] = 0x50; <BR> $ns[10] = 0x45; <BR> $ns[11] = 0x32; <BR> $ns[12] = 0x2e; <BR> $ns[13] = 0x30; <BR> $ns[14] = 0x03; <BR> $ns[15] = 0x01; <BR> $ns[16] = $this->loop & 255; <BR> $ns[17] = $this->loop >> 8; <BR> $ns[18] = 0x00; <BR> $this->putbytes($ns, 19); <BR> } <BR> } <BR> if($this->use_global_in) { <BR> $outtable = $this->global_in; <BR> $outsize = $this->global_color_table_size; <BR> $outsizecode = $this->global_color_table_sizecode; <BR> } else { <BR> $outtable = $this->global_out; <BR> $outsize = $this->out_color_table_size; <BR> } <BR> if($this->local_color_table_flag) { <BR> if($size == $this->out_color_table_size && !$this->arrcmp($this->local_in, $this->global_out, $size)) { <BR> $outtable = $global_out; <BR> $outsize = $this->out_color_table_size; <BR> } else { <BR> $outtable = $this->local_in; <BR> $outsize = $size; <BR> $outsizecode = $sizecode; <BR> } <BR> } <BR> $use_trans = false; <BR> if($this->transparent) { <BR> for($i = 0; $i < $outsize; $i++) { <BR> if($outtable[3 * $i] == $this->trans1 && $outtable [3 * $i + 1] == $this->trans2 && $outtable [3 * $i + 2] == $this->trans3) { <BR> break; <BR> } <BR> } <BR> if($i < $outsize) { <BR> $transindex = $i; <BR> $use_trans = true; <BR> } <BR> } <BR> if($this->delay || $use_trans) { <BR> $this->buffer[0] = 0x21; <BR> $this->buffer[1] = 0xf9; <BR> $this->buffer[2] = 0x04; <BR> $this->buffer[3] = ($this->disposal << 2) + ($use_trans ? 1 : 0); <BR> $this->buffer[4] = $this->delay & 0xff; <BR> $this->buffer[5] = ($this->delay & 0xff00) >> 8; <BR> $this->buffer[6] = $use_trans ? $transindex : 0; <BR> $this->buffer[7] = 0x00; <BR> $this->putbytes($this->buffer,8); <BR> } <BR> $this->buffer[0] = 0x2c; <BR> $this->putbytes($this->buffer,1); <BR> $head[0] = $this->x & 0xff; <BR> $head[1] = ($this->x & 0xff00) >> 8; <BR> $head[2] = $this->y & 0xff; <BR> $head[3] = ($this->y & 0xff00) >> 8; <BR> $head[8] &= 0x40; <BR> if($outtable != $this->global_out) { <BR> $head[8] |= 0x80; <BR> $head[8] |= $outsizecode; <BR> } <BR> $this->putbytes($head,9); <BR> if($outtable != $this->global_out) { <BR> $this->putbytes($outtable, (3 * $outsize)); <BR> } <BR> $this->getbytes(1); <BR> $this->putbytes($this->buffer,1); <BR> for(;;) { <BR> $this->getbytes(1); <BR> $this->putbytes($this->buffer,1); <BR> if(($u = $this->buffer[0]) == 0) { <BR> break; <BR> } <BR> $this->getbytes($u); <BR> $this->putbytes($this->buffer, $u); <BR> } <BR> } <BR> function read_extension() { <BR> $this->getbytes(1); <BR> switch($this->buffer[0]) { <BR> case 0xf9: <BR> $this->getbytes(6); <BR> break; <BR> case 0xfe: <BR> for(;;) { <BR> $this->getbytes(1); <BR> if(($u = $this->buffer[0]) == 0) { <BR> break; <BR> } <BR> $this->getbytes($u); <BR> } <BR> break; <BR> case 0x01: <BR> $this->getbytes(13); <BR> for(;;) { <BR> $this->getbytes(0); <BR> if(($u = $this->buffer[0]) == 0) { <BR> break; <BR> } <BR> $this->getbytes($u); <BR> } <BR> break; <BR> case 0xff: <BR> $this->getbytes(9); <BR> $this->getbytes(3); <BR> for(;;) { <BR> $this->getbytes(1); <BR> if(!$this->buffer[0]) { <BR> break; <BR> } <BR> $this->getbytes($this->buffer[0]); <BR> } <BR> break; <BR> default: <BR> for(;;) { <BR> $this->getbytes(1); <BR> if(!$this->buffer[0]) { <BR> break; <BR> } <BR> $this->getbytes($this->buffer[0]); <BR> } <BR> } <BR> } <BR> function arrcmp($b, $s, $l) { <BR> for($i = 0; $i < $l; $i++) { <BR> if($s{$i} != $b{$i}) { <BR> return false; <BR> } <BR> } <BR> return true; <BR> } <BR> function getbytes($l) { <BR> for($i = 0; $i < $l; $i++) { <BR> if($this->mod == 'C_FILE') { <BR> $bin = unpack('C*', fread($this->fin, 1)); <BR> $this->buffer[$i] = $bin[1]; <BR> } elseif($this->mod == 'C_MEMORY') { <BR> $bin = unpack('C*', substr($this->fin, $this->ch, 1)); <BR> $this->buffer[$i] = $bin[1]; <BR> $this->ch++; <BR> } <BR> } <BR> return $this->buffer; <BR> } <BR> function putbytes($s, $l) { <BR> for($i = 0; $i < $l; $i++) { <BR> $this->fout .= pack('C*', $s[$i]); <BR> } <BR> } <BR> function getAnimation() { <BR> return $this->fout; <BR> } <BR>} <BR>?><BR>