Skip to content

Commit a9fc5f8

Browse files
authored
Merge pull request #2450 from PHPOffice/pr2369
Word2007 Reader : Added option to disable loading images
2 parents 34276d3 + eefb2b5 commit a9fc5f8

File tree

6 files changed

+93
-13
lines changed

6 files changed

+93
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1111
- Word2007 Reader/Writer : Added noWrap table cell property by @kernusr in #2359
1212
- HTML Reader : Support for `font-variant: small-caps` by @cambraca in #2117
1313
- Improved TextDirection for styling a cell by @terryzwt in #2429
14+
- Word2007 Reader : Added option to disable loading images by @aelliott1485 in #2450
1415
### Bug fixes
1516

1617
- Fixed wrong mimetype for docx files by @gamerlv in #2416

src/PhpWord/Reader/AbstractReader.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ abstract class AbstractReader implements ReaderInterface
4242
*/
4343
protected $fileHandle;
4444

45+
/**
46+
* Load images.
47+
*
48+
* @var bool
49+
*/
50+
protected $imageLoading = true;
51+
4552
/**
4653
* Read data only?
4754
*
@@ -67,6 +74,18 @@ public function setReadDataOnly($value = true)
6774
return $this;
6875
}
6976

77+
public function hasImageLoading(): bool
78+
{
79+
return $this->imageLoading;
80+
}
81+
82+
public function setImageLoading(bool $value): self
83+
{
84+
$this->imageLoading = $value;
85+
86+
return $this;
87+
}
88+
7089
/**
7190
* Open file for reading.
7291
*

src/PhpWord/Reader/Word2007.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile,
9494
if (class_exists($partClass)) {
9595
/** @var \PhpOffice\PhpWord\Reader\Word2007\AbstractPart $part Type hint */
9696
$part = new $partClass($docFile, $xmlFile);
97+
$part->setImageLoading($this->hasImageLoading());
9798
$part->setRels($relationships);
9899
$part->read($phpWord);
99100
}

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ abstract class AbstractPart
6767
*/
6868
protected $rels = [];
6969

70+
/**
71+
* Image Loading.
72+
*
73+
* @var bool
74+
*/
75+
protected $imageLoading = true;
76+
7077
/**
7178
* Read part.
7279
*/
@@ -94,6 +101,18 @@ public function setRels($value): void
94101
$this->rels = $value;
95102
}
96103

