Ejemplo n.º 1
1
 /**
  * Build the Escher object corresponding to the MSODRAWINGGROUP record
  */
 private function _buildWorkbookEscher()
 {
     $escher = null;
     // any drawings in this workbook?
     $found = false;
     foreach ($this->_phpExcel->getAllSheets() as $sheet) {
         if (count($sheet->getDrawingCollection()) > 0) {
             $found = true;
             break;
         }
     }
     // nothing to do if there are no drawings
     if (!$found) {
         return;
     }
     // if we reach here, then there are drawings in the workbook
     $escher = new PHPExcel_Shared_Escher();
     // dggContainer
     $dggContainer = new PHPExcel_Shared_Escher_DggContainer();
     $escher->setDggContainer($dggContainer);
     // set IDCLs (identifier clusters)
     $dggContainer->setIDCLs($this->_IDCLs);
     // 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 PHPExcel_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 PHPExcel_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 = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
                         ob_start();
                         imagepng(imagecreatefromgif($filename));
                         $blipData = ob_get_contents();
                         ob_end_clean();
                         break;
                     case 2:
                         // JPEG
                         $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
                         $blipData = file_get_contents($filename);
                         break;
                     case 3:
                         // PNG
                         $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
                         $blipData = file_get_contents($filename);
                         break;
                     case 6:
                         // Windows DIB (BMP), we convert to PNG
                         $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
                         ob_start();
                         imagepng(PHPExcel_Shared_Drawing::imagecreatefrombmp($filename));
                         $blipData = ob_get_contents();
                         ob_end_clean();
                         break;
                     default:
                         continue 2;
                 }
                 $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
                 $blip->setData($blipData);
                 $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
                 $BSE->setBlipType($blipType);
                 $BSE->setBlip($blip);
                 $bstoreContainer->addBSE($BSE);
             } else {
                 if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
                     switch ($drawing->getRenderingFunction()) {
                         case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG:
                             $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
                             $renderingFunction = 'imagejpeg';
                             break;
                         case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF:
                         case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG:
                         case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT:
                             $blipType = PHPExcel_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 PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
                     $blip->setData($blipData);
                     $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
                     $BSE->setBlipType($blipType);
                     $BSE->setBlip($blip);
                     $bstoreContainer->addBSE($BSE);
                 }
             }
         }
     }
     // Set the Escher object
     $this->_writerWorkbook->setEscher($escher);
 }
Ejemplo n.º 2
0
 /**
  * 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 PHPExcel_Shared_Escher();
     // dgContainer
     $dgContainer = new PHPExcel_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 PHPExcel_Shared_Escher_DgContainer_SpgrContainer();
     $dgContainer->setSpgrContainer($spgrContainer);
     // add one shape which is the group shape
     $spContainer = new PHPExcel_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 PHPExcel_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 = PHPExcel_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 PHPExcel_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);
     }
 }
Ejemplo n.º 3
0
 /**
  * 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 PHPExcel_Shared_Escher();
         // dggContainer
         $dggContainer = new PHPExcel_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 PHPExcel_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 PHPExcel_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 = PHPExcel_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 = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
                             $blipData = file_get_contents($filename);
                             break;
                         case 3:
                             // PNG
                             $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
                             $blipData = file_get_contents($filename);
                             break;
                         case 6:
                             // Windows DIB (BMP), we convert to PNG
                             $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
                             $imageResource = PHPExcel_Shared_Drawing::imagecreatefrombmp($filename);
                             ob_start();
                             imagepng($imageResource);
                             $blipData = ob_get_contents();
                             ob_end_clean();
                             break;
                         default:
                             continue 2;
                     }
                     $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
                     $blip->setData($blipData);
                     $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
                     $BSE->setBlipType($blipType);
                     $BSE->setBlip($blip);
                     $bstoreContainer->addBSE($BSE);
                 } else {
                     if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
                         switch ($drawing->getRenderingFunction()) {
                             case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG:
                                 $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
                                 $renderingFunction = 'imagejpeg';
                                 break;
                             case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF:
                             case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG:
                             case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT:
                                 $blipType = PHPExcel_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 PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
                         $blip->setData($blipData);
                         $BSE = new PHPExcel_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 PHPExcel_Writer_Excel5_Escher($escher);
         $data = $writer->close();
         $record = 0xeb;
         $length = strlen($data);
         $header = pack("vv", $record, $length);
         return $this->writeData($header . $data);
     }
 }