setPhpPresentation($pPhpPresentation ?? new PhpPresentation()); // Set ZIP Adapter $this->setZipAdapter(new ZipArchiveAdapter()); } /** * Save PhpPresentation to file. * * @throws DirectoryNotFoundException * @throws InvalidParameterException */ public function save(string $pFilename): void { if (empty($pFilename)) { throw new InvalidParameterException('pFilename', ''); } if (!\is_dir(\dirname($pFilename))) { throw new DirectoryNotFoundException(\dirname($pFilename)); } $oPresentation = $this->getPhpPresentation(); // Create new ZIP file and open it for writing $objZip = $this->getZipAdapter(); // Try opening the ZIP file $objZip->open($pFilename); // Add media $slideCount = $oPresentation->getSlideCount(); for ($i = 0; $i < $slideCount; ++$i) { for ($j = 0; $j < $oPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) { if ($oPresentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawingAdapter) { $imgTemp = $oPresentation->getSlide($i)->getShapeCollection()->offsetGet($j); $objZip->addFromString( 'media/' . $imgTemp->getImageIndex() . '/' . \pathinfo($imgTemp->getPath(), PATHINFO_BASENAME), \file_get_contents($imgTemp->getPath()) ); } } } // Add PhpPresentation.xml to the document, which represents a PHP serialized PhpPresentation object $objZip->addFromString('PhpPresentation.xml', $this->writeSerialized($oPresentation, $pFilename)); // Close file $objZip->close(); } /** * Serialize PhpPresentation object to XML. * * @param PhpPresentation|null $pPhpPresentation * @param string $pFilename * * @return string XML Output */ protected function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '') { // Clone $pPhpPresentation $pPhpPresentation = clone $pPhpPresentation; // Update media links $slideCount = $pPhpPresentation->getSlideCount(); for ($i = 0; $i < $slideCount; ++$i) { for ($j = 0; $j < $pPhpPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) { if ($pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawingAdapter) { $imgTemp = $pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j); $imgPath = 'zip://' . $pFilename . '#media/' . $imgTemp->getImageIndex() . '/' . \pathinfo($imgTemp->getPath(), PATHINFO_BASENAME); if ($imgTemp instanceof File) { $imgTemp->setPath($imgPath, false); } else { $imgTemp->setPath($imgPath); } } } } // Create XML writer $objWriter = new XMLWriter(); $objWriter->openMemory(); $objWriter->setIndent(true); // XML header $objWriter->startDocument('1.0', 'UTF-8', 'yes'); // PhpPresentation $objWriter->startElement('PhpPresentation'); $objWriter->writeAttribute('version', '##VERSION##'); // Comment $objWriter->writeComment('This file has been generated using PhpPresentation v##VERSION## (http://github.com/PHPOffice/PhpPresentation). It contains a base64 encoded serialized version of the PhpPresentation internal object.'); // Data $objWriter->startElement('data'); $objWriter->writeCData(\base64_encode(\serialize($pPhpPresentation))); $objWriter->endElement(); $objWriter->endElement(); // Return return $objWriter->getData(); } }