104+
public function setImageLoading(bool $value): self
105+
{
106+
$this->imageLoading = $value;
107+
108+
return $this;
109+
}
110+
111+
public function hasImageLoading(): bool
112+
{
113+
return $this->imageLoading;
114+
}
115+
97116
/**
98117
* Read w:p.
99118
*
@@ -249,7 +268,7 @@ protected function readRunChild(XMLReader $xmlReader, DOMElement $node, Abstract
249268
// Image
250269
$rId = $xmlReader->getAttribute('r:id', $node, 'v:shape/v:imagedata');
251270
$target = $this->getMediaTarget($docPart, $rId);
252-
if (null !== $target) {
271+
if ($this->hasImageLoading() && null !== $target) {
253272
if ('External' == $this->getTargetMode($docPart, $rId)) {
254273
$imageSource = $target;
255274
} else {
@@ -271,7 +290,7 @@ protected function readRunChild(XMLReader $xmlReader, DOMElement $node, Abstract
271290
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
272291
}
273292
$target = $this->getMediaTarget($docPart, $embedId);
274-
if (null !== $target) {
293+
if ($this->hasImageLoading() && null !== $target) {
275294
$imageSource = "zip://{$this->docFile}#{$target}";
276295
$parent->addImage($imageSource, null, false, $name);
277296
}

src/PhpWord/Style/Language.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ private function validateLocale($locale)
256256
if ($locale !== null && strlen($locale) === 2) {
257257
return strtolower($locale) . '-' . strtoupper($locale);
258258
}
259-
259+
if ($locale === 'und') {
260+
return 'en-EN';
261+
}
260262
if ($locale !== null && $locale !== 'zxx' && strstr($locale, '-') === false) {
261263
throw new InvalidArgumentException($locale . ' is not a valid language code');
262264
}

tests/PhpWordTests/Reader/Word2007Test.php

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
namespace PhpOffice\PhpWordTests\Reader;
1919

20+
use PhpOffice\PhpWord\Element\Image;
21+
use PhpOffice\PhpWord\Element\TextRun;
2022
use PhpOffice\PhpWord\IOFactory;
23+
use PhpOffice\PhpWord\PhpWord;
2124
use PhpOffice\PhpWord\Reader\Word2007;
2225
use PhpOffice\PhpWordTests\TestHelperDOCX;
2326

@@ -36,8 +39,7 @@ class Word2007Test extends \PHPUnit\Framework\TestCase
3639
public function testCanRead(): void
3740
{
3841
$object = new Word2007();
39-
$filename = __DIR__ . '/../_files/documents/reader.docx';
40-
self::assertTrue($object->canRead($filename));
42+
self::assertTrue($object->canRead(dirname(__DIR__, 1) . '/_files/documents/reader.docx'));
4143
}
4244

4345
/**
@@ -46,19 +48,17 @@ public function testCanRead(): void
4648
public function testCanReadFailed(): void
4749
{
4850
$object = new Word2007();
49-
$filename = __DIR__ . '/../_files/documents/foo.docx';
50-
self::assertFalse($object->canRead($filename));
51+
self::assertFalse($object->canRead(dirname(__DIR__, 1) . '/_files/documents/foo.docx'));
5152
}
5253

5354
/**
5455
* Load.
5556
*/
5657
public function testLoad(): void
5758
{
58-
$filename = __DIR__ . '/../_files/documents/reader.docx';
59-
$phpWord = IOFactory::load($filename);
59+
$phpWord = IOFactory::load(dirname(__DIR__, 1) . '/_files/documents/reader.docx', 'Word2007');
6060

61-
self::assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
61+
self::assertInstanceOf(PhpWord::class, $phpWord);
6262
self::assertTrue($phpWord->getSettings()->hasDoNotTrackMoves());
6363
self::assertFalse($phpWord->getSettings()->hasDoNotTrackFormatting());
6464
self::assertEquals(100, $phpWord->getSettings()->getZoom());
@@ -72,12 +72,50 @@ public function testLoad(): void
7272
*/
7373
public function testLoadWord2011(): void
7474
{
75-
$filename = __DIR__ . '/../_files/documents/reader-2011.docx';
76-
$phpWord = IOFactory::load($filename);
75+
$reader = new Word2007();
76+
$phpWord = $reader->load(dirname(__DIR__, 1) . '/_files/documents/reader-2011.docx');
7777

78-
self::assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
78+
self::assertInstanceOf(PhpWord::class, $phpWord);
7979

8080
$doc = TestHelperDOCX::getDocument($phpWord);
8181
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[3]/w:r/w:pict/v:shape/v:imagedata'));
8282
}
83+
84+
/**
85+
* Load a Word without/withoutImages.
86+
*
87+
* @dataProvider providerSettingsImageLoading
88+
*/
89+
public function testLoadWord2011SettingsImageLoading(bool $hasImageLoading): void
90+
{
91+
$reader = new Word2007();
92+
$reader->setImageLoading($hasImageLoading);
93+
$phpWord = $reader->load(dirname(__DIR__, 1) . '/_files/documents/reader-2011.docx');
94+
95+
self::assertInstanceOf(PhpWord::class, $phpWord);
96+
97+
$sections = $phpWord->getSections();
98+
self::assertCount(1, $sections);
99+
$section = $sections[0];
100+
$elements = $section->getElements();
101+
self::assertCount(3, $elements);
102+
$element = $elements[2];
103+
self::assertInstanceOf(TextRun::class, $element);
104+
$subElements = $element->getElements();
105+
if ($hasImageLoading) {
106+
self::assertCount(1, $subElements);
107+
$subElement = $subElements[0];
108+
self::assertInstanceOf(Image::class, $subElement);
109+
} else {
110+
self::assertCount(0, $subElements);
111+
}
112+
}
113+
114+
public function providerSettingsImageLoading(): iterable
115+
{
116+
return [
117+
[true],
118+
[false],
119+
];
120+
}
83121
}

0 commit comments

Comments
 (0)