Skip to content

Commit 9ee99d2

Browse files
authored
Merge pull request #1324 from troosan/parse_drawings
Support reading of w:drawing for documents produced by word 2011+
2 parents 6475812 + de83da2 commit 9ee99d2

File tree

8 files changed

+98
-4
lines changed

8 files changed

+98
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ v0.15.0 (?? ??? 2018)
3737
- Fix colspan and rowspan for tables in HTML Writer @mattbolt #1292
3838
- Fix parsing of Heading and Title formating @troosan @gthomas2 #465
3939
- Fix Dateformat typo, fix hours casing, add Month-Day-Year formats @ComputerTinker #591
40+
- Support reading of w:drawing for documents produced by word 2011+ @gthomas2 #464 #1324
4041
- Fix missing column width in ODText writer @potofcoffee #413
4142
- Disable entity loader before parsing XML to avoid XXE injection @Tom4t0 #1427
4243

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"php": "^5.3.3 || ^7.0",
6262
"ext-xml": "*",
6363
"zendframework/zend-escaper": "^2.2",
64-
"phpoffice/common": "^0.2"
64+
"phpoffice/common": "^0.2.9"
6565
},
6666
"require-dev": {
6767
"ext-zip": "*",

src/PhpWord/Element/AbstractContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* @method TOC addTOC(mixed $fontStyle = null, mixed $tocStyle = null, int $minDepth = 1, int $maxDepth = 9)
3636
* @method PageBreak addPageBreak()
3737
* @method Table addTable(mixed $style = null)
38-
* @method Image addImage(string $source, mixed $style = null, bool $isWatermark = false)
38+
* @method Image addImage(string $source, mixed $style = null, bool $isWatermark = false, $name = null)
3939
* @method OLEObject addOLEObject(string $source, mixed $style = null)
4040
* @method TextBox addTextBox(mixed $style = null)
4141
* @method Field addField(string $type = null, array $properties = array(), array $options = array(), mixed $text = null)

src/PhpWord/Element/Image.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class Image extends AbstractElement
6565
*/
6666
private $watermark;
6767

68+
/**
69+
* Name of image
70+
*
71+
* @var string
72+
*/
73+
private $name;
74+
6875
/**
6976
* Image type
7077
*
@@ -127,15 +134,17 @@ class Image extends AbstractElement
127134
* @param string $source
128135
* @param mixed $style
129136
* @param bool $watermark
137+
* @param string $name
130138
*
131139
* @throws \PhpOffice\PhpWord\Exception\InvalidImageException
132140
* @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
133141
*/
134-
public function __construct($source, $style = null, $watermark = false)
142+
public function __construct($source, $style = null, $watermark = false, $name = null)
135143
{
136144
$this->source = $source;
137-
$this->setIsWatermark($watermark);
138145
$this->style = $this->setNewStyle(new ImageStyle(), $style, true);
146+
$this->setIsWatermark($watermark);
147+
$this->setName($name);
139148

140149
$this->checkImage();
141150
}
@@ -170,6 +179,26 @@ public function getSourceType()
170179
return $this->sourceType;
171180
}
172181

182+
/**
183+
* Sets the image name
184+
*
185+
* @param string $value
186+
*/
187+
public function setName($value)
188+
{
189+
$this->name = $value;
190+
}
191+
192+
/**
193+
* Get image name
194+
*
195+
* @return null|string
196+
*/
197+
public function getName()
198+
{
199+
return $this->name;
200+
}
201+
173202
/**
174203
* Get image media ID
175204
*

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,20 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
261261
}
262262
$parent->addImage($imageSource);
263263
}
264+
} elseif ($node->nodeName == 'w:drawing') {
265+
// Office 2011 Image
266+
$xmlReader->registerNamespace('wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
267+
$xmlReader->registerNamespace('r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
268+
$xmlReader->registerNamespace('pic', 'http://schemas.openxmlformats.org/drawingml/2006/picture');
269+
$xmlReader->registerNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
270+
271+
$name = $xmlReader->getAttribute('name', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
272+
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
273+
$target = $this->getMediaTarget($docPart, $embedId);
274+
if (!is_null($target)) {
275+
$imageSource = "zip://{$this->docFile}#{$target}";
276+
$parent->addImage($imageSource, null, false, $name);
277+
}
264278
} elseif ($node->nodeName == 'w:object') {
265279
// Object
266280
$rId = $xmlReader->getAttribute('r:id', $node, 'o:OLEObject');

tests/PhpWord/Reader/Word2007/ElementTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,40 @@ public function testReadTitleStyle()
236236
$this->assertEquals('Title', $formattedTitle->getStyle());
237237
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $formattedTitle->getText());
238238
}
239+
240+
/**
241+
* Test reading Drawing
242+
*/
243+
public function testReadDrawing()
244+
{
245+
$documentXml = '<w:p>
246+
<w:r>
247+
<w:drawing xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
248+
<wp:inline distT="0" distB="0" distL="0" distR="0">
249+
<wp:extent cx="5727700" cy="6621145"/>
250+
<wp:docPr id="1" name="Picture 1"/>
251+
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
252+
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
253+
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
254+
<pic:nvPicPr>
255+
<pic:cNvPr id="1" name="file_name.jpg"/>
256+
<pic:cNvPicPr/>
257+
</pic:nvPicPr>
258+
<pic:blipFill>
259+
<a:blip r:embed="rId4" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
260+
</a:blip>
261+
</pic:blipFill>
262+
</pic:pic>
263+
</a:graphicData>
264+
</a:graphic>
265+
</wp:inline>
266+
</w:drawing>
267+
</w:r>
268+
</w:p>';
269+
270+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
271+
272+
$elements = $phpWord->getSection(0)->getElements();
273+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
274+
}
239275
}

tests/PhpWord/Reader/Word2007Test.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,18 @@ public function testLoad()
6464
$doc = TestHelperDOCX::getDocument($phpWord);
6565
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b'));
6666
}
67+
68+
/**
69+
* Load a Word 2011 file
70+
*/
71+
public function testLoadWord2011()
72+
{
73+
$filename = __DIR__ . '/../_files/documents/reader-2011.docx';
74+
$phpWord = IOFactory::load($filename);
75+
76+
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
77+
78+
$doc = TestHelperDOCX::getDocument($phpWord);
79+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[3]/w:r/w:pict/v:shape/v:imagedata'));
80+
}
6781
}
36.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)