Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Image
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 writeImage
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
5
 writeWatermark
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Writer\Word2007\Element;
19
20use PhpOffice\PhpWord\Element\Image as ImageElement;
21use PhpOffice\PhpWord\Shared\XMLWriter;
22use PhpOffice\PhpWord\Style\Font as FontStyle;
23use PhpOffice\PhpWord\Style\Frame as FrameStyle;
24use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
25use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter;
26
27/**
28 * Image element writer.
29 *
30 * @since 0.10.0
31 */
32class Image extends AbstractElement
33{
34    /**
35     * Write element.
36     */
37    public function write(): void
38    {
39        $xmlWriter = $this->getXmlWriter();
40        $element = $this->getElement();
41        if (!$element instanceof ImageElement) {
42            return;
43        }
44
45        if ($element->isWatermark()) {
46            $this->writeWatermark($xmlWriter, $element);
47        } else {
48            $this->writeImage($xmlWriter, $element);
49        }
50    }
51
52    /**
53     * Write image element.
54     */
55    private function writeImage(XMLWriter $xmlWriter, ImageElement $element): void
56    {
57        $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
58        $style = $element->getStyle();
59        $styleWriter = new ImageStyleWriter($xmlWriter, $style);
60
61        if (!$this->withoutP) {
62            $xmlWriter->startElement('w:p');
63            $styleWriter->writeAlignment();
64        }
65        $this->writeCommentRangeStart();
66
67        $xmlWriter->startElement('w:r');
68
69        // Write position
70        $position = $style->getPosition();
71        if ($position && $style->getWrap() == FrameStyle::WRAP_INLINE) {
72            $fontStyle = new FontStyle('text');
73            $fontStyle->setPosition($position);
74            $fontStyleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
75            $fontStyleWriter->write();
76        }
77
78        $xmlWriter->startElement('w:pict');
79        $xmlWriter->startElement('v:shape');
80        $xmlWriter->writeAttribute('type', '#_x0000_t75');
81        $xmlWriter->writeAttribute('stroked', 'f');
82
83        $styleWriter->write();
84
85        $xmlWriter->startElement('v:imagedata');
86        $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
87        $xmlWriter->writeAttribute('o:title', '');
88        $xmlWriter->endElement(); // v:imagedata
89
90        $xmlWriter->endElement(); // v:shape
91        $xmlWriter->endElement(); // w:pict
92        $xmlWriter->endElement(); // w:r
93
94        $this->endElementP();
95    }
96
97    /**
98     * Write watermark element.
99     */
100    private function writeWatermark(XMLWriter $xmlWriter, ImageElement $element): void
101    {
102        $rId = $element->getRelationId();
103        $style = $element->getStyle();
104        $style->setPositioning('absolute');
105        $styleWriter = new ImageStyleWriter($xmlWriter, $style);
106
107        if (!$this->withoutP) {
108            $xmlWriter->startElement('w:p');
109        }
110        $xmlWriter->startElement('w:r');
111        $xmlWriter->startElement('w:pict');
112        $xmlWriter->startElement('v:shape');
113        $xmlWriter->writeAttribute('type', '#_x0000_t75');
114        $xmlWriter->writeAttribute('stroked', 'f');
115
116        $styleWriter->write();
117
118        $xmlWriter->startElement('v:imagedata');
119        $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
120        $xmlWriter->writeAttribute('o:title', '');
121        $xmlWriter->endElement(); // v:imagedata
122        $xmlWriter->endElement(); // v:shape
123        $xmlWriter->endElement(); // w:pict
124        $xmlWriter->endElement(); // w:r
125        if (!$this->withoutP) {
126            $xmlWriter->endElement(); // w:p
127        }
128    }
129}