diff --git a/CHANGELOG.md b/CHANGELOG.md index 869ea58d1..a37fef1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - ODPresentation Reader/Writer : Name of the slide - @Progi1984 GH-121 - PowerPoint2007 Reader/Writer : Mark as final - @Progi1984 GH-118 +- PowerPoint2007 Reader/Writer : Set default zoom value for presentation - @Progi1984 GH-122 ## 0.5.0 - 2015-10-08 diff --git a/docs/recipes.rst b/docs/recipes.rst index 1bf4d20bb..ec2c604e8 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -3,6 +3,27 @@ Recipes ======= +How to define the zoom of a presentation ? +------------------------------------------ + +You must define the zoom of your presentation with the method ``setZoom()`` + +.. code-block:: php + + // Default + $zoom = $oPHPPresentation->getZoom(); + // $zoom = 1 + + // Without parameter + $oPHPPresentation->setZoom(); + $zoom = $oPHPPresentation->getZoom(); + // $zoom = true + + // Parameter = false + $oPHPPresentation->setZoom(2.8); + $zoom = $oPHPPresentation->getZoom(); + // $zoom = 2.8 + How to mark a presentation as final ? ------------------------------------- diff --git a/samples/Sample_14_Zoom.php b/samples/Sample_14_Zoom.php new file mode 100644 index 000000000..8428b2027 --- /dev/null +++ b/samples/Sample_14_Zoom.php @@ -0,0 +1,51 @@ +setZoom(3); + +// Create slide +echo date('H:i:s') . ' Create slide'.EOL; +$currentSlide = $objPHPPresentation->getActiveSlide(); + +// Create a shape (drawing) +echo date('H:i:s') . ' Create a shape (drawing)'.EOL; +$shape = $currentSlide->createDrawingShape(); +$shape->setName('PHPPresentation logo') + ->setDescription('PHPPresentation logo') + ->setPath('./resources/phppowerpoint_logo.gif') + ->setHeight(36) + ->setOffsetX(10) + ->setOffsetY(10); +$shape->getShadow()->setVisible(true) + ->setDirection(45) + ->setDistance(10); +$shape->getHyperlink()->setUrl('https://github.com/PHPOffice/PHPPresentation/')->setTooltip('PHPPresentation'); + +// Create a shape (text) +echo date('H:i:s') . ' Create a shape (rich text)'.EOL; +$shape = $currentSlide->createRichTextShape() + ->setHeight(300) + ->setWidth(600) + ->setOffsetX(170) + ->setOffsetY(180); +$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER ); +$textRun = $shape->createTextRun('Thank you for using PHPPresentation!'); +$textRun->getFont()->setBold(true) + ->setSize(60) + ->setColor( new Color( 'FFE06B20' ) ); + +// Save file +echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers); +if (!CLI) { + include_once 'Sample_Footer.php'; +} diff --git a/src/PhpPresentation/PhpPresentation.php b/src/PhpPresentation/PhpPresentation.php index 2fdbe4546..a3e354854 100644 --- a/src/PhpPresentation/PhpPresentation.php +++ b/src/PhpPresentation/PhpPresentation.php @@ -59,6 +59,12 @@ class PhpPresentation */ private $markAsFinal = false; + /** + * Zoom + * @var float + */ + private $zoom = 1; + /** * Create a new PhpPresentation with one Slide */ @@ -319,4 +325,26 @@ public function isMarkedAsFinal() { return $this->markAsFinal; } + + /** + * Set the zoom of the document (in percentage) + * @param float $zoom + * @return PhpPresentation + */ + public function setZoom($zoom = 1) + { + if (is_numeric($zoom)) { + $this->zoom = $zoom; + } + return $this; + } + + /** + * Return the zoom (in percentage) + * @return float + */ + public function getZoom() + { + return $this->zoom; + } } diff --git a/src/PhpPresentation/Reader/PowerPoint2007.php b/src/PhpPresentation/Reader/PowerPoint2007.php index e0ba8f04b..c69df3661 100644 --- a/src/PhpPresentation/Reader/PowerPoint2007.php +++ b/src/PhpPresentation/Reader/PowerPoint2007.php @@ -123,6 +123,11 @@ protected function loadFile($pFilename) if ($docPropsCustom !== false) { $this->loadCustomProperties($docPropsCustom); } + + $pptViewProps = $this->oZip->getFromName('ppt/viewProps.xml'); + if ($pptViewProps !== false) { + $this->loadViewProperties($pptViewProps); + } $pptPresentation = $this->oZip->getFromName('ppt/presentation.xml'); if ($pptPresentation !== false) { @@ -166,8 +171,6 @@ protected function loadDocumentProperties($sPart) } } - - /** * Read Custom Properties * @param string $sPart @@ -185,6 +188,23 @@ protected function loadCustomProperties($sPart) } } } + + /** + * Read View Properties + * @param string $sPart + */ + protected function loadViewProperties($sPart) + { + $xmlReader = new XMLReader(); + if ($xmlReader->getDomFromString($sPart)) { + $pathZoom = '/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx'; + if (is_object($oElement = $xmlReader->getElement($pathZoom))) { + if ($oElement->hasAttribute('d') && $oElement->hasAttribute('n')) { + $this->oPhpPresentation->setZoom($oElement->getAttribute('n') / $oElement->getAttribute('d')); + } + } + } + } /** * Extract all slides diff --git a/src/PhpPresentation/Writer/PowerPoint2007.php b/src/PhpPresentation/Writer/PowerPoint2007.php index 88740f315..d50f105b4 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007.php +++ b/src/PhpPresentation/Writer/PowerPoint2007.php @@ -216,7 +216,7 @@ public function save($pFilename) // Add PPT properties and styles to ZIP file - Required for Apple Keynote compatibility. $objZip->addFromString('ppt/presProps.xml', $wPptProps->writePresProps()); $objZip->addFromString('ppt/tableStyles.xml', $wPptProps->writeTableStyles()); - $objZip->addFromString('ppt/viewProps.xml', $wPptProps->writeViewProps()); + $objZip->addFromString('ppt/viewProps.xml', $wPptProps->writeViewProps($this->presentation)); // Add relationships to ZIP file $objZip->addFromString('_rels/.rels', $wPartRels->writeRelationships()); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptProps.php b/src/PhpPresentation/Writer/PowerPoint2007/PptProps.php index 27dd316ff..ed827c8bf 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptProps.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptProps.php @@ -105,7 +105,7 @@ public function writeTableStyles() * @return string XML Output * @throws \Exception */ - public function writeViewProps() + public function writeViewProps(PhpPresentation $oPhpPresentation) { // Create XML writer $objWriter = $this->getXMLWriter(); @@ -120,7 +120,46 @@ public function writeViewProps() $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main'); $objWriter->writeAttribute('showComments', '0'); - // > p:viewPr + // p:viewPr > p:slideViewPr + $objWriter->startElement('p:slideViewPr'); + + // p:viewPr > p:slideViewPr > p:cSldViewPr + $objWriter->startElement('p:cSldViewPr'); + + // p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr + $objWriter->startElement('p:cViewPr'); + + // p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr > p:scale + $objWriter->startElement('p:scale'); + + $objWriter->startElement('a:sx'); + $objWriter->writeAttribute('d', '100'); + $objWriter->writeAttribute('n', (int)($oPhpPresentation->getZoom() * 100)); + $objWriter->endElement(); + + $objWriter->startElement('a:sy'); + $objWriter->writeAttribute('d', '100'); + $objWriter->writeAttribute('n', (int)($oPhpPresentation->getZoom() * 100)); + $objWriter->endElement(); + + // > // p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr > p:scale + $objWriter->endElement(); + + $objWriter->startElement('p:origin'); + $objWriter->writeAttribute('x', '0'); + $objWriter->writeAttribute('y', '0'); + $objWriter->endElement(); + + // > // p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr + $objWriter->endElement(); + + // > // p:viewPr > p:slideViewPr > p:cSldViewPr + $objWriter->endElement(); + + // > // p:viewPr > p:slideViewPr + $objWriter->endElement(); + + // > // p:viewPr $objWriter->endElement(); return $objWriter->getData(); diff --git a/tests/PhpPresentation/Tests/PhpPowerpointTest.php b/tests/PhpPresentation/Tests/PhpPowerpointTest.php index 2ee7df65e..13b4805ac 100644 --- a/tests/PhpPresentation/Tests/PhpPowerpointTest.php +++ b/tests/PhpPresentation/Tests/PhpPowerpointTest.php @@ -117,4 +117,16 @@ public function testSetActiveSlideIndexException() $object = new PhpPresentation(); $object->setActiveSlideIndex(1); } + + public function testZoom() + { + $object = new PhpPresentation(); + $this->assertEquals(1, $object->getZoom()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->setZoom('AAAA')); + $this->assertEquals(1, $object->getZoom()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->setZoom(2.3)); + $this->assertEquals(2.3, $object->getZoom()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->setZoom()); + $this->assertEquals(1, $object->getZoom()); + } } diff --git a/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php b/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php index 0a6ed1de3..084d5ed96 100644 --- a/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php +++ b/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php @@ -478,4 +478,20 @@ public function testMarkAsFinal() $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation); $this->assertTrue($oPhpPresentation->isMarkedAsFinal()); } + + public function testZoom() + { + $file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/Sample_12.pptx'; + $object = new PowerPoint2007(); + $oPhpPresentation = $object->load($file); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation); + $this->assertEquals(1, $oPhpPresentation->getZoom()); + + + $file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/PPTX_Zoom.pptx'; + $object = new PowerPoint2007(); + $oPhpPresentation = $object->load($file); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation); + $this->assertEquals(2.68, $oPhpPresentation->getZoom()); + } } diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php index e54583aa4..9036a0ba0 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php @@ -163,4 +163,28 @@ public function testMarkAsFinal() $this->assertFalse($pres->elementExists('/Properties/property[@name="_MarkAsFinal"]', 'docProps/custom.xml')); $this->assertFalse($pres->elementExists('/cp:coreProperties/cp:contentStatus', 'docProps/core.xml')); } + + public function testZoom() + { + $oPhpPresentation = new PhpPresentation(); + + $pres = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007'); + $this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'ppt/viewProps.xml')); + $this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'n', 'ppt/viewProps.xml')); + $this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'd', 'ppt/viewProps.xml')); + $this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'ppt/viewProps.xml')); + $this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'n', 'ppt/viewProps.xml')); + $this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'd', 'ppt/viewProps.xml')); + + $value = rand(1, 100); + $oPhpPresentation->setZoom($value); + + $pres = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007'); + $this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'ppt/viewProps.xml')); + $this->assertEquals($value * 100, $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'n', 'ppt/viewProps.xml')); + $this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'd', 'ppt/viewProps.xml')); + $this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'ppt/viewProps.xml')); + $this->assertEquals($value * 100, $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'n', 'ppt/viewProps.xml')); + $this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'd', 'ppt/viewProps.xml')); + } } diff --git a/tests/resources/files/PPTX_Zoom.pptx b/tests/resources/files/PPTX_Zoom.pptx new file mode 100644 index 000000000..0ef676fc9 Binary files /dev/null and b/tests/resources/files/PPTX_Zoom.pptx differ