/** * Process the object to be written */ public function close() { // initialize $this->_data = ''; switch (get_class($this->_object)) { case 'Shared_Escher': if ($dggContainer = $this->_object->getDggContainer()) { $writer = new Writer_Excel5_Escher($dggContainer); $this->_data = $writer->close(); } else { if ($dgContainer = $this->_object->getDgContainer()) { $writer = new Writer_Excel5_Escher($dgContainer); $this->_data = $writer->close(); $this->_spOffsets = $writer->getSpOffsets(); } } break; case 'Shared_Escher_DggContainer': // this is a container record // initialize $innerData = ''; // write the dgg $recVer = 0x0; $recInstance = 0x0; $recType = 0xf006; $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; // dgg data $dggData = pack('VVVV', $this->_object->getSpIdMax(), $this->_object->getCDgSaved() + 1, $this->_object->getCSpSaved(), $this->_object->getCDgSaved()); // add file identifier clusters (one per drawing) for ($i = 0; $i < $this->_object->getCDgSaved(); ++$i) { $dggData .= pack('VV', 0, 0); } $header = pack('vvV', $recVerInstance, $recType, strlen($dggData)); $innerData .= $header . $dggData; // write the bstoreContainer if ($bstoreContainer = $this->_object->getBstoreContainer()) { $writer = new Writer_Excel5_Escher($bstoreContainer); $innerData .= $writer->close(); } // write the record $recVer = 0xf; $recInstance = 0x0; $recType = 0xf000; $length = strlen($innerData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header . $innerData; break; case 'Shared_Escher_DggContainer_BstoreContainer': // this is a container record // initialize $innerData = ''; // treat the inner data if ($BSECollection = $this->_object->getBSECollection()) { foreach ($BSECollection as $BSE) { $writer = new Writer_Excel5_Escher($BSE); $innerData .= $writer->close(); } } // write the record $recVer = 0xf; $recInstance = count($this->_object->getBSECollection()); $recType = 0xf001; $length = strlen($innerData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header . $innerData; break; case 'Shared_Escher_DggContainer_BstoreContainer_BSE': // this is a semi-container record // initialize $innerData = ''; // here we treat the inner data if ($blip = $this->_object->getBlip()) { $writer = new Writer_Excel5_Escher($blip); $innerData .= $writer->close(); } // initialize $data = ''; $btWin32 = $this->_object->getBlipType(); $btMacOS = $this->_object->getBlipType(); $data .= pack('CC', $btWin32, $btMacOS); $rgbUid = pack('VVVV', 0, 0, 0, 0); // todo $data .= $rgbUid; $tag = 0; $size = strlen($innerData); $cRef = 1; $foDelay = 0; //todo $unused1 = 0x0; $cbName = 0x0; $unused2 = 0x0; $unused3 = 0x0; $data .= pack('vVVVCCCC', $tag, $size, $cRef, $foDelay, $unused1, $cbName, $unused2, $unused3); $data .= $innerData; // write the record $recVer = 0x2; $recInstance = $this->_object->getBlipType(); $recType = 0xf007; $length = strlen($data); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header; $this->_data .= $data; break; case 'Shared_Escher_DggContainer_BstoreContainer_BSE_Blip': // this is an atom record // write the record switch ($this->_object->getParent()->getBlipType()) { case Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG: // initialize $innerData = ''; $rgbUid1 = pack('VVVV', 0, 0, 0, 0); // todo $innerData .= $rgbUid1; $tag = 0xff; // todo $innerData .= pack('C', $tag); $innerData .= $this->_object->getData(); $recVer = 0x0; $recInstance = 0x46a; $recType = 0xf01d; $length = strlen($innerData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header; $this->_data .= $innerData; break; case Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG: // initialize $innerData = ''; $rgbUid1 = pack('VVVV', 0, 0, 0, 0); // todo $innerData .= $rgbUid1; $tag = 0xff; // todo $innerData .= pack('C', $tag); $innerData .= $this->_object->getData(); $recVer = 0x0; $recInstance = 0x6e0; $recType = 0xf01e; $length = strlen($innerData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header; $this->_data .= $innerData; break; } break; case 'Shared_Escher_DgContainer': // this is a container record // initialize $innerData = ''; // write the dg $recVer = 0x0; $recInstance = $this->_object->getDgId(); $recType = 0xf008; $length = 8; $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); // number of shapes in this drawing (including group shape) $countShapes = count($this->_object->getSpgrContainer()->getChildren()); $innerData .= $header . pack('VV', $countShapes, $this->_object->getLastSpId()); //$innerData .= $header . pack('VV', 0, 0); // write the spgrContainer if ($spgrContainer = $this->_object->getSpgrContainer()) { $writer = new Writer_Excel5_Escher($spgrContainer); $innerData .= $writer->close(); // get the shape offsets relative to the spgrContainer record $spOffsets = $writer->getSpOffsets(); // save the shape offsets relative to dgContainer foreach ($spOffsets as &$spOffset) { $spOffset += 24; // add length of dgContainer header data (8 bytes) plus dg data (16 bytes) } $this->_spOffsets = $spOffsets; } // write the record $recVer = 0xf; $recInstance = 0x0; $recType = 0xf002; $length = strlen($innerData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header . $innerData; break; case 'Shared_Escher_DgContainer_SpgrContainer': // this is a container record // initialize $innerData = ''; // initialize spape offsets $totalSize = 8; $spOffsets = array(); // treat the inner data foreach ($this->_object->getChildren() as $spContainer) { $writer = new Writer_Excel5_Escher($spContainer); $spData = $writer->close(); $innerData .= $spData; // save the shape offsets (where new shape records begin) $totalSize += strlen($spData); $spOffsets[] = $totalSize; } // write the record $recVer = 0xf; $recInstance = 0x0; $recType = 0xf003; $length = strlen($innerData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header . $innerData; $this->_spOffsets = $spOffsets; break; case 'Shared_Escher_DgContainer_SpgrContainer_SpContainer': // initialize $data = ''; // build the data // write group shape record, if necessary? if ($this->_object->getSpgr()) { $recVer = 0x1; $recInstance = 0x0; $recType = 0xf009; $length = 0x10; $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $data .= $header . pack('VVVV', 0, 0, 0, 0); } // write the shape record $recVer = 0x2; $recInstance = $this->_object->getSpType(); // shape type $recType = 0xf00a; $length = 0x8; $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $data .= $header . pack('VV', $this->_object->getSpId(), $this->_object->getSpgr() ? 0x5 : 0xa00); // the options if ($this->_object->getOPTCollection()) { $optData = ''; $recVer = 0x3; $recInstance = count($this->_object->getOPTCollection()); $recType = 0xf00b; foreach ($this->_object->getOPTCollection() as $property => $value) { $optData .= pack('vV', $property, $value); } $length = strlen($optData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $data .= $header . $optData; } // the client anchor if ($this->_object->getStartCoordinates()) { $clientAnchorData = ''; $recVer = 0x0; $recInstance = 0x0; $recType = 0xf010; // start coordinates list($column, $row) = Cell::coordinateFromString($this->_object->getStartCoordinates()); $c1 = Cell::columnIndexFromString($column) - 1; $r1 = $row - 1; // start offsetX $startOffsetX = $this->_object->getStartOffsetX(); // start offsetY $startOffsetY = $this->_object->getStartOffsetY(); // end coordinates list($column, $row) = Cell::coordinateFromString($this->_object->getEndCoordinates()); $c2 = Cell::columnIndexFromString($column) - 1; $r2 = $row - 1; // end offsetX $endOffsetX = $this->_object->getEndOffsetX(); // end offsetY $endOffsetY = $this->_object->getEndOffsetY(); $clientAnchorData = pack('vvvvvvvvv', 0x2, $c1, $startOffsetX, $r1, $startOffsetY, $c2, $endOffsetX, $r2, $endOffsetY); $length = strlen($clientAnchorData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $data .= $header . $clientAnchorData; } // the client data, just empty for now if (!$this->_object->getSpgr()) { $clientDataData = ''; $recVer = 0x0; $recInstance = 0x0; $recType = 0xf011; $length = strlen($clientDataData); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $data .= $header . $clientDataData; } // write the record $recVer = 0xf; $recInstance = 0x0; $recType = 0xf004; $length = strlen($data); $recVerInstance = $recVer; $recVerInstance |= $recInstance << 4; $header = pack('vvV', $recVerInstance, $recType, $length); $this->_data = $header . $data; break; } return $this->_data; }
/** * Write MSODRAWING record */ private function _writeMsoDrawing() { // check if there are any shapes for this sheet if (count($this->_phpSheet->getDrawingCollection()) == 0) { return; } // create intermediate Escher object $escher = new Shared_Escher(); // dgContainer $dgContainer = new Shared_Escher_DgContainer(); // set the drawing index (we use sheet index + 1) $dgContainer->setDgId($this->_phpSheet->getParent()->getIndex($this->_phpSheet) + 1); $escher->setDgContainer($dgContainer); // spgrContainer $spgrContainer = new Shared_Escher_DgContainer_SpgrContainer(); $dgContainer->setSpgrContainer($spgrContainer); // add one shape which is the group shape $spContainer = new Shared_Escher_DgContainer_SpgrContainer_SpContainer(); $spContainer->setSpgr(true); $spContainer->setSpType(0); $spContainer->setSpId($this->_phpSheet->getParent()->getIndex($this->_phpSheet) + 1 << 10); $spgrContainer->addChild($spContainer); // add the shapes // outer loop is for determining BSE index $blipIndex = 0; // 1-based index to BstoreContainer $countShapes = 0; // count number of shapes (minus group shape), in this sheet foreach ($this->_phpSheet->getParent()->getAllsheets() as $sheet) { foreach ($sheet->getDrawingCollection() as $drawing) { ++$blipIndex; if ($sheet === $this->_phpSheet) { ++$countShapes; // add the shape $spContainer = new Shared_Escher_DgContainer_SpgrContainer_SpContainer(); // set the shape type $spContainer->setSpType(0x4b); // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index) $spId = $countShapes | $this->_phpSheet->getParent()->getIndex($this->_phpSheet) + 1 << 10; $spContainer->setSpId($spId); // keep track of last spId $lastSpId = $spId; // set the BLIP index $spContainer->setOPT(0x4104, $blipIndex); // set coordinates and offsets, client anchor $coordinates = $drawing->getCoordinates(); $offsetX = $drawing->getOffsetX(); $offsetY = $drawing->getOffsetY(); $width = $drawing->getWidth(); $height = $drawing->getHeight(); $twoAnchor = Shared_Excel5::oneAnchor2twoAnchor($this->_phpSheet, $coordinates, $offsetX, $offsetY, $width, $height); $spContainer->setStartCoordinates($twoAnchor['startCoordinates']); $spContainer->setStartOffsetX($twoAnchor['startOffsetX']); $spContainer->setStartOffsetY($twoAnchor['startOffsetY']); $spContainer->setEndCoordinates($twoAnchor['endCoordinates']); $spContainer->setEndOffsetX($twoAnchor['endOffsetX']); $spContainer->setEndOffsetY($twoAnchor['endOffsetY']); $spgrContainer->addChild($spContainer); } } } // set last shape index $dgContainer->setLastSpId($lastSpId); // write the Escher stream $writer = new Writer_Excel5_Escher($escher); $data = $writer->close(); $spOffsets = $writer->getSpOffsets(); // write the neccesary MSODRAWING, OBJ records // split the Escher stream $spOffsets[0] = 0; $nm = count($spOffsets) - 1; // number of shapes excluding first shape for ($i = 1; $i <= $nm; ++$i) { // MSODRAWING record $record = 0xec; // Record identifier // chunk of Escher stream for one shape $dataChunk = substr($data, $spOffsets[$i - 1], $spOffsets[$i] - $spOffsets[$i - 1]); $length = strlen($dataChunk); $header = pack("vv", $record, $length); $this->_append($header . $dataChunk); // OBJ record $record = 0x5d; // record identifier $objData = ''; // ftCmo $objData .= pack('vvvvvVVV', 0x15, 0x12, 0x8, $i, 0x6011, 0, 0, 0); // ftEnd $objData .= pack('vv', 0x0, 0x0); $length = strlen($objData); $header = pack('vv', $record, $length); $this->_append($header . $objData); } }
/** * Writes the MSODRAWINGGROUP record if needed. Possibly split using CONTINUE records. */ private function _writeMsoDrawingGroup() { // any drawings in this workbook? $found = false; foreach ($this->_phpExcel->getAllSheets() as $sheet) { if (count($sheet->getDrawingCollection()) > 0) { $found = true; } } // if there are drawings, then we need to write MSODRAWINGGROUP record if ($found) { // create intermediate Escher object $escher = new Shared_Escher(); // dggContainer $dggContainer = new Shared_Escher_DggContainer(); $escher->setDggContainer($dggContainer); // this loop is for determining maximum shape identifier of all drawing $spIdMax = 0; $totalCountShapes = 0; $countDrawings = 0; foreach ($this->_phpExcel->getAllsheets() as $sheet) { $sheetCountShapes = 0; // count number of shapes (minus group shape), in sheet if (count($sheet->getDrawingCollection()) > 0) { ++$countDrawings; foreach ($sheet->getDrawingCollection() as $drawing) { ++$sheetCountShapes; ++$totalCountShapes; $spId = $sheetCountShapes | $this->_phpExcel->getIndex($sheet) + 1 << 10; $spIdMax = max($spId, $spIdMax); } } } $dggContainer->setSpIdMax($spIdMax + 1); $dggContainer->setCDgSaved($countDrawings); $dggContainer->setCSpSaved($totalCountShapes + $countDrawings); // total number of shapes incl. one group shapes per drawing // bstoreContainer $bstoreContainer = new Shared_Escher_DggContainer_BstoreContainer(); $dggContainer->setBstoreContainer($bstoreContainer); // the BSE's (all the images) foreach ($this->_phpExcel->getAllsheets() as $sheet) { foreach ($sheet->getDrawingCollection() as $drawing) { if ($drawing instanceof Worksheet_Drawing) { $filename = $drawing->getPath(); list($imagesx, $imagesy, $imageFormat) = getimagesize($filename); switch ($imageFormat) { case 1: // GIF, not supported by BIFF8, we convert to PNG $blipType = Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG; $imageResource = imagecreatefromgif($filename); ob_start(); imagepng($imageResource); $blipData = ob_get_contents(); ob_end_clean(); break; case 2: // JPEG $blipType = Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG; $blipData = file_get_contents($filename); break; case 3: // PNG $blipType = Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG; $blipData = file_get_contents($filename); break; case 6: // Windows DIB (BMP), we convert to PNG $blipType = Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG; $imageResource = Shared_Drawing::imagecreatefrombmp($filename); ob_start(); imagepng($imageResource); $blipData = ob_get_contents(); ob_end_clean(); break; default: continue 2; } $blip = new Shared_Escher_DggContainer_BstoreContainer_BSE_Blip(); $blip->setData($blipData); $BSE = new Shared_Escher_DggContainer_BstoreContainer_BSE(); $BSE->setBlipType($blipType); $BSE->setBlip($blip); $bstoreContainer->addBSE($BSE); } else { if ($drawing instanceof Worksheet_MemoryDrawing) { switch ($drawing->getRenderingFunction()) { case Worksheet_MemoryDrawing::RENDERING_JPEG: $blipType = Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG; $renderingFunction = 'imagejpeg'; break; case Worksheet_MemoryDrawing::RENDERING_GIF: case Worksheet_MemoryDrawing::RENDERING_PNG: case Worksheet_MemoryDrawing::RENDERING_DEFAULT: $blipType = Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG; $renderingFunction = 'imagepng'; break; } ob_start(); call_user_func($renderingFunction, $drawing->getImageResource()); $blipData = ob_get_contents(); ob_end_clean(); $blip = new Shared_Escher_DggContainer_BstoreContainer_BSE_Blip(); $blip->setData($blipData); $BSE = new Shared_Escher_DggContainer_BstoreContainer_BSE(); $BSE->setBlipType($blipType); $BSE->setBlip($blip); $bstoreContainer->addBSE($BSE); } } } } // write the Escher stream from the intermediate Escher object $writer = new Writer_Excel5_Escher($escher); $data = $writer->close(); $record = 0xeb; $length = strlen($data); $header = pack("vv", $record, $length); return $this->writeData($header . $data); } }