/** * Set Worksheet * * @param \PHPExcel\Worksheet $pValue * @param boolean $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet? * @throws \PHPExcel\Exception * @return BaseDrawing */ public function setWorksheet(\PHPExcel\Worksheet $pValue = null, $pOverrideOld = false) { if (is_null($this->worksheet)) { // Add drawing to \PHPExcel\Worksheet $this->worksheet = $pValue; $this->worksheet->getCell($this->coordinates); $this->worksheet->getDrawingCollection()->append($this); } else { if ($pOverrideOld) { // Remove drawing from old \PHPExcel\Worksheet $iterator = $this->worksheet->getDrawingCollection()->getIterator(); while ($iterator->valid()) { if ($iterator->current()->getHashCode() == $this->getHashCode()) { $this->worksheet->getDrawingCollection()->offsetUnset($iterator->key()); $this->worksheet = null; break; } } // Set new \PHPExcel\Worksheet $this->setWorksheet($pValue); } else { throw new \PHPExcel\Exception("A \\PHPExcel\\Worksheet has already been assigned. Drawings can only exist on one \\PHPExcel\\Worksheet."); } } return $this; }
/** * Write drawings to XML format * * @param \PHPExcel\Worksheet $pWorksheet * @param int &$chartRef Chart ID * @param boolean $includeCharts Flag indicating if we should include drawing details for charts * @return string XML Output * @throws \PHPExcel\Writer\Exception */ public function writeDrawings(\PHPExcel\Worksheet $pWorksheet, &$chartRef, $includeCharts = false) { // Create XML writer $objWriter = null; if ($this->getParentWriter()->getUseDiskCaching()) { $objWriter = new \PHPExcel\Shared\XMLWriter(\PHPExcel\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); } else { $objWriter = new \PHPExcel\Shared\XMLWriter(\PHPExcel\Shared\XMLWriter::STORAGE_MEMORY); } // XML header $objWriter->startDocument('1.0', 'UTF-8', 'yes'); // xdr:wsDr $objWriter->startElement('xdr:wsDr'); $objWriter->writeAttribute('xmlns:xdr', 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing'); $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main'); // Loop through images and write drawings $i = 1; $iterator = $pWorksheet->getDrawingCollection()->getIterator(); while ($iterator->valid()) { $this->writeDrawing($objWriter, $iterator->current(), $i); $iterator->next(); ++$i; } if ($includeCharts) { $chartCount = $pWorksheet->getChartCount(); // Loop through charts and write the chart position if ($chartCount > 0) { for ($c = 0; $c < $chartCount; ++$c) { $this->writeChart($objWriter, $pWorksheet->getChartByIndex($c), $c + $i); } } } $objWriter->endElement(); // Return return $objWriter->getData(); }
/** * Write drawing relationships to XML format * * @param \PHPExcel\Worksheet $pWorksheet * @param int &$chartRef Chart ID * @param boolean $includeCharts Flag indicating if we should write charts * @return string XML Output * @throws \PHPExcel\Writer\Exception */ public function writeDrawingRelationships(\PHPExcel\Worksheet $pWorksheet, &$chartRef, $includeCharts = false) { // Create XML writer $objWriter = null; if ($this->getParentWriter()->getUseDiskCaching()) { $objWriter = new \PHPExcel\Shared\XMLWriter(\PHPExcel\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); } else { $objWriter = new \PHPExcel\Shared\XMLWriter(\PHPExcel\Shared\XMLWriter::STORAGE_MEMORY); } // XML header $objWriter->startDocument('1.0', 'UTF-8', 'yes'); // Relationships $objWriter->startElement('Relationships'); $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); // Loop through images and write relationships $i = 1; $iterator = $pWorksheet->getDrawingCollection()->getIterator(); while ($iterator->valid()) { if ($iterator->current() instanceof \PHPExcel\Worksheet\Drawing || $iterator->current() instanceof \PHPExcel\Worksheet\MemoryDrawing) { // Write relationship for image drawing $this->writeRelationship($objWriter, $i, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())); } $iterator->next(); ++$i; } if ($includeCharts) { // Loop through charts and write relationships $chartCount = $pWorksheet->getChartCount(); if ($chartCount > 0) { for ($c = 0; $c < $chartCount; ++$c) { $this->writeRelationship($objWriter, $i++, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart', '../charts/chart' . ++$chartRef . '.xml'); } } } $objWriter->endElement(); return $objWriter->getData(); }
/** * Generate image tag in cell * * @param \PHPExcel\Worksheet $pSheet \PHPExcel\Worksheet * @param string $coordinates Cell coordinates * @return string * @throws \PHPExcel\Writer\Exception */ private function writeImageInCell(\PHPExcel\Worksheet $pSheet, $coordinates) { // Construct HTML $html = ''; // Write images foreach ($pSheet->getDrawingCollection() as $drawing) { if ($drawing instanceof \PHPExcel\Worksheet\Drawing) { if ($drawing->getCoordinates() == $coordinates) { $filename = $drawing->getPath(); // Strip off eventual '.' if (substr($filename, 0, 1) == '.') { $filename = substr($filename, 1); } // Prepend images root $filename = $this->getImagesRoot() . $filename; // Strip off eventual '.' if (substr($filename, 0, 1) == '.' && substr($filename, 0, 2) != './') { $filename = substr($filename, 1); } // Convert UTF8 data to PCDATA $filename = htmlspecialchars($filename); $html .= PHP_EOL; if (!$this->embedImages || $this->isPdf) { $imageData = $filename; } else { $imageDetails = getimagesize($filename); if ($fp = fopen($filename, "rb", 0)) { $picture = fread($fp, filesize($filename)); fclose($fp); // base64 encode the binary data, then break it // into chunks according to RFC 2045 semantics $base64 = chunk_split(base64_encode($picture)); $imageData = 'data:' . $imageDetails['mime'] . ';base64,' . $base64; } else { $imageData = $filename; } } $html .= '<div style="position: relative;">'; $html .= '<img style="position: absolute; z-index: 1; left: ' . $drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px; width: ' . $drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' . $imageData . '" border="0" />'; $html .= '</div>'; } } elseif ($drawing instanceof \PHPExcel\Worksheet\MemoryDrawing) { if ($drawing->getCoordinates() != $coordinates) { continue; } ob_start(); // Let's start output buffering. imagepng($drawing->getImageResource()); // This will normally output the image, but because of ob_start(), it won't. $contents = ob_get_contents(); // Instead, output above is saved to $contents ob_end_clean(); // End the output buffer. $dataUri = "data:image/jpeg;base64," . base64_encode($contents); // Because of the nature of tables, width is more important than height. // max-width: 100% ensures that image doesnt overflow containing cell // width: X sets width of supplied image. // As a result, images bigger than cell will be contained and images smaller will not get stretched $html .= '<img src="' . $dataUri . '" style="max-width:100%;width:' . $drawing->getWidth() . 'px;" />'; } } return $html; }
/** * Write Drawings * * @param \PHPExcel\Shared\XMLWriter $objWriter XML Writer * @param \PHPExcel\Worksheet $pSheet Worksheet * @param boolean $includeCharts Flag indicating if we should include drawing details for charts * @throws \PHPExcel\Writer\Exception */ private function writeDrawings(\PHPExcel\Shared\XMLWriter $objWriter = null, \PHPExcel\Worksheet $pSheet = null, $includeCharts = false) { $chartCount = $includeCharts ? $pSheet->getChartCollection()->count() : 0; // If sheet contains drawings, add the relationships if ($pSheet->getDrawingCollection()->count() > 0 || $chartCount > 0) { $objWriter->startElement('drawing'); $objWriter->writeAttribute('r:id', 'rId1'); $objWriter->endElement(); } }