Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 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 @@ -540,6 +541,11 @@
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 @@ -636,6 +642,61 @@
}
}

/**
* 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 || $filePathParts[0] !== 'Media') {

Check failure on line 664 in src/PhpPresentation/Reader/ODPresentation.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

Negated boolean expression is always false.
return;
}

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

$tmpEmbed = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderODPEmbed');
file_put_contents($tmpEmbed, $mediaFile);

$shape = new Media();
$shape
->setFileName(basename($filePath))
->setPath($tmpEmbed, false);

$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
123 changes: 92 additions & 31 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\PresentationProperties;
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
use PhpOffice\PhpPresentation\Shape\Hyperlink;
use PhpOffice\PhpPresentation\Shape\Media;
use PhpOffice\PhpPresentation\Shape\Placeholder;
use PhpOffice\PhpPresentation\Shape\RichText;
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
Expand Down Expand Up @@ -791,7 +793,11 @@
{
// Core
$document->registerNamespace('asvg', 'http://schemas.microsoft.com/office/drawing/2016/SVG/main');
if ($document->getElement('p:blipFill/a:blip/a:extLst/a:ext/asvg:svgBlip', $node)) {
$embedNode = $document->getElements("p:nvPicPr/p:nvPr//*[local-name()='media']", $node);
$embedNode = $embedNode ? $embedNode->item(0) : false;

Check failure on line 797 in src/PhpPresentation/Reader/PowerPoint2007.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

Ternary operator condition is always true.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PHP Static Analysis (7.1) error doesn't look correct.

Ternary operator condition is always true.

$document->getElements() likely invokes and returns the result of DOMXPath::query, thus $embedNode may have a value of false:

If the expression is malformed or the contextNode is invalid, DOMXPath::query() returns false.

https://www.php.net/manual/en/domxpath.query.php

if ($embedNode) {
$oShape = new Media();
} elseif ($document->getElement('p:blipFill/a:blip/a:extLst/a:ext/asvg:svgBlip', $node)) {
$oShape = new Base64();
} else {
$oShape = new Gd();
Expand All @@ -814,36 +820,10 @@
}
}

$oElement = $document->getElement('p:blipFill/a:blip', $node);
if ($oElement instanceof DOMElement) {
if ($oElement->hasAttribute('r:embed') && isset($this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'])) {
$pathImage = 'ppt/slides/' . $this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'];
$pathImage = explode('/', $pathImage);
foreach ($pathImage as $key => $partPath) {
if ('..' == $partPath) {
unset($pathImage[$key - 1], $pathImage[$key]);
}
}
$pathImage = implode('/', $pathImage);
$imageFile = $this->oZip->getFromName($pathImage);
if (!empty($imageFile)) {
if ($oShape instanceof Gd) {
$info = getimagesizefromstring($imageFile);
if (!$info) {
return;
}
$oShape->setMimeType($info['mime']);
$oShape->setRenderingFunction(str_replace('/', '', $info['mime']));
$image = @imagecreatefromstring($imageFile);
if (!$image) {
return;
}
$oShape->setImageResource($image);
} elseif ($oShape instanceof Base64) {
$oShape->setData('data:image/svg+xml;base64,' . base64_encode($imageFile));
}
}
}
if ($oShape instanceof Media) {
$oShape = $this->loadShapeDrawingEmbed($embedNode, $fileRels, $oShape);
} else {
$oShape = $this->loadShapeDrawingImage($document, $node, $fileRels, $oShape);
}

$oElement = $document->getElement('p:spPr', $node);
Expand Down Expand Up @@ -888,6 +868,87 @@
$oSlide->addShape($oShape);
}

protected function loadShapeDrawingEmbed(DOMElement $oElement, string $fileRels, Media $oShape): Media
{
if (!$oElement->hasAttribute('r:embed')) {
return $oShape;
}
if (!isset($this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'])) {
return $oShape;
}

$embedPath = $this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'];

$pathEmbed = "ppt/slides/{$embedPath}";

$pathEmbed = explode('/', $pathEmbed);
foreach ($pathEmbed as $key => $partPath) {
if ('..' == $partPath) {
unset($pathEmbed[$key - 1], $pathEmbed[$key]);
}
}
$pathEmbed = implode('/', $pathEmbed);
$contentEmbed = $this->oZip->getFromName($pathEmbed);

$tmpEmbed = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderPPT2007Embed');

file_put_contents($tmpEmbed, $contentEmbed);

$fileName = basename($embedPath);

$oShape
->setName($fileName)
->setFileName($fileName)
->setPath($tmpEmbed, false);

return $oShape;
}

protected function loadShapeDrawingImage(XMLReader $document, DOMElement $node, string $fileRels, AbstractDrawingAdapter $oShape): AbstractDrawingAdapter
{
$oElement = $document->getElement('p:blipFill/a:blip', $node);
if (!($oElement instanceof DOMElement)) {
return $oShape;
}
if (!$oElement->hasAttribute('r:embed')) {
return $oShape;
}
if (!isset($this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'])) {
return $oShape;
}

$pathImage = 'ppt/slides/' . $this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'];
$pathImage = explode('/', $pathImage);
foreach ($pathImage as $key => $partPath) {
if ('..' == $partPath) {
unset($pathImage[$key - 1], $pathImage[$key]);
}
}
$pathImage = implode('/', $pathImage);
$imageFile = $this->oZip->getFromName($pathImage);
if (!$imageFile) {
return $oShape;
}

if ($oShape instanceof Gd) {
$info = getimagesizefromstring($imageFile);
if (!$info) {
return $oShape;
}
$oShape->setMimeType($info['mime']);
$oShape->setRenderingFunction(str_replace('/', '', $info['mime']));
$image = @imagecreatefromstring($imageFile);
if (!$image) {
return $oShape;
}
$oShape->setImageResource($image);
} elseif ($oShape instanceof Base64) {
$oShape->setData('data:image/svg+xml;base64,' . base64_encode($imageFile));
}

return $oShape;
}

/**
* Load Shadow for shape or paragraph.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/PhpPresentation/Shape/Drawing/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class File extends AbstractDrawingAdapter
*/
protected $path = '';

/**
* @var string Name of the file
*/
protected $fileName = '';

/**
* Get Path.
*/
Expand Down Expand Up @@ -62,6 +67,18 @@ public function setPath(string $pValue = '', bool $pVerifyFile = true): self
return $this;
}

public function getFileName(): string
{
return $this->fileName;
}

public function setFileName(string $fileName): self
{
$this->fileName = $fileName;

return $this;
}

public function getContents(): string
{
return CommonFile::fileGetContents($this->getPath());
Expand Down
Loading