Skip to content

Powerpoint 2007 & ODPresentation Readers: Loading embedded media + media loading from disk #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
72 changes: 67 additions & 5 deletions src/PhpPresentation/Reader/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PhpOffice\PhpPresentation\PresentationProperties;
use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
use PhpOffice\PhpPresentation\Shape\Media;
use PhpOffice\PhpPresentation\Shape\RichText;
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
use PhpOffice\PhpPresentation\Slide\Background\Color as BackgroundColor;
Expand Down Expand Up @@ -269,12 +270,14 @@ protected function loadStyle(DOMElement $nodeStyle): bool
if ('bitmap' == $nodeDrawingPageProps->getAttribute('draw:fill') && $nodeDrawingPageProps->hasAttribute('draw:fill-image-name')) {
$nameStyle = $nodeDrawingPageProps->getAttribute('draw:fill-image-name');
if (!empty($this->arrayCommonStyles[$nameStyle]) && 'image' == $this->arrayCommonStyles[$nameStyle]['type'] && !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);
if ($contentImg) {
$tmpFile = new Gd();
$tmpFile->loadFromContent($contentImg, basename($this->arrayCommonStyles[$nameStyle]['path']));

$oBackground = new Image();
$oBackground->setPath($tmpBkgImg);
$oBackground = new Image();
$oBackground->setImage($tmpFile);
}
}
}
}
Expand Down Expand Up @@ -540,6 +543,11 @@ protected function loadSlide(DOMElement $nodeSlide): bool
if ($this->oXMLReader->getElement('draw:text-box', $oNodeFrame)) {
$this->loadShapeRichText($oNodeFrame);

continue;
}
if ($this->oXMLReader->getElement('draw:plugin', $oNodeFrame)) {
$this->loadShapeMedia($oNodeFrame);

continue;
}
}
Expand Down Expand Up @@ -578,7 +586,7 @@ protected function loadShapeDrawing(DOMElement $oNodeFrame): void
// Contents of file
if (empty($mimetype)) {
$shape = new Gd();
$shape->setImageResource(imagecreatefromstring($imageFile));
$shape->loadFromContent($imageFile, basename($sFilename));
} else {
$shape = new Base64();
$shape->setData('data:' . $mimetype . ';base64,' . base64_encode($imageFile));
Expand Down Expand Up @@ -636,6 +644,60 @@ protected function loadShapeRichText(DOMElement $oNodeFrame): void
}
}

/**
* Read Shape Media.
*/
protected function loadShapeMedia(DOMElement $oNodeFrame): void
{
$oNodePlugin = $this->oXMLReader->getElement('draw:plugin', $oNodeFrame);
if (!($oNodePlugin instanceof DOMElement)) {
return;
}

$mediaFile = null;
$filePath = null;
if ($oNodePlugin->hasAttribute('xlink:href')) {
$filePath = $oNodePlugin->getAttribute('xlink:href');
if (!$filePath) {
return;
}

$filePathParts = explode('/', $filePath);
if ($filePathParts[0] !== 'Media') {
return;
}

$mediaFile = $this->oZip->getFromName($filePath);
}

if (!$mediaFile) {
return;
}

$shape = new Media();
$shape->loadFromContent($mediaFile, basename($filePath));

$shape->getShadow()->setVisible(false);
$shape->setName($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
$shape->setDescription($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
$shape->setResizeProportional(false);
$shape->setWidth($oNodeFrame->hasAttribute('svg:width') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:width'), 0, -2)) : 0);
$shape->setHeight($oNodeFrame->hasAttribute('svg:height') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:height'), 0, -2)) : 0);
$shape->setResizeProportional(true);
$shape->setOffsetX($oNodeFrame->hasAttribute('svg:x') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:x'), 0, -2)) : 0);
$shape->setOffsetY($oNodeFrame->hasAttribute('svg:y') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:y'), 0, -2)) : 0);

if ($oNodeFrame->hasAttribute('draw:style-name')) {
$keyStyle = $oNodeFrame->getAttribute('draw:style-name');
if (isset($this->arrayStyles[$keyStyle])) {
$shape->setShadow($this->arrayStyles[$keyStyle]['shadow']);
$shape->setFill($this->arrayStyles[$keyStyle]['fill']);
}
}

$this->oPhpPresentation->getActiveSlide()->addShape($shape);
}

/**
* Read Paragraph.
*/
Expand Down
Loading