Exemple #1
0
 public function emit(array $chunkInfo, $callback)
 {
     if (!isset($chunkInfo['crcBytes'])) {
         $chunkInfo['crc'] = TOGoS_PNGChunks_Util::calculateChunkCrcBytes($chunkInfo);
     }
     $chunkInfo['lengthBytes'] = pack('N', strlen($chunkInfo['data']));
     call_user_func($callback, $chunkInfo['lengthBytes'] . $chunkInfo['typeBytes']);
     call_user_func($callback, $chunkInfo['data']);
     call_user_func($callback, $chunkInfo['crcBytes']);
 }
Exemple #2
0
 protected function _update()
 {
     switch ($this->state) {
         case self::STATE_BEGIN:
             if (strlen($this->buffer) >= 8) {
                 if ($this->validating) {
                     $header = substr($this->buffer, 0, 8);
                     if ($header !== TOGoS_PNGChunks_Util::PNG_HEADER) {
                         throw new TOGoS_PNGChunks_MalformedDataException("Bad PNG header.");
                     }
                 }
                 $this->buffer = substr($this->buffer, 8);
                 $this->state = self::STATE_HEADER_READ;
                 return true;
             }
             return false;
         case self::STATE_HEADER_READ:
             if (strlen($this->buffer) >= 12) {
                 // 12 being the minimum chunk length
                 $unpacked = unpack('Nlen', $this->buffer);
                 $length = $unpacked['len'];
                 $chunkLen = $length + 12;
                 if (strlen($this->buffer) >= $chunkLen) {
                     $typeBytes = substr($this->buffer, 4, 4);
                     $data = substr($this->buffer, 8, $length);
                     $crcBytes = substr($this->buffer, $chunkLen - 4, 4);
                     $chunkInfo = array('data' => $data, 'typeBytes' => $typeBytes, 'crcBytes' => $crcBytes);
                     if ($this->validating) {
                         TOGoS_PNGChunks_Util::verifyChunkCrc($chunkInfo);
                     }
                     call_user_func($this->chunkCallback, $chunkInfo);
                     $this->buffer = substr($this->buffer, $chunkLen);
                     return true;
                 }
             }
             return false;
         default:
             throw new Exception("Invalid state: " . $this->state);
     }
 }