Skip to content

Word2007 Reader : Added option to disable loading images #2450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/PhpWord/Reader/AbstractReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ abstract class AbstractReader implements ReaderInterface
*/
protected $fileHandle;

/**
* Load images.
*
* @var bool
*/
protected $imageLoading = true;

/**
* Read data only?
*
Expand All @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Reader/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 21 additions & 2 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ abstract class AbstractPart
*/
protected $rels = [];

/**
* Image Loading.
*
* @var bool
*/
protected $imageLoading = true;

/**
* Read part.
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/PhpWord/Style/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ private function validateLocale($locale)
if ($locale !== null && strlen($locale) === 2) {
return strtolower($locale) . '-' . strtoupper($locale);
}

if ($locale === 'und') {
return 'en-EN';
}
if ($locale !== null && $locale !== 'zxx' && strstr($locale, '-') === false) {
throw new InvalidArgumentException($locale . ' is not a valid language code');
}
Expand Down
58 changes: 48 additions & 10 deletions tests/PhpWordTests/Reader/Word2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'));
}

/**
Expand All @@ -46,19 +48,17 @@ 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'));
}

/**
* Load.
*/
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());
Expand All @@ -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],
];
}
}