PhpOffice\PhpPresentation\Writer\Serialized::save PHP Method

save() public method

Save PhpPresentation to file
public save ( string $pFilename )
$pFilename string
    public function save($pFilename)
    {
        if (empty($pFilename)) {
            throw new \Exception("Filename is empty.");
        }
        $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 AbstractDrawing) {
                    $imgTemp = $oPresentation->getSlide($i)->getShapeCollection()->offsetGet($j);
                    $objZip->addFromString('media/' . $imgTemp->getFilename(), 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();
    }

Usage Example

Example #1
0
 public function testSaveOverwriting()
 {
     $oPhpPresentation = new PhpPresentation();
     $oSlide = $oPhpPresentation->getActiveSlide();
     $oImage = $oSlide->createDrawingShape();
     $oImage->setPath(PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'PhpPresentationLogo.png');
     $object = new Serialized($oPhpPresentation);
     $file = tempnam(sys_get_temp_dir(), 'PhpPresentation_Serialized');
     file_put_contents($file, rand(1, 100));
     $this->assertFileExists($file, $object->save($file));
 }