Skip to content

Commit

Permalink
Merge 92a4b59 into 733f845
Browse files Browse the repository at this point in the history
  • Loading branch information
begnini committed Jan 6, 2020
2 parents 733f845 + 92a4b59 commit b941b1b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Expand Up @@ -270,6 +270,11 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac

$name = $xmlReader->getAttribute('name', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
if (is_null($name) && is_null($embedId)) {
$name = $xmlReader->getAttribute('name', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
$embedId = $xmlReader->getAttribute('r:embed', $node, 'wp:anchor/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
}

$target = $this->getMediaTarget($docPart, $embedId);
if (!is_null($target)) {
$imageSource = "zip://{$this->docFile}#{$target}";
Expand Down
74 changes: 72 additions & 2 deletions tests/PhpWord/Reader/Word2007/ElementTest.php
Expand Up @@ -252,7 +252,7 @@ public function testReadDrawing()
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="1" name="file_name.jpg"/>
<pic:cNvPr id="1" name="mars.jpg"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
Expand All @@ -267,9 +267,79 @@ public function testReadDrawing()
</w:r>
</w:p>';

$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
//$resolutionXml = '<Relationship Id="rId4" Target="media/mars.jpg" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/>';
$relationships = array(
'document' => array(
'rId4' => array(
'target' => 'media/mars.jpg',
'type' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
),
),
);

$media = array(
__DIR__ . '/../../_files/images/mars.jpg',
);

$phpWord = $this->getDocumentFromString(array('document' => $documentXml), $relationships, $media);

$elements = $phpWord->getSection(0)->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);

$children = $elements[0]->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Image', $children[0]);
}

/**
* Test reading Drawing with anchors
*/
public function testReadDrawingWithAnchor()
{
$documentXml = '<w:p>
<w:r>
<w:drawing xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
<wp:anchor allowOverlap="1" behindDoc="0" distB="0" distL="0" distR="0" distT="0" layoutInCell="1" locked="0" relativeHeight="2" simplePos="0">
<wp:extent cx="5727700" cy="6621145"/>
<wp:docPr id="1" name="Picture 1"/>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="1" name="mars.jpg"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId4" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
</a:blip>
</pic:blipFill>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:anchor>
</w:drawing>
</w:r>
</w:p>';

//$resolutionXml = '<Relationship Id="rId4" Target="media/mars.jpg" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/>';
$relationships = array(
'document' => array(
'rId4' => array(
'target' => 'media/mars.jpg',
'type' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
),
),
);

$media = array(
__DIR__ . '/../../_files/images/mars.jpg',
);

$phpWord = $this->getDocumentFromString(array('document' => $documentXml), $relationships, $media);

$elements = $phpWord->getSection(0)->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\TextRun', $elements[0]);

$children = $elements[0]->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Image', $children[0]);
}
}
20 changes: 17 additions & 3 deletions tests/PhpWord/_includes/AbstractTestReader.php
Expand Up @@ -33,11 +33,12 @@ abstract class AbstractTestReader extends \PHPUnit\Framework\TestCase
/**
* Builds a PhpWord instance based on the xml passed
*
* @param string $documentXml
* @param null|string $stylesXml
* @param array $partXmls
* @param array $rels
* @param array $media
* @return \PhpOffice\PhpWord\PhpWord
*/
protected function getDocumentFromString(array $partXmls = array())
protected function getDocumentFromString(array $partXmls = array(), array $rels = array(), array $media = array())
{
$file = __DIR__ . '/../_files/temp.docx';
$zip = new \ZipArchive();
Expand All @@ -47,13 +48,26 @@ protected function getDocumentFromString(array $partXmls = array())
$zip->addFromString("{$partName}.xml", str_replace('{toReplace}', $partXmls[$partName], $this->parts[$partName]['xml']));
}
}

if (!empty($media)) {
$zip->addEmptyDir('media');

foreach ($media as $image) {
$zip->addFile($image, 'media/' . basename($image));
}
}
$zip->close();

$phpWord = new PhpWord();
foreach ($this->parts as $partName => $part) {
if (array_key_exists($partName, $partXmls)) {
$className = $this->parts[$partName]['class'];
$reader = new $className($file, "{$partName}.xml");

if (isset($rels[$partName])) {
$reader->setRels($rels);
}

$reader->read($phpWord);
}
}
Expand Down

0 comments on commit b941b1b

Please sign in to comment.