|
23 | 23 | use PhpOffice\PhpWord\Element\TextRun;
|
24 | 24 | use PhpOffice\PhpWord\Element\TrackChange;
|
25 | 25 | use PhpOffice\PhpWord\PhpWord;
|
| 26 | +use PhpOffice\PhpWord\Style\Image as ImageStyle; |
26 | 27 |
|
27 | 28 | /**
|
28 | 29 | * Abstract part reader
|
@@ -263,23 +264,7 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
|
263 | 264 | }
|
264 | 265 | } elseif ($node->nodeName == 'w:drawing') {
|
265 | 266 | // 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); |
283 | 268 | } elseif ($node->nodeName == 'w:object') {
|
284 | 269 | // Object
|
285 | 270 | $rId = $xmlReader->getAttribute('r:id', $node, 'o:OLEObject');
|
@@ -318,6 +303,53 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac
|
318 | 303 | }
|
319 | 304 | }
|
320 | 305 |
|
| 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 | + |
321 | 353 | /**
|
322 | 354 | * Read w:tbl.
|
323 | 355 | *
|
|
0 commit comments