private function encodeFrame($frame, $delay)
{
$data = '';
$localsStr = 13 + 3 * (2 << (ord($frame[10]) & 0x7));
$localsEnd = strlen($frame) - $localsStr - 1;
$localsTmp = substr($frame, $localsStr, $localsEnd);
$globalLen = 2 << (ord($this->initialFrame[10]) & 0x7);
$localsLen = 2 << (ord($frame[10]) & 0x7);
$globalRgb = substr($this->initialFrame, 13, 3 * (2 << (ord($this->initialFrame[10]) & 0x7)));
$localsRgb = substr($frame, 13, 3 * (2 << (ord($frame[10]) & 0x7)));
$localsExt = "!ù" . chr(($this->disposal << 2) + 0) . chr($delay >> 0 & 0xff) . chr($delay >> 8 & 0xff) . "";
if ($this->transparentColor !== null && ord($frame[10]) & 0x80) {
for ($j = 0; $j < 2 << (ord($frame[10]) & 0x7); $j++) {
if (ord($localsRgb[3 * $j + 0]) == ($this->transparentColor >> 16 & 0xff) && ord($localsRgb[3 * $j + 1]) == ($this->transparentColor >> 8 & 0xff) && ord($localsRgb[3 * $j + 2]) == ($this->transparentColor >> 0 & 0xff)) {
$localsExt = "!ù" . chr(($this->disposal << 2) + 1) . chr($delay >> 0 & 0xff) . chr($delay >> 8 & 0xff) . chr($j) . "";
break;
}
}
}
switch ($localsTmp[0]) {
case "!":
$localsImg = substr($localsTmp, 8, 10);
$localsTmp = substr($localsTmp, 18, strlen($localsTmp) - 18);
break;
case ",":
$localsImg = substr($localsTmp, 0, 10);
$localsTmp = substr($localsTmp, 10, strlen($localsTmp) - 10);
break;
}
if (ord($frame[10]) & 0x80 && null !== $this->initialFrame) {
if ($globalLen == $localsLen) {
if ($this->gifBlockCompare($globalRgb, $localsRgb, $globalLen)) {
$data .= $localsExt . $localsImg . $localsTmp;
} else {
$byte = ord($localsImg[9]);
$byte |= 0x80;
$byte &= 0xf8;
$byte |= ord($this->initialFrame[10]) & 0x7;
$localsImg[9] = chr($byte);
$data .= $localsExt . $localsImg . $localsRgb . $localsTmp;
}
} else {
$byte = ord($localsImg[9]);
$byte |= 0x80;
$byte &= 0xf8;
$byte |= ord($frame[10]) & 0x7;
$localsImg[9] = chr($byte);
$data .= $localsExt . $localsImg . $localsRgb . $localsTmp;
}
} else {
$data .= $localsExt . $localsImg . $localsTmp;
}
return $data;
}