Skip to content

Commit 92a4b59

Browse files
committed
fixed Word2007 Reader to read images when pictures is between <wp:anchor>
1 parent 733f845 commit 92a4b59

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
270270

271271
$name = $xmlReader->getAttribute('name', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
272272
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
273+
if (is_null($name) && is_null($embedId)) {
274+
$name = $xmlReader->getAttribute('name', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
275+
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
276+
}
277+
273278
$target = $this->getMediaTarget($docPart, $embedId);
274279
if (!is_null($target)) {
275280
$imageSource = "zip://{$this->docFile}#{$target}";

tests/PhpWord/Reader/Word2007/ElementTest.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function testReadDrawing()
252252
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
253253
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
254254
<pic:nvPicPr>
255-
<pic:cNvPr id="1" name="file_name.jpg"/>
255+
<pic:cNvPr id="1" name="mars.jpg"/>
256256
<pic:cNvPicPr/>
257257
</pic:nvPicPr>
258258
<pic:blipFill>
@@ -267,9 +267,79 @@ public function testReadDrawing()
267267
</w:r>
268268
</w:p>';
269269

270-
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
270+
//$resolutionXml = '<Relationship Id="rId4" Target="media/mars.jpg" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/>';
271+
$relationships = array(
272+
'document' => array(
273+
'rId4' => array(
274+
'target' => 'media/mars.jpg',
275+
'type' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
276+
),
277+
),
278+
);
279+
280+
$media = array(
281+
__DIR__ . '/../../_files/images/mars.jpg',
282+
);
283+
284+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml), $relationships, $media);
271285

272286
$elements = $phpWord->getSection(0)->getElements();
273287
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
288+
289+
$children = $elements[0]->getElements();
290+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Image', $children[0]);
291+
}
292+
293+
/**
294+
* Test reading Drawing with anchors
295+
*/
296+
public function testReadDrawingWithAnchor()
297+
{
298+
$documentXml = '<w:p>
299+
<w:r>
300+
<w:drawing xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
301+
<wp:anchor allowOverlap="1" behindDoc="0" distB="0" distL="0" distR="0" distT="0" layoutInCell="1" locked="0" relativeHeight="2" simplePos="0">
302+
<wp:extent cx="5727700" cy="6621145"/>
303+
<wp:docPr id="1" name="Picture 1"/>
304+
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
305+
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
306+
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
307+
<pic:nvPicPr>
308+
<pic:cNvPr id="1" name="mars.jpg"/>
309+
<pic:cNvPicPr/>
310+
</pic:nvPicPr>
311+
<pic:blipFill>
312+
<a:blip r:embed="rId4" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
313+
</a:blip>
314+
</pic:blipFill>
315+
</pic:pic>
316+
</a:graphicData>
317+
</a:graphic>
318+
</wp:anchor>
319+
</w:drawing>
320+
</w:r>
321+
</w:p>';
322+
323+
//$resolutionXml = '<Relationship Id="rId4" Target="media/mars.jpg" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/>';
324+
$relationships = array(
325+
'document' => array(
326+
'rId4' => array(
327+
'target' => 'media/mars.jpg',
328+
'type' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
329+
),
330+
),
331+
);
332+
333+
$media = array(
334+
__DIR__ . '/../../_files/images/mars.jpg',
335+
);
336+
337+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml), $relationships, $media);
338+
339+
$elements = $phpWord->getSection(0)->getElements();
340+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);
341+
342+
$children = $elements[0]->getElements();
343+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Image', $children[0]);
274344
}
275345
}

tests/PhpWord/_includes/AbstractTestReader.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ abstract class AbstractTestReader extends \PHPUnit\Framework\TestCase
3333
/**
3434
* Builds a PhpWord instance based on the xml passed
3535
*
36-
* @param string $documentXml
37-
* @param null|string $stylesXml
36+
* @param array $partXmls
37+
* @param array $rels
38+
* @param array $media
3839
* @return \PhpOffice\PhpWord\PhpWord
3940
*/
40-
protected function getDocumentFromString(array $partXmls = array())
41+
protected function getDocumentFromString(array $partXmls = array(), array $rels = array(), array $media = array())
4142
{
4243
$file = __DIR__ . '/../_files/temp.docx';
4344
$zip = new \ZipArchive();
@@ -47,13 +48,26 @@ protected function getDocumentFromString(array $partXmls = array())
4748
$zip->addFromString("{$partName}.xml", str_replace('{toReplace}', $partXmls[$partName], $this->parts[$partName]['xml']));
4849
}
4950
}
51+
52+
if (!empty($media)) {
53+
$zip->addEmptyDir('media');
54+
55+
foreach ($media as $image) {
56+
$zip->addFile($image, 'media/' . basename($image));
57+
}
58+
}
5059
$zip->close();
5160

5261
$phpWord = new PhpWord();
5362
foreach ($this->parts as $partName => $part) {
5463
if (array_key_exists($partName, $partXmls)) {
5564
$className = $this->parts[$partName]['class'];
5665
$reader = new $className($file, "{$partName}.xml");
66+
67+
if (isset($rels[$partName])) {
68+
$reader->setRels($rels);
69+
}
70+
5771
$reader->read($phpWord);
5872
}
5973
}

0 commit comments

Comments
 (0)