Skip to content

Commit

Permalink
#152 : Implement reader ODPresentation Background
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jan 3, 2016
1 parent e38e24d commit c338df3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
10 changes: 10 additions & 0 deletions samples/Sample_Header.php
Expand Up @@ -282,6 +282,16 @@ protected function displayPhpPresentationInfo(PhpPresentation $oPHPPpt)
$this->append('<dt>Offset Y</dt><dd>'.$oSlide->getOffsetY().'</dd>');
$this->append('<dt>Extent X</dt><dd>'.$oSlide->getExtentX().'</dd>');
$this->append('<dt>Extent Y</dt><dd>'.$oSlide->getExtentY().'</dd>');
$oBkg = $oSlide->getBackground();
if ($oBkg instanceof Slide\AbstractBackground) {
if ($oBkg instanceof Slide\Background\Color) {
$this->append('<dt>Background Color</dt><dd>#'.$oBkg->getColor()->getRGB().'</dd>');
}
if ($oBkg instanceof Slide\Background\Image) {
$sBkgImgContents = file_get_contents($oBkg->getPath());
$this->append('<dt>Background Image</dt><dd><img src="data:image/png;base64,'.base64_encode($sBkgImgContents).'"></dd>');
}
}
$this->append('</dl>');
$this->append('</div>');

Expand Down
71 changes: 67 additions & 4 deletions src/PhpPresentation/Reader/ODPresentation.php
Expand Up @@ -52,6 +52,10 @@ class ODPresentation implements ReaderInterface
* @var array[]
*/
protected $arrayStyles = array();
/**
* @var array[]
*/
protected $arrayCommonStyles = array();
/**
* @var \PhpOffice\Common\XMLReader
*/
Expand Down Expand Up @@ -131,6 +135,10 @@ protected function loadFile($pFilename)
$this->loadDocumentProperties();
}
$this->oXMLReader = new XMLReader();
if ($this->oXMLReader->getDomFromZip($pFilename, 'styles.xml') !== false) {
$this->loadStylesFile();
}
$this->oXMLReader = new XMLReader();
if ($this->oXMLReader->getDomFromZip($pFilename, 'content.xml') !== false) {
$this->loadSlides();
}
Expand Down Expand Up @@ -191,7 +199,30 @@ protected function loadSlides()
protected function loadStyle(\DOMElement $nodeStyle)
{
$keyStyle = $nodeStyle->getAttribute('style:name');


$nodeDrawingPageProps = $this->oXMLReader->getElement('style:drawing-page-properties', $nodeStyle);
if ($nodeDrawingPageProps) {
// Read Background Color
if ($nodeDrawingPageProps->hasAttribute('draw:fill-color') && $nodeDrawingPageProps->getAttribute('draw:fill') == 'solid') {
$oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Color();
$oColor = new Color();
$oColor->setRGB(substr($nodeDrawingPageProps->getAttribute('draw:fill-color'), -6));
$oBackground->setColor($oColor);
}
// Read Background Image
if ($nodeDrawingPageProps->getAttribute('draw:fill') == 'bitmap' && $nodeDrawingPageProps->hasAttribute('draw:fill-image-name')) {
$nameStyle = $nodeDrawingPageProps->getAttribute('draw:fill-image-name');
if(!empty($this->arrayCommonStyles[$nameStyle]) && $this->arrayCommonStyles[$nameStyle]['type'] == 'image' && !empty($this->arrayCommonStyles[$nameStyle]['path'])) {
$tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderODPBkg');
$contentImg = $this->oZip->getFromName($this->arrayCommonStyles[$nameStyle]['path']);
file_put_contents($tmpBkgImg, $contentImg);

$oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Image();
$oBackground->setPath($tmpBkgImg);
}
}
}

$nodeGraphicProps = $this->oXMLReader->getElement('style:graphic-properties', $nodeStyle);
if ($nodeGraphicProps) {
// Read Shadow
Expand Down Expand Up @@ -235,6 +266,7 @@ protected function loadStyle(\DOMElement $nodeStyle)
$oFont->setSize(substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2));
}
}

