Skip to content

Commit

Permalink
PHPOffice#246 : PowerPoint : Reading Slide Size
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Sep 7, 2016
1 parent 5d23e65 commit 6e9f78e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- ODPresentation & PowerPoint2007 Writer : Support for video - @Progi1984 GH-123
- ODPresentation & PowerPoint2007 Writer : Support for Visibility for slides - @Progi1984
- PowerPoint2007 Reader : Layout Management - @vincentKool @Progi1984 GH-161
- PowerPoint2007 Reader : Slide size - @loverslcn @Progi1984 GH-246
- PowerPoint2007 Reader : Bullet Color - @Progi1984 GH-257
- PowerPoint2007 Reader : Line Spacing - @Progi1984 GH-257
- PowerPoint2007 Writer : Presentation with predefined View Type - @Progi1984 GH-120
Expand Down
1 change: 1 addition & 0 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ protected function displayPhpPresentationInfo(PhpPresentation $oPHPPpt)
$this->append('<div class="infoBlk" id="divPhpPresentationInfo">');
$this->append('<dl>');
$this->append('<dt>Number of slides</dt><dd>'.$oPHPPpt->getSlideCount().'</dd>');
$this->append('<dt>Document Layout Name</dt><dd>'.(empty($oPHPPpt->getLayout()->getDocumentLayout()) ? 'Custom' : $oPHPPpt->getLayout()->getDocumentLayout()).'</dd>');
$this->append('<dt>Document Layout Height</dt><dd>'.$oPHPPpt->getLayout()->getCY(DocumentLayout::UNIT_MILLIMETER).' mm</dd>');
$this->append('<dt>Document Layout Width</dt><dd>'.$oPHPPpt->getLayout()->getCX(DocumentLayout::UNIT_MILLIMETER).' mm</dd>');
$this->append('<dt>Properties : Category</dt><dd>'.$oPHPPpt->getDocumentProperties()->getCategory().'</dd>');
Expand Down
13 changes: 7 additions & 6 deletions src/PhpPresentation/DocumentLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function setDocumentLayout($pValue = self::LAYOUT_SCREEN_4X3, $isLandscap
/**
* Get Document Layout cx
*
* @param float $unit
* @param string $unit
* @return integer
*/
public function getCX($unit = self::UNIT_EMU)
Expand All @@ -161,7 +161,7 @@ public function getCX($unit = self::UNIT_EMU)
/**
* Get Document Layout cy
*
* @param float $unit
* @param string $unit
* @return integer
*/
public function getCY($unit = self::UNIT_EMU)
Expand All @@ -173,8 +173,8 @@ public function getCY($unit = self::UNIT_EMU)
* Get Document Layout cx
*
* @param float $value
* @param float $unit
* @return integer
* @param string $unit
* @return DocumentLayout
*/
public function setCX($value, $unit = self::UNIT_EMU)
{
Expand All @@ -187,8 +187,8 @@ public function setCX($value, $unit = self::UNIT_EMU)
* Get Document Layout cy
*
* @param float $value
* @param float $unit
* @return integer
* @param string $unit
* @return DocumentLayout
*/
public function setCY($value, $unit = self::UNIT_EMU)
{
Expand All @@ -202,6 +202,7 @@ public function setCY($value, $unit = self::UNIT_EMU)
* @param float $value
* @param string $fromUnit
* @param string $toUnit
* @return float
*/
protected function convertUnit($value, $fromUnit, $toUnit)
{
Expand Down
26 changes: 26 additions & 0 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace PhpOffice\PhpPresentation\Reader;

use PhpOffice\PhpPresentation\DocumentLayout;
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
use PhpOffice\PhpPresentation\Slide\AbstractSlide;
use PhpOffice\PhpPresentation\Shape\Placeholder;
Expand Down Expand Up @@ -148,12 +149,37 @@ protected function loadFile($pFilename)

$pptPresentation = $this->oZip->getFromName('ppt/presentation.xml');
if ($pptPresentation !== false) {
$this->loadDocumentLayout($pptPresentation);
$this->loadSlides($pptPresentation);
}

return $this->oPhpPresentation;
}

/**
* Read Document Layout
* @param $sPart
*/
protected function loadDocumentLayout($sPart)
{
$xmlReader = new XMLReader();
if ($xmlReader->getDomFromString($sPart)) {
foreach ($xmlReader->getElements('/p:presentation/p:sldSz') as $oElement) {
$type = $oElement->getAttribute('type');
$oLayout = $this->oPhpPresentation->getLayout();
if ($type == DocumentLayout::LAYOUT_CUSTOM) {
$oLayout->setCX($oElement->getAttribute('cx'));
$oLayout->setCY($oElement->getAttribute('cy'));
} else {
$oLayout->setDocumentLayout($type, true);
if ($oElement->getAttribute('cx') < $oElement->getAttribute('cy')) {
$oLayout->setDocumentLayout($type, false);
}
}
}
}
}

/**
* Read Document Properties
* @param string $sPart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function render()
$objWriter->writeAttribute('cy', '9144000');
$objWriter->endElement();

$objWriter->writeRaw(' <p:defaultTextStyle>
$objWriter->writeRaw('<p:defaultTextStyle>
<a:defPPr>
<a:defRPr lang="fr-FR"/>
</a:defPPr>
Expand Down
30 changes: 18 additions & 12 deletions tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace PhpOffice\PhpPresentation\Tests\Reader;

use PhpOffice\PhpPresentation\DocumentLayout;
use PhpOffice\PhpPresentation\Reader\PowerPoint2007;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Bullet;
Expand Down Expand Up @@ -83,14 +84,19 @@ public function testLoadFile01()
$oPhpPresentation = $object->load($file);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation);
// Document Properties
$this->assertEquals('PHPOffice', $oPhpPresentation->getProperties()->getCreator());
$this->assertEquals('PHPPresentation Team', $oPhpPresentation->getProperties()->getLastModifiedBy());
$this->assertEquals('Sample 02 Title', $oPhpPresentation->getProperties()->getTitle());
$this->assertEquals('Sample 02 Subject', $oPhpPresentation->getProperties()->getSubject());
$this->assertEquals('Sample 02 Description', $oPhpPresentation->getProperties()->getDescription());
$this->assertEquals('office 2007 openxml libreoffice odt php', $oPhpPresentation->getProperties()->getKeywords());
$this->assertEquals('Sample Category', $oPhpPresentation->getProperties()->getCategory());
//
$this->assertEquals('PHPOffice', $oPhpPresentation->getDocumentProperties()->getCreator());
$this->assertEquals('PHPPresentation Team', $oPhpPresentation->getDocumentProperties()->getLastModifiedBy());
$this->assertEquals('Sample 02 Title', $oPhpPresentation->getDocumentProperties()->getTitle());
$this->assertEquals('Sample 02 Subject', $oPhpPresentation->getDocumentProperties()->getSubject());
$this->assertEquals('Sample 02 Description', $oPhpPresentation->getDocumentProperties()->getDescription());
$this->assertEquals('office 2007 openxml libreoffice odt php', $oPhpPresentation->getDocumentProperties()->getKeywords());
$this->assertEquals('Sample Category', $oPhpPresentation->getDocumentProperties()->getCategory());
// Document Layout
$this->assertEquals(DocumentLayout::LAYOUT_SCREEN_4X3, $oPhpPresentation->getLayout()->getDocumentLayout());
$this->assertEquals(254, $oPhpPresentation->getLayout()->getCX(DocumentLayout::UNIT_MILLIMETER));
$this->assertEquals(190.5, $oPhpPresentation->getLayout()->getCY(DocumentLayout::UNIT_MILLIMETER));

// Slides
$this->assertCount(4, $oPhpPresentation->getAllSlides());

// Slide 1
Expand Down Expand Up @@ -469,14 +475,14 @@ public function testMarkAsFinal()
$object = new PowerPoint2007();
$oPhpPresentation = $object->load($file);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation);
$this->assertFalse($oPhpPresentation->isMarkedAsFinal());
$this->assertFalse($oPhpPresentation->getPresentationProperties()->isMarkedAsFinal());


$file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/PPTX_MarkAsFinal.pptx';
$object = new PowerPoint2007();
$oPhpPresentation = $object->load($file);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation);
$this->assertTrue($oPhpPresentation->isMarkedAsFinal());
$this->assertTrue($oPhpPresentation->getPresentationProperties()->isMarkedAsFinal());
}

public function testZoom()
Expand All @@ -485,14 +491,14 @@ public function testZoom()
$object = new PowerPoint2007();
$oPhpPresentation = $object->load($file);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation);
$this->assertEquals(1, $oPhpPresentation->getZoom());
$this->assertEquals(1, $oPhpPresentation->getPresentationProperties()->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());
$this->assertEquals(2.68, $oPhpPresentation->getPresentationProperties()->getZoom());
}

public function testSlideLayout()
Expand Down

0 comments on commit 6e9f78e

Please sign in to comment.