Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Image
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Writer\ODText\Element;
20
21use PhpOffice\PhpWord\Element\Image as ElementImage;
22use PhpOffice\PhpWord\Shared\Converter;
23
24/**
25 * Image element writer.
26 *
27 * @since 0.10.0
28 */
29class Image extends AbstractElement
30{
31    /**
32     * Write element.
33     */
34    public function write(): void
35    {
36        $element = $this->getElement();
37        if (!$element instanceof ElementImage) {
38            return;
39        }
40
41        $mediaIndex = $element->getMediaIndex();
42        $target = 'Pictures/' . $element->getTarget();
43        $style = $element->getStyle();
44        $width = Converter::pixelToCm($style->getWidth());
45        $height = Converter::pixelToCm($style->getHeight());
46
47        $xmlWriter = $this->getXmlWriter();
48
49        if (!$this->withoutP) {
50            $xmlWriter->startElement('text:p');
51            $xmlWriter->writeAttribute('text:style-name', 'IM' . $mediaIndex);
52        }
53
54        $this->writeCommentRangeStart();
55        $xmlWriter->startElement('draw:frame');
56        $xmlWriter->writeAttribute('draw:style-name', 'fr' . $mediaIndex);
57        $xmlWriter->writeAttributeIf($this->withoutP, 'draw:text-style-name', 'IM' . $mediaIndex);
58        $xmlWriter->writeAttribute('draw:name', $element->getElementId());
59        $xmlWriter->writeAttribute('text:anchor-type', $element->isWatermark() ? 'at-page' : 'as-char');
60        $xmlWriter->writeAttribute('svg:width', $width . 'cm');
61        $xmlWriter->writeAttribute('svg:height', $height . 'cm');
62        if ($element->isWatermark()) {
63            $xmlWriter->writeAttributeIf($style->getLeft() != 0, 'svg:x', Converter::pixelToCm($style->getLeft()) . 'cm');
64            $xmlWriter->writeAttributeIf($style->getTop() != 0, 'svg:y', Converter::pixelToCm($style->getTop()) . 'cm');
65            $xmlWriter->writeAttribute('draw:z-index', '-1');
66        } else {
67            $xmlWriter->writeAttribute('draw:z-index', $mediaIndex);
68        }
69
70        $xmlWriter->startElement('draw:image');
71        $xmlWriter->writeAttribute('xlink:href', $target);
72        $xmlWriter->writeAttribute('xlink:type', 'simple');
73        $xmlWriter->writeAttribute('xlink:show', 'embed');
74        $xmlWriter->writeAttribute('xlink:actuate', 'onLoad');
75        $xmlWriter->endElement(); // draw:image
76
77        $xmlWriter->endElement(); // draw:frame
78        $this->writeCommentRangeEnd();
79
80        if (!$this->withoutP) {
81            $xmlWriter->endElement(); // text:p
82        }
83    }
84}