$nodeParagraphProps = $this->oXMLReader->getElement('style:paragraph-properties', $nodeStyle);
if ($nodeParagraphProps) {
$oAlignment = new Alignment();
Expand Down Expand Up @@ -284,6 +316,7 @@ protected function loadStyle(\DOMElement $nodeStyle)

$this->arrayStyles[$keyStyle] = array(
'alignment' => isset($oAlignment) ? $oAlignment : null,
'background' => isset($oBackground) ? $oBackground : null,
'font' => isset($oFont) ? $oFont : null,
'shadow' => isset($oShadow) ? $oShadow : null,
'listStyle' => isset($arrayListStyle) ? $arrayListStyle : null,
Expand All @@ -293,7 +326,8 @@ protected function loadStyle(\DOMElement $nodeStyle)
}

/**
* Extract data from slide
* Read Slide
*
* @param \DOMElement $nodeSlide
*/
protected function loadSlide(\DOMElement $nodeSlide)
Expand All @@ -304,6 +338,12 @@ protected function loadSlide(\DOMElement $nodeSlide)
if ($nodeSlide->hasAttribute('draw:name')) {
$this->oPhpPresentation->getActiveSlide()->setName($nodeSlide->getAttribute('draw:name'));
}
if ($nodeSlide->hasAttribute('draw:style-name')) {
$keyStyle = $nodeSlide->getAttribute('draw:style-name');
if (isset($this->arrayStyles[$keyStyle])) {
$this->oPhpPresentation->getActiveSlide()->setBackground($this->arrayStyles[$keyStyle]['background']);
}
}
foreach ($this->oXMLReader->getElements('draw:frame', $nodeSlide) as $oNodeFrame) {
if ($this->oXMLReader->getElement('draw:image', $oNodeFrame)) {
$this->loadShapeDrawing($oNodeFrame);
Expand All @@ -318,6 +358,7 @@ protected function loadSlide(\DOMElement $nodeSlide)
}

/**
* Read Shape Drawing
*
* @param \DOMElement $oNodeFrame
*/
Expand Down Expand Up @@ -362,6 +403,7 @@ protected function loadShapeDrawing(\DOMElement $oNodeFrame)
}

/**
* Read Shape RichText
*
* @param \DOMElement $oNodeFrame
*/
Expand Down Expand Up @@ -438,7 +480,13 @@ protected function readParagraphItem(Paragraph $oParagraph, \DOMElement $oNodePa
}
}
}


/**
* Read List
*
* @param RichText $oShape
* @param \DOMElement $oNodeParent
*/
protected function readList(RichText $oShape, \DOMElement $oNodeParent)
{
foreach ($this->oXMLReader->getElements('text:list-item/*', $oNodeParent) as $oNodeListItem) {
Expand All @@ -454,7 +502,7 @@ protected function readList(RichText $oShape, \DOMElement $oNodeParent)
}

/**
* Read Paragraph
* Read List Item
* @param RichText $oShape
* @param \DOMElement $oNodeParent
* @param \DOMElement $oNodeParagraph
Expand All @@ -473,4 +521,19 @@ protected function readListItem(RichText $oShape, \DOMElement $oNodeParent, \DOM
$this->readParagraphItem($oParagraph, $oNodeRichTextElement);
}
}

/**
* Load file 'styles.xml'
*/
protected function loadStylesFile()
{
foreach ($this->oXMLReader->getElements('/office:document-styles/office:styles/*') as $oElement) {
if ($oElement->nodeName == 'draw:fill-image') {
$this->arrayCommonStyles[$oElement->getAttribute('draw:name')] = array(
'type' => 'image',
'path' => $oElement->hasAttribute('xlink:href') ? $oElement->getAttribute('xlink:href') : null
);
}
}
}
}
2 changes: 1 addition & 1 deletion src/PhpPresentation/Slide.php
Expand Up @@ -510,7 +510,7 @@ public function getBackground()
* @param AbstractBackground $background
* @return $this
*/
public function setBackground(AbstractBackground $background)
public function setBackground(AbstractBackground $background = null)
{
$this->background = $background;
return $this;
Expand Down

0 comments on commit c338df3

Please sign in to comment.