Esempio n. 1
0
 /**
  * Save PHPPowerPoint to file
  *
  * @param 	string 		$pFileName
  * @throws 	Exception
  */
 public function save($pFilename = null)
 {
     if (!is_null($this->_presentation)) {
         // If $pFilename is php://output or php://stdout, make it a temporary file...
         $originalFilename = $pFilename;
         if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
             $pFilename = @tempnam('./', 'phppttmp');
             if ($pFilename == '') {
                 $pFilename = $originalFilename;
             }
         }
         // Create drawing dictionary
         $this->_drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->_presentation));
         // Create new ZIP file and open it for writing
         $objZip = new ZipArchive();
         // Try opening the ZIP file
         if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
             if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
                 throw new Exception("Could not open " . $pFilename . " for writing.");
             }
         }
         // Add [Content_Types].xml to ZIP file
         $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_presentation));
         // Add relationships to ZIP file
         $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_presentation));
         $objZip->addFromString('ppt/_rels/presentation.xml.rels', $this->getWriterPart('Rels')->writePresentationRelationships($this->_presentation));
         // Add document properties to ZIP file
         $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_presentation));
         $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_presentation));
         // Add theme to ZIP file
         $objZip->addFromString('ppt/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_presentation));
         // Add slide master to ZIP file
         $masterSlide = $this->getLayoutPack()->getMasterSlide();
         $objZip->addFromString('ppt/slideMasters/_rels/slideMaster1.xml.rels', $this->getWriterPart('Rels')->writeSlideMasterRelationships());
         $objZip->addFromString('ppt/slideMasters/slideMaster1.xml', $masterSlide['body']);
         // Add slide layouts to ZIP file
         $slideLayouts = $this->getLayoutPack()->getLayouts();
         for ($i = 0; $i < count($slideLayouts); ++$i) {
             $objZip->addFromString('ppt/slideLayouts/_rels/slideLayout' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeSlideLayoutRelationships());
             $objZip->addFromString('ppt/slideLayouts/slideLayout' . ($i + 1) . '.xml', $slideLayouts[$i]['body']);
         }
         // Add presentation to ZIP file
         $objZip->addFromString('ppt/presentation.xml', $this->getWriterPart('Presentation')->writePresentation($this->_presentation));
         // Add slides (drawings, ...)
         for ($i = 0; $i < $this->_presentation->getSlideCount(); ++$i) {
             // Add slide
             $objZip->addFromString('ppt/slides/slide' . ($i + 1) . '.xml', $this->getWriterPart('Slide')->writeSlide($this->_presentation->getSlide($i)));
         }
         // Add slide relationships (drawings, ...)
         for ($i = 0; $i < $this->_presentation->getSlideCount(); ++$i) {
             // Add relationships
             $objZip->addFromString('ppt/slides/_rels/slide' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeSlideRelationships($this->_presentation->getSlide($i), $i + 1));
         }
         // Add media
         for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
             if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPPowerPoint_Shape_Drawing) {
                 $imageContents = null;
                 $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
                 if (strpos($imagePath, 'zip://') !== false) {
                     $imagePath = substr($imagePath, 6);
                     $imagePathSplitted = explode('#', $imagePath);
                     $imageZip = new ZipArchive();
                     $imageZip->open($imagePathSplitted[0]);
                     $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
                     $imageZip->close();
                     unset($imageZip);
                 } else {
                     $imageContents = file_get_contents($imagePath);
                 }
                 $objZip->addFromString('ppt/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
             } else {
                 if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPPowerPoint_Shape_MemoryDrawing) {
                     ob_start();
                     call_user_func($this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), $this->getDrawingHashTable()->getByIndex($i)->getImageResource());
                     $imageContents = ob_get_contents();
                     ob_end_clean();
                     $objZip->addFromString('ppt/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
                 }
             }
         }
         // Close file
         if ($objZip->close() === false) {
             throw new Exception("Could not close zip file {$pFilename}.");
         }
         // If a temporary file was used, copy it to the correct file stream
         if ($originalFilename != $pFilename) {
             if (copy($pFilename, $originalFilename) === false) {
                 throw new Exception("Could not copy temporary zip file {$pFilename} to {$originalFilename}.");
             }
             @unlink($pFilename);
         }
     } else {
         throw new Exception("PHPPowerPoint object unassigned.");
     }
 }
Esempio n. 2
0
 /**
  * Save PHPPowerPoint to file
  *
  * @param 	string 		$pFileName
  * @throws 	Exception
  */
 public function save($pFilename = null)
 {
     if (!is_null($this->_presentation)) {
         // If $pFilename is php://output or php://stdout, make it a temporary file...
         $originalFilename = $pFilename;
         if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
             $pFilename = @tempnam('./', 'phppttmp');
             if ($pFilename == '') {
                 $pFilename = $originalFilename;
             }
         }
         // Create drawing dictionary
         $this->_drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->_presentation));
         // Create new ZIP file and open it for writing
         $objZip = new ZipArchive();
         // Try opening the ZIP file
         if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
             if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
                 throw new Exception("Could not open " . $pFilename . " for writing.");
             }
         }
         // Add mimetype to ZIP file
         //@todo Not in ZIPARCHIVE::CM_STORE mode
         $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->_presentation));
         // Add content.xml to ZIP file
         $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->_presentation));
         // Add meta.xml to ZIP file
         $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->_presentation));
         // Add styles.xml to ZIP file
         $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->_presentation));
         // Add META-INF/manifest.xml
         $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_presentation));
         // Add media
         $arrMedia = array();
         for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
             if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPPowerPoint_Shape_Drawing) {
                 if (!in_array(md5($this->getDrawingHashTable()->getByIndex($i)->getPath()), $arrMedia)) {
                     $arrMedia[] = md5($this->getDrawingHashTable()->getByIndex($i)->getPath());
                     $imageContents = null;
                     $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
                     if (strpos($imagePath, 'zip://') !== false) {
                         $imagePath = substr($imagePath, 6);
                         $imagePathSplitted = explode('#', $imagePath);
                         $imageZip = new ZipArchive();
                         $imageZip->open($imagePathSplitted[0]);
                         $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
                         $imageZip->close();
                         unset($imageZip);
                     } else {
                         $imageContents = file_get_contents($imagePath);
                     }
                     $objZip->addFromString('Pictures/' . md5($this->getDrawingHashTable()->getByIndex($i)->getPath()) . '.' . $this->getDrawingHashTable()->getByIndex($i)->getExtension(), $imageContents);
                 }
             } else {
                 if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPPowerPoint_Shape_MemoryDrawing) {
                     if (!in_array(md5($this->getDrawingHashTable()->getByIndex($i)->getPath()), $arrMedia)) {
                         $arrMedia[] = md5($this->getDrawingHashTable()->getByIndex($i)->getPath());
                         ob_start();
                         call_user_func($this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), $this->getDrawingHashTable()->getByIndex($i)->getImageResource());
                         $imageContents = ob_get_contents();
                         ob_end_clean();
                         $objZip->addFromString('Pictures/' . md5($this->getDrawingHashTable()->getByIndex($i)->getPath()) . '.' . $this->getDrawingHashTable()->getByIndex($i)->getExtension(), $imageContents);
                     }
                 }
             }
         }
         // Close file
         if ($objZip->close() === false) {
             throw new Exception("Could not close zip file {$pFilename}.");
         }
         // If a temporary file was used, copy it to the correct file stream
         if ($originalFilename != $pFilename) {
             if (copy($pFilename, $originalFilename) === false) {
                 throw new Exception("Could not copy temporary zip file {$pFilename} to {$originalFilename}.");
             }
             @unlink($pFilename);
         }
     } else {
         throw new Exception("PHPPowerPoint object unassigned.");
     }
 }