Skip to content

Commit b926b71

Browse files
authored
Reader : Option to not load images (#850)
1 parent c691a45 commit b926b71

File tree

10 files changed

+978
-8
lines changed

10 files changed

+978
-8
lines changed

docs/changes/1.2.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#839](https://github.com/PHPOffice/PHPPresentation/pull/839)
1010
- `phpoffice/phpspreadsheet`: Allow version 3.0 by [@Progi1984](https://github.com/Progi1984) fixing [#836](https://github.com/PHPOffice/PHPPresentation/pull/836) in [#839](https://github.com/PHPOffice/PHPPresentation/pull/839)
1111
- `createAutoShape` : Add method to create geometric shapes by [@mhasanshahid](https://github.com/mhasanshahid) & [@Progi1984](https://github.com/Progi1984) in [#848](https://github.com/PHPOffice/PHPPresentation/pull/848)
12+
- Reader : Option to not load images by [@Progi1984](https://github.com/Progi1984) fixing [#795](https://github.com/PHPOffice/PHPPresentation/pull/795) in [#850](https://github.com/PHPOffice/PHPPresentation/pull/850)
1213

1314
## Bug fixes
1415

docs/usage/readers.md

+45
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ $reader = IOFactory::createReader('ODPresentation');
1010
$reader->load(__DIR__ . '/sample.odp');
1111
```
1212

13+
### Options
14+
15+
#### Load without images
16+
17+
You can load a presentation without images.
18+
19+
``` php
20+
<?php
21+
22+
use PhpOffice\PhpPresentation\Reader\ODPresentation;
23+
24+
$reader = new ODPresentation();
25+
$reader->load(__DIR__ . '/sample.odp', ODPresentation::SKIP_IMAGES);
26+
```
27+
1328
## PowerPoint97
1429
The name of the reader is `PowerPoint97`.
1530

@@ -20,6 +35,21 @@ $reader = IOFactory::createReader('PowerPoint97');
2035
$reader->load(__DIR__ . '/sample.ppt');
2136
```
2237

38+
### Options
39+
40+
#### Load without images
41+
42+
You can load a presentation without images.
43+
44+
``` php
45+
<?php
46+
47+
use PhpOffice\PhpPresentation\Reader\PowerPoint97;
48+
49+
$reader = new PowerPoint97();
50+
$reader->load(__DIR__ . '/sample.ppt', PowerPoint97::SKIP_IMAGES);
51+
```
52+
2353
## PowerPoint2007
2454
The name of the reader is `PowerPoint2007`.
2555

@@ -30,6 +60,21 @@ $reader = IOFactory::createReader('PowerPoint2007');
3060
$reader->load(__DIR__ . '/sample.pptx');
3161
```
3262

63+
### Options
64+
65+
#### Load without images
66+
67+
You can load a presentation without images.
68+
69+
``` php
70+
<?php
71+
72+
use PhpOffice\PhpPresentation\Reader\PowerPoint2007;
73+
74+
$reader = new PowerPoint2007();
75+
$reader->load(__DIR__ . '/sample.pptx', PowerPoint2007::SKIP_IMAGES);
76+
```
77+
3378
## Serialized
3479
The name of the reader is `Serialized`.
3580

src/PhpPresentation/Reader/ODPresentation.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class ODPresentation implements ReaderInterface
8282
*/
8383
protected $levelParagraph = 0;
8484

85+
/**
86+
* @var bool
87+
*/
88+
protected $loadImages = true;
89+
8590
/**
8691
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
8792
*/
@@ -116,13 +121,15 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''):
116121
/**
117122
* Loads PhpPresentation Serialized file.
118123
*/
119-
public function load(string $pFilename): PhpPresentation
124+
public function load(string $pFilename, int $flags = 0): PhpPresentation
120125
{
121126
// Unserialize... First make sure the file supports it!
122127
if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
123128
throw new InvalidFileFormatException($pFilename, self::class);
124129
}
125130

131+
$this->loadImages = !((bool) ($flags & self::SKIP_IMAGES));
132+
126133
return $this->loadFile($pFilename);
127134
}
128135

@@ -532,7 +539,7 @@ protected function loadSlide(DOMElement $nodeSlide): bool
532539
}
533540
foreach ($this->oXMLReader->getElements('draw:frame', $nodeSlide) as $oNodeFrame) {
534541
if ($oNodeFrame instanceof DOMElement) {
535-
if ($this->oXMLReader->getElement('draw:image', $oNodeFrame)) {
542+
if ($this->loadImages && $this->oXMLReader->getElement('draw:image', $oNodeFrame)) {
536543
$this->loadShapeDrawing($oNodeFrame);
537544

538545
continue;

src/PhpPresentation/Reader/PowerPoint2007.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ class PowerPoint2007 implements ReaderInterface
9595
*/
9696
protected $fileRels;
9797

98+
/**
99+
* @var bool
100+
*/
101+
protected $loadImages = true;
102+
98103
/**
99104
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
100105
*/
@@ -129,13 +134,15 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''):
129134
/**
130135
* Loads PhpPresentation Serialized file.
131136
*/
132-
public function load(string $pFilename): PhpPresentation
137+
public function load(string $pFilename, int $flags = 0): PhpPresentation
133138
{
134139
// Unserialize... First make sure the file supports it!
135140
if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
136141
throw new InvalidFileFormatException($pFilename, self::class);
137142
}
138143

144+
$this->loadImages = !((bool) ($flags & self::SKIP_IMAGES));
145+
139146
return $this->loadFile($pFilename);
140147
}
141148

@@ -1498,7 +1505,7 @@ protected function loadSlideShapes($oSlide, DOMNodeList $oElements, XMLReader $x
14981505

14991506
break;
15001507
case 'p:pic':
1501-
if ($oSlide instanceof AbstractSlide) {
1508+
if ($this->loadImages && $oSlide instanceof AbstractSlide) {
15021509
$this->loadShapeDrawing($xmlReader, $oNode, $oSlide);
15031510
}
15041511

src/PhpPresentation/Reader/PowerPoint97.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ class PowerPoint97 implements ReaderInterface
395395
*/
396396
private $filename;
397397

398+
/**
399+
* @var bool
400+
*/
401+
protected $loadImages = true;
402+
398403
/**
399404
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
400405
*/
@@ -428,7 +433,7 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''):
428433
/**
429434
* Loads PhpPresentation Serialized file.
430435
*/
431-
public function load(string $pFilename): PhpPresentation
436+
public function load(string $pFilename, int $flags = 0): PhpPresentation
432437
{
433438
// Unserialize... First make sure the file supports it!
434439
if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
@@ -437,6 +442,8 @@ public function load(string $pFilename): PhpPresentation
437442

438443
$this->filename = $pFilename;
439444

445+
$this->loadImages = !((bool) ($flags & self::SKIP_IMAGES));
446+
440447
return $this->loadFile();
441448
}
442449

@@ -1581,7 +1588,7 @@ private function readRecordOfficeArtSpContainer(string $stream, int $pos)
15811588
if (isset($shpPrimaryOptions['pib'])) {
15821589
// isDrawing
15831590
$drawingPib = $shpPrimaryOptions['pib'];
1584-
if (isset($this->arrayPictures[$drawingPib - 1])) {
1591+
if ($this->loadImages && isset($this->arrayPictures[$drawingPib - 1])) {
15851592
$gdImage = imagecreatefromstring($this->arrayPictures[$drawingPib - 1]);
15861593
$arrayReturn['shape'] = new Drawing\Gd();
15871594
$arrayReturn['shape']->setImageResource($gdImage);

src/PhpPresentation/Reader/ReaderInterface.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
*/
2828
interface ReaderInterface
2929
{
30+
/**
31+
* Skip loading of images.
32+
*/
33+
public const SKIP_IMAGES = 1;
34+
3035
/**
3136
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
3237
*/
@@ -35,5 +40,5 @@ public function canRead(string $pFilename): bool;
3540
/**
3641
* Loads PhpPresentation from file.
3742
*/
38-
public function load(string $pFilename): PhpPresentation;
43+
public function load(string $pFilename, int $flags = 0): PhpPresentation;
3944
}

src/PhpPresentation/Reader/Serialized.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename): bool
5858
/**
5959
* Loads PhpPresentation Serialized file.
6060
*/
61-
public function load(string $pFilename): PhpPresentation
61+
public function load(string $pFilename, int $flags = 0): PhpPresentation
6262
{
6363
// Check if file exists
6464
if (!file_exists($pFilename)) {

0 commit comments

Comments
 (0)