Skip to content

Commit a8cc93a

Browse files
author
Phil Wilkinson
committed
PowerPoint2007 - load images/resources to disk instead of loading into memory
1 parent 79fada6 commit a8cc93a

File tree

1 file changed

+17
-54
lines changed

1 file changed

+17
-54
lines changed

src/PhpPresentation/Reader/PowerPoint2007.php

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ protected function loadSlide(string $sPart, string $baseFile): void
440440
$oSlide = $this->oPhpPresentation->createSlide();
441441
$this->oPhpPresentation->setActiveSlideIndex($this->oPhpPresentation->getSlideCount() - 1);
442442
$oSlide->setRelsIndex('ppt/slides/_rels/' . $baseFile . '.rels');
443-
444443
// Background
445444
$oElement = $xmlReader->getElement('/p:sld/p:cSld/p:bg/p:bgPr');
446445
if ($oElement instanceof DOMElement) {
@@ -482,23 +481,24 @@ protected function loadSlide(string $sPart, string $baseFile): void
482481
}
483482
$pathImage = implode('/', $pathImage);
484483
$contentImg = $this->oZip->getFromName($pathImage);
484+
$fileName = basename($pathImage);
485+
486+
$tmpFile = new Gd();
487+
$tmpFile->loadFromContent($contentImg, $fileName);
485488

486-
$tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderPpt2007Bkg');
487-
file_put_contents($tmpBkgImg, $contentImg);
488489
// Background
489490
$oBackground = new Slide\Background\Image();
490-
$oBackground->setPath($tmpBkgImg);
491+
$oBackground->setImage($tmpFile);
492+
491493
// Slide Background
492494
$oSlide = $this->oPhpPresentation->getActiveSlide();
493495
$oSlide->setBackground($oBackground);
494496
}
495497
}
496498
}
497-
498499
// Shapes
499500
$arrayElements = $xmlReader->getElements('/p:sld/p:cSld/p:spTree/*');
500501
$this->loadSlideShapes($oSlide, $arrayElements, $xmlReader);
501-
502502
// Layout
503503
$oSlide = $this->oPhpPresentation->getActiveSlide();
504504
foreach ($this->arrayRels['ppt/slides/_rels/' . $baseFile . '.rels'] as $valueRel) {
@@ -507,7 +507,6 @@ protected function loadSlide(string $sPart, string $baseFile): void
507507
if (array_key_exists($layoutBasename, $this->arraySlideLayouts)) {
508508
$oSlide->setSlideLayout($this->arraySlideLayouts[$layoutBasename]);
509509
}
510-
511510
break;
512511
}
513512
}
@@ -555,15 +554,13 @@ protected function loadMasterSlide(string $sPart, string $baseFile): void
555554
continue;
556555
}
557556
$oRTParagraph = new Paragraph();
558-
559557
if ('a:defPPr' == $oElementLvl->nodeName) {
560558
$level = 0;
561559
} else {
562560
$level = str_replace('a:lvl', '', $oElementLvl->nodeName);
563561
$level = str_replace('pPr', '', $level);
564562
$level = (int) $level;
565563
}
566-
567564
if ($oElementLvl->hasAttribute('algn')) {
568565
$oRTParagraph->getAlignment()->setHorizontal($oElementLvl->getAttribute('algn'));
569566
}
@@ -602,7 +599,6 @@ protected function loadMasterSlide(string $sPart, string $baseFile): void
602599
$oRTParagraph->getFont()->setColor($oSchemeColor);
603600
}
604601
}
605-
606602
switch ($oElementTxStyle->nodeName) {
607603
case 'p:bodyStyle':
608604
$oSlideMaster->getTextStyles()->setBodyStyleAtLvl($oRTParagraph, $level);
@@ -627,7 +623,6 @@ protected function loadMasterSlide(string $sPart, string $baseFile): void
627623
if (false !== $pptTheme) {
628624
$this->loadTheme($pptTheme, $oSlideMaster);
629625
}
630-
631626
break;
632627
}
633628
}
@@ -692,7 +687,6 @@ protected function loadLayoutSlide(string $sPart, string $baseFile, SlideMaster
692687

693688
return $oSlideLayout;
694689
}
695-
696690
// @phpstan-ignore-next-line
697691
return null;
698692
}
@@ -764,12 +758,15 @@ protected function loadSlideBackground(XMLReader $xmlReader, DOMElement $oElemen
764758
}
765759
$pathImage = implode('/', $pathImage);
766760
$contentImg = $this->oZip->getFromName($pathImage);
761+
$fileName = basename($pathImage);
762+
763+
$tmpFile = new Gd();
764+
$tmpFile->loadFromContent($contentImg, $fileName);
767765

768-
$tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderPpt2007Bkg');
769-
file_put_contents($tmpBkgImg, $contentImg);
770766
// Background
771767
$oBackground = new Slide\Background\Image();
772-
$oBackground->setPath($tmpBkgImg);
768+
$oBackground->setImage($tmpFile);
769+
773770
// Slide Background
774771
$oSlide->setBackground($oBackground);
775772
}
@@ -783,7 +780,6 @@ protected function loadSlideNote(string $baseFile, Slide $oSlide): void
783780
// @phpstan-ignore-next-line
784781
if ($xmlReader->getDomFromString($sPart)) {
785782
$oNote = $oSlide->getNote();
786-
787783
$arrayElements = $xmlReader->getElements('/p:notes/p:cSld/p:spTree/*');
788784
$this->loadSlideShapes($oNote, $arrayElements, $xmlReader);
789785
}
@@ -805,12 +801,10 @@ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, Abstr
805801
$oShape->getShadow()->setVisible(false);
806802
// Variables
807803
$fileRels = $oSlide->getRelsIndex();
808-
809804
$oElement = $document->getElement('p:nvPicPr/p:cNvPr', $node);
810805
if ($oElement instanceof DOMElement) {
811806
$oShape->setName($oElement->hasAttribute('name') ? $oElement->getAttribute('name') : '');
812807
$oShape->setDescription($oElement->hasAttribute('descr') ? $oElement->getAttribute('descr') : '');
813-
814808
// Hyperlink
815809
$oElementHlinkClick = $document->getElement('a:hlinkClick', $oElement);
816810
if (is_object($oElementHlinkClick)) {
@@ -819,26 +813,22 @@ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, Abstr
819813
);
820814
}
821815
}
822-
823816
if ($oShape instanceof Media) {
824817
$oShape = $this->loadShapeDrawingEmbed($embedNode, $fileRels, $oShape);
825818
} else {
826819
$oShape = $this->loadShapeDrawingImage($document, $node, $fileRels, $oShape);
827820
}
828-
829821
$oElement = $document->getElement('p:spPr', $node);
830822
if ($oElement instanceof DOMElement) {
831823
$oFill = $this->loadStyleFill($document, $oElement);
832824
$oShape->setFill($oFill);
833825
}
834-
835826
$oElement = $document->getElement('p:spPr/a:xfrm', $node);
836827
if ($oElement instanceof DOMElement) {
837828
if ($oElement->hasAttribute('rot')) {
838829
$oShape->setRotation((int) CommonDrawing::angleToDegrees((int) $oElement->getAttribute('rot')));
839830
}
840831
}
841-
842832
$oElement = $document->getElement('p:spPr/a:xfrm/a:off', $node);
843833
if ($oElement instanceof DOMElement) {
844834
if ($oElement->hasAttribute('x')) {
@@ -848,7 +838,6 @@ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, Abstr
848838
$oShape->setOffsetY(CommonDrawing::emuToPixels((int) $oElement->getAttribute('y')));
849839
}
850840
}
851-
852841
$oElement = $document->getElement('p:spPr/a:xfrm/a:ext', $node);
853842
if ($oElement instanceof DOMElement) {
854843
if ($oElement->hasAttribute('cx')) {
@@ -878,7 +867,6 @@ protected function loadShapeDrawingEmbed(DOMElement $oElement, string $fileRels,
878867
}
879868

880869
$embedPath = $this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'];
881-
882870
$pathEmbed = "ppt/slides/{$embedPath}";
883871

884872
$pathEmbed = explode('/', $pathEmbed);
@@ -889,17 +877,9 @@ protected function loadShapeDrawingEmbed(DOMElement $oElement, string $fileRels,
889877
}
890878
$pathEmbed = implode('/', $pathEmbed);
891879
$contentEmbed = $this->oZip->getFromName($pathEmbed);
892-
893-
$tmpEmbed = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderPPT2007Embed');
894-
895-
file_put_contents($tmpEmbed, $contentEmbed);
896-
897880
$fileName = basename($embedPath);
898881

899-
$oShape
900-
->setName($fileName)
901-
->setFileName($fileName)
902-
->setPath($tmpEmbed, false);
882+
$oShape->loadFromContent($contentEmbed, $fileName);
903883

904884
return $oShape;
905885
}
@@ -926,29 +906,14 @@ protected function loadShapeDrawingImage(XMLReader $document, DOMElement $node,
926906
}
927907
$pathImage = implode('/', $pathImage);
928908
$imageFile = $this->oZip->getFromName($pathImage);
909+
$fileName = basename($pathImage);
910+
929911
if (!$imageFile) {
930912
return $oShape;
931913
}
932914

933915
if ($oShape instanceof Gd) {
934-
$info = getimagesizefromstring($imageFile);
935-
if (!$info) {
936-
return $oShape;
937-
}
938-
$oShape->setMimeType($info['mime']);
939-
$oShape->setRenderingFunction(str_replace('/', '', $info['mime']));
940-
if (!@imagecreatefromstring($imageFile)) {
941-
return $oShape;
942-
}
943-
944-
$tmpEmbed = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderPPT2007ImageGd');
945-
file_put_contents($tmpEmbed, $imageFile);
946-
947-
$fileName = basename($pathImage);
948-
949-
$oShape
950-
->setName($fileName)
951-
->setPath($tmpEmbed);
916+
$oShape->loadFromContent($imageFile, $fileName);
952917
} elseif ($oShape instanceof Base64) {
953918
$oShape->setData('data:image/svg+xml;base64,' . base64_encode($imageFile));
954919
}
@@ -1106,7 +1071,6 @@ protected function loadShapeRichText(XMLReader $document, DOMElement $node, $oSl
11061071
protected function loadShapeTable(XMLReader $document, DOMElement $node, AbstractSlide $oSlide): void
11071072
{
11081073
$this->fileRels = $oSlide->getRelsIndex();
1109-
11101074
$oShape = $oSlide->createTableShape();
11111075

11121076
$oElement = $document->getElement('p:cNvPr', $node);
@@ -1331,6 +1295,7 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
13311295
$oParagraph->getBulletStyle()->setBulletColor($oColor);
13321296
}
13331297
}
1298+
13341299
$arraySubElements = $document->getElements('(a:r|a:br)', $oElement);
13351300
foreach ($arraySubElements as $oSubElement) {
13361301
if (!($oSubElement instanceof DOMElement)) {
@@ -1343,7 +1308,6 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
13431308
$oElementrPr = $document->getElement('a:rPr', $oSubElement);
13441309
if (is_object($oElementrPr)) {
13451310
$oText = $oParagraph->createTextRun();
1346-
13471311
if ($oElementrPr->hasAttribute('b')) {
13481312
$att = $oElementrPr->getAttribute('b');
13491313
$oText->getFont()->setBold('true' == $att || '1' == $att ? true : false);
@@ -1420,7 +1384,6 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
14201384
$oText->getFont()->setCharset((int) $oElementFont->getAttribute('charset'));
14211385
}
14221386
}
1423-
14241387
$oSubSubElement = $document->getElement('a:t', $oSubElement);
14251388
$oText->setText($oSubSubElement->nodeValue);
14261389
}

0 commit comments

Comments
 (0)