diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d8a8b5605..2439058f6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Word2007 Reader/Writer : Added noWrap table cell property by @kernusr in #2359 - HTML Reader : Support for `font-variant: small-caps` by @cambraca in #2117 - Improved TextDirection for styling a cell by @terryzwt in #2429 +- Word2007 Reader : Added option to disable loading images by @aelliott1485 in #2450 ### Bug fixes - Fixed wrong mimetype for docx files by @gamerlv in #2416 diff --git a/src/PhpWord/Reader/AbstractReader.php b/src/PhpWord/Reader/AbstractReader.php index ff768e3306..da5e6f6e19 100644 --- a/src/PhpWord/Reader/AbstractReader.php +++ b/src/PhpWord/Reader/AbstractReader.php @@ -42,6 +42,13 @@ abstract class AbstractReader implements ReaderInterface */ protected $fileHandle; + /** + * Load images. + * + * @var bool + */ + protected $imageLoading = true; + /** * Read data only? * @@ -67,6 +74,18 @@ public function setReadDataOnly($value = true) return $this; } + public function hasImageLoading(): bool + { + return $this->imageLoading; + } + + public function setImageLoading(bool $value): self + { + $this->imageLoading = $value; + + return $this; + } + /** * Open file for reading. * diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 5f2a69c721..1febe0ff0a 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -94,6 +94,7 @@ private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile, if (class_exists($partClass)) { /** @var \PhpOffice\PhpWord\Reader\Word2007\AbstractPart $part Type hint */ $part = new $partClass($docFile, $xmlFile); + $part->setImageLoading($this->hasImageLoading()); $part->setRels($relationships); $part->read($phpWord); } diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index b1afccaafb..418661d6f0 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -67,6 +67,13 @@ abstract class AbstractPart */ protected $rels = []; + /** + * Image Loading. + * + * @var bool + */ + protected $imageLoading = true; + /** * Read part. */ @@ -94,6 +101,18 @@ public function setRels($value): void $this->rels = $value; } + public function setImageLoading(bool $value): self + { + $this->imageLoading = $value; + + return $this; + } + + public function hasImageLoading(): bool + { + return $this->imageLoading; + } + /** * Read w:p. * @@ -249,7 +268,7 @@ protected function readRunChild(XMLReader $xmlReader, DOMElement $node, Abstract // Image $rId = $xmlReader->getAttribute('r:id', $node, 'v:shape/v:imagedata'); $target = $this->getMediaTarget($docPart, $rId); - if (null !== $target) { + if ($this->hasImageLoading() && null !== $target) { if ('External' == $this->getTargetMode($docPart, $rId)) { $imageSource = $target; } else { @@ -271,7 +290,7 @@ protected function readRunChild(XMLReader $xmlReader, DOMElement $node, Abstract $embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip'); } $target = $this->getMediaTarget($docPart, $embedId); - if (null !== $target) { + if ($this->hasImageLoading() && null !== $target) { $imageSource = "zip://{$this->docFile}#{$target}"; $parent->addImage($imageSource, null, false, $name); } diff --git a/tests/PhpWordTests/Reader/Word2007Test.php b/tests/PhpWordTests/Reader/Word2007Test.php index 999eaf853a..883dc84d53 100644 --- a/tests/PhpWordTests/Reader/Word2007Test.php +++ b/tests/PhpWordTests/Reader/Word2007Test.php @@ -17,7 +17,10 @@ namespace PhpOffice\PhpWordTests\Reader; +use PhpOffice\PhpWord\Element\Image; +use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\IOFactory; +use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Reader\Word2007; use PhpOffice\PhpWordTests\TestHelperDOCX; @@ -36,8 +39,7 @@ class Word2007Test extends \PHPUnit\Framework\TestCase public function testCanRead(): void { $object = new Word2007(); - $filename = __DIR__ . '/../_files/documents/reader.docx'; - self::assertTrue($object->canRead($filename)); + self::assertTrue($object->canRead(dirname(__DIR__, 1) . '/_files/documents/reader.docx')); } /** @@ -46,8 +48,7 @@ public function testCanRead(): void public function testCanReadFailed(): void { $object = new Word2007(); - $filename = __DIR__ . '/../_files/documents/foo.docx'; - self::assertFalse($object->canRead($filename)); + self::assertFalse($object->canRead(dirname(__DIR__, 1) . '/_files/documents/foo.docx')); } /** @@ -55,10 +56,9 @@ public function testCanReadFailed(): void */ public function testLoad(): void { - $filename = __DIR__ . '/../_files/documents/reader.docx'; - $phpWord = IOFactory::load($filename); + $phpWord = IOFactory::load(dirname(__DIR__, 1) . '/_files/documents/reader.docx', 'Word2007'); - self::assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord); + self::assertInstanceOf(PhpWord::class, $phpWord); self::assertTrue($phpWord->getSettings()->hasDoNotTrackMoves()); self::assertFalse($phpWord->getSettings()->hasDoNotTrackFormatting()); self::assertEquals(100, $phpWord->getSettings()->getZoom()); @@ -72,12 +72,50 @@ public function testLoad(): void */ public function testLoadWord2011(): void { - $filename = __DIR__ . '/../_files/documents/reader-2011.docx'; - $phpWord = IOFactory::load($filename); + $reader = new Word2007(); + $phpWord = $reader->load(dirname(__DIR__, 1) . '/_files/documents/reader-2011.docx'); - self::assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord); + self::assertInstanceOf(PhpWord::class, $phpWord); $doc = TestHelperDOCX::getDocument($phpWord); self::assertTrue($doc->elementExists('/w:document/w:body/w:p[3]/w:r/w:pict/v:shape/v:imagedata')); } + + /** + * Load a Word without/withoutImages. + * + * @dataProvider providerSettingsImageLoading + */ + public function testLoadWord2011SettingsImageLoading(bool $hasImageLoading): void + { + $reader = new Word2007(); + $reader->setImageLoading($hasImageLoading); + $phpWord = $reader->load(dirname(__DIR__, 1) . '/_files/documents/reader-2011.docx'); + + self::assertInstanceOf(PhpWord::class, $phpWord); + + $sections = $phpWord->getSections(); + self::assertCount(1, $sections); + $section = $sections[0]; + $elements = $section->getElements(); + self::assertCount(3, $elements); + $element = $elements[2]; + self::assertInstanceOf(TextRun::class, $element); + $subElements = $element->getElements(); + if ($hasImageLoading) { + self::assertCount(1, $subElements); + $subElement = $subElements[0]; + self::assertInstanceOf(Image::class, $subElement); + } else { + self::assertCount(0, $subElements); + } + } + + public function providerSettingsImageLoading(): iterable + { + return [ + [true], + [false], + ]; + } }