Skip to content

Commit 6e463bb

Browse files
committed
handling width and height in wp:drawing (images)
1 parent 92a4b59 commit 6e463bb

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

src/PhpWord/Element/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class Image extends AbstractElement
142142
public function __construct($source, $style = null, $watermark = false, $name = null)
143143
{
144144
$this->source = $source;
145-
$this->style = $this->setNewStyle(new ImageStyle(), $style, true);
145+
$this->style = $this->setNewStyle(new ImageStyle(), $style, is_null($style));
146146
$this->setIsWatermark($watermark);
147147
$this->setName($name);
148148

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpOffice\PhpWord\Element\TextRun;
2424
use PhpOffice\PhpWord\Element\TrackChange;
2525
use PhpOffice\PhpWord\PhpWord;
26+
use PhpOffice\PhpWord\Style\Image as ImageStyle;
2627

2728
/**
2829
* Abstract part reader
@@ -263,23 +264,7 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
263264
}
264265
} elseif ($node->nodeName == 'w:drawing') {
265266
// 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-
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-
278-
$target = $this->getMediaTarget($docPart, $embedId);
279-
if (!is_null($target)) {
280-
$imageSource = "zip://{$this->docFile}#{$target}";
281-
$parent->addImage($imageSource, null, false, $name);
282-
}
267+
$this->readImage($xmlReader, $node, $parent, $docPart);
283268
} elseif ($node->nodeName == 'w:object') {
284269
// Object
285270
$rId = $xmlReader->getAttribute('r:id', $node, 'o:OLEObject');
@@ -318,6 +303,53 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
318303
}
319304
}
320305

306+
/**
307+
* Parses node w:drawing
308+
*
309+
* @param XMLReader $xmlReader
310+
* @param \DOMElement $node
311+
* @param AbstractContainer $parent
312+
* @param string $docPart
313+
*/
314+
protected function readImage(xmlReader $xmlReader, \DOMElement $node, AbstractContainer $parent, $docPart)
315+
{
316+
$xmlReader->registerNamespace('wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
317+
$xmlReader->registerNamespace('r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
318+
$xmlReader->registerNamespace('pic', 'http://schemas.openxmlformats.org/drawingml/2006/picture');
319+
$xmlReader->registerNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
320+
321+
$name = null;
322+
$embedId = null;
323+
324+
$graphicParent = $xmlReader->getElement('wp:inline', $node);
325+
if (is_null($graphicParent)) {
326+
$graphicParent = $xmlReader->getElement('wp:anchor', $node);
327+
}
328+
/* get the height and width of the image in this document */
329+
$imageWidth = $xmlReader->getAttribute('cx', $graphicParent, 'wp:extent');
330+
$imageHeight = $xmlReader->getAttribute('cy', $graphicParent, 'wp:extent');
331+
$style = null;
332+
if (!is_null($imageWidth) && !is_null($imageHeight)) {
333+
$style = new ImageStyle();
334+
$style->setUnit(ImageStyle::UNIT_PT);
335+
336+
/* transform EMUs to pt - 1 inch is 914400 EMUs */
337+
$imageWidth = (int) ($imageWidth) / (914400 / 72);
338+
$imageHeight = (int) ($imageHeight) / (914400 / 72);
339+
$style->setWidth($imageWidth);
340+
$style->setHeight($imageHeight);
341+
}
342+
343+
$name = $xmlReader->getAttribute('name', $graphicParent, 'a:graphic/a:graphicData/pic:pic/pic:nvPicPr/pic:cNvPr');
344+
$embedId = $xmlReader->getAttribute('r:embed', $graphicParent, 'a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip');
345+
346+
$target = $this->getMediaTarget($docPart, $embedId);
347+
if (!is_null($target)) {
348+
$imageSource = "zip://{$this->docFile}#{$target}";
349+
$parent->addImage($imageSource, $style, false, $name);
350+
}
351+
}
352+
321353
/**
322354
* Read w:tbl.
323355
*

tests/PhpWord/Reader/Word2007/ElementTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,5 +341,12 @@ public function testReadDrawingWithAnchor()
341341

342342
$children = $elements[0]->getElements();
343343
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Image', $children[0]);
344+
345+
$style = $children[0]->getStyle();
346+
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Image', $style);
347+
348+
$this->assertEquals('pt', $style->getUnit());
349+
$this->assertEquals('451', $style->getWidth());
350+
$this->assertEquals('521.35', $style->getHeight());
344351
}
345352
}

0 commit comments

Comments
 (0)