Skip to content

Commit

Permalink
Reader : Option to not load images
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Feb 3, 2025
1 parent c691a45 commit 17a1c04
Show file tree
Hide file tree
Showing 10 changed files with 978 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#839](https://github.com/PHPOffice/PHPPresentation/pull/839)
- `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)
- `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)
- 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)

## Bug fixes

Expand Down
45 changes: 45 additions & 0 deletions docs/usage/readers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ $reader = IOFactory::createReader('ODPresentation');
$reader->load(__DIR__ . '/sample.odp');
```

### Options

#### Load without images

You can load a presentation without images.

``` php
<?php

use PhpOffice\PhpPresentation\Reader\ODPresentation;

$reader = new ODPresentation();
$reader->load(__DIR__ . '/sample.odp', ODPresentation::SKIP_IMAGES);
```

## PowerPoint97
The name of the reader is `PowerPoint97`.

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

### Options

#### Load without images

You can load a presentation without images.

``` php
<?php

use PhpOffice\PhpPresentation\Reader\PowerPoint97;

$reader = new PowerPoint97();
$reader->load(__DIR__ . '/sample.ppt', PowerPoint97::SKIP_IMAGES);
```

## PowerPoint2007
The name of the reader is `PowerPoint2007`.

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

### Options

#### Load without images

You can load a presentation without images.

``` php
<?php

use PhpOffice\PhpPresentation\Reader\PowerPoint2007;

$reader = new PowerPoint2007();
$reader->load(__DIR__ . '/sample.pptx', PowerPoint2007::SKIP_IMAGES);
```

## Serialized
The name of the reader is `Serialized`.

Expand Down
11 changes: 9 additions & 2 deletions src/PhpPresentation/Reader/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class ODPresentation implements ReaderInterface
*/
protected $levelParagraph = 0;

/**
* @var bool
*/
protected $loadImages = true;

/**
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
*/
Expand Down Expand Up @@ -116,13 +121,15 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''):
/**
* Loads PhpPresentation Serialized file.
*/
public function load(string $pFilename): PhpPresentation
public function load(string $pFilename, int $flags = 0): PhpPresentation
{
// Unserialize... First make sure the file supports it!
if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
throw new InvalidFileFormatException($pFilename, self::class);
}

$this->loadImages = !((bool) ($flags & self::SKIP_IMAGES));

return $this->loadFile($pFilename);
}

Expand Down Expand Up @@ -532,7 +539,7 @@ protected function loadSlide(DOMElement $nodeSlide): bool
}
foreach ($this->oXMLReader->getElements('draw:frame', $nodeSlide) as $oNodeFrame) {
if ($oNodeFrame instanceof DOMElement) {
if ($this->oXMLReader->getElement('draw:image', $oNodeFrame)) {
if ($this->loadImages && $this->oXMLReader->getElement('draw:image', $oNodeFrame)) {
$this->loadShapeDrawing($oNodeFrame);

continue;
Expand Down
11 changes: 9 additions & 2 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class PowerPoint2007 implements ReaderInterface
*/
protected $fileRels;

/**
* @var bool
*/
protected $loadImages = true;

/**
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
*/
Expand Down Expand Up @@ -129,13 +134,15 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''):
/**
* Loads PhpPresentation Serialized file.
*/
public function load(string $pFilename): PhpPresentation
public function load(string $pFilename, int $flags = 0): PhpPresentation
{
// Unserialize... First make sure the file supports it!
if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
throw new InvalidFileFormatException($pFilename, self::class);
}

$this->loadImages = !((bool) ($flags & self::SKIP_IMAGES));

return $this->loadFile($pFilename);
}

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

break;
case 'p:pic':
if ($oSlide instanceof AbstractSlide) {
if ($this->loadImages && $oSlide instanceof AbstractSlide) {
$this->loadShapeDrawing($xmlReader, $oNode, $oSlide);
}

Expand Down
11 changes: 9 additions & 2 deletions src/PhpPresentation/Reader/PowerPoint97.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ class PowerPoint97 implements ReaderInterface
*/
private $filename;

/**
* @var bool
*/
protected $loadImages = true;

/**
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
*/
Expand Down Expand Up @@ -428,7 +433,7 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''):
/**
* Loads PhpPresentation Serialized file.
*/
public function load(string $pFilename): PhpPresentation
public function load(string $pFilename, int $flags = 0): PhpPresentation
{
// Unserialize... First make sure the file supports it!
if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
Expand All @@ -437,6 +442,8 @@ public function load(string $pFilename): PhpPresentation

$this->filename = $pFilename;

$this->loadImages = !((bool) ($flags & self::SKIP_IMAGES));

return $this->loadFile();
}

Expand Down Expand Up @@ -1581,7 +1588,7 @@ private function readRecordOfficeArtSpContainer(string $stream, int $pos)
if (isset($shpPrimaryOptions['pib'])) {
// isDrawing
$drawingPib = $shpPrimaryOptions['pib'];
if (isset($this->arrayPictures[$drawingPib - 1])) {
if ($this->loadImages && isset($this->arrayPictures[$drawingPib - 1])) {
$gdImage = imagecreatefromstring($this->arrayPictures[$drawingPib - 1]);
$arrayReturn['shape'] = new Drawing\Gd();
$arrayReturn['shape']->setImageResource($gdImage);
Expand Down
7 changes: 6 additions & 1 deletion src/PhpPresentation/Reader/ReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
*/
interface ReaderInterface
{
/**
* Skip loading of images.
*/
public const SKIP_IMAGES = 1;

/**
* Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
*/
Expand All @@ -35,5 +40,5 @@ public function canRead(string $pFilename): bool;
/**
* Loads PhpPresentation from file.
*/
public function load(string $pFilename): PhpPresentation;
public function load(string $pFilename, int $flags = 0): PhpPresentation;
}
2 changes: 1 addition & 1 deletion src/PhpPresentation/Reader/Serialized.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename): bool
/**
* Loads PhpPresentation Serialized file.
*/
public function load(string $pFilename): PhpPresentation
public function load(string $pFilename, int $flags = 0): PhpPresentation
{
// Check if file exists
if (!file_exists($pFilename)) {
Expand Down
Loading

0 comments on commit 17a1c04

Please sign in to comment.