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/**
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\Word2007\Element;
20
21use PhpOffice\PhpWord\Element\Image as ImageElement;
22use PhpOffice\PhpWord\Shared\XMLWriter;
23use PhpOffice\PhpWord\Style\Font as FontStyle;
24use PhpOffice\PhpWord\Style\Frame as FrameStyle;
25use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
26use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter;
27
28/**
29 * Image element writer.
30 *
31 * @since 0.10.0
32 */
33class Image extends AbstractElement
34{
35    /**
36     * Write element.
37     */
38    public function write(): void
39    {
40        $xmlWriter = $this->getXmlWriter();
41        $element = $this->getElement();
42        if (!$element instanceof ImageElement) {
43            return;
44        }
45
46        if ($element->isWatermark()) {
47            $this->writeWatermark($xmlWriter, $element);
48        } else {
49            $this->writeImage($xmlWriter, $element);
50        }
51    }
52
53    /**
54     * Write image element.
55     */
56    private function writeImage(XMLWriter $xmlWriter, ImageElement $element): void
57    {
58        $rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
59        $style = $element->getStyle();
60        $styleWriter = new ImageStyleWriter($xmlWriter, $style);
61
62        if (!$this->withoutP) {
63            $xmlWriter->startElement('w:p');
64            $styleWriter->writeAlignment();
65        }
66        $this->writeCommentRangeStart();
67
68        $xmlWriter->startElement('w:r');
69
70        // Write position
71        $position = $style->getPosition();
72        if ($position && $style->getWrap() == FrameStyle::WRAP_INLINE) {
73            $fontStyle = new FontStyle('text');
74            $fontStyle->setPosition($position);
75            $fontStyleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
76            $fontStyleWriter->write();
77        }
78
79        $xmlWriter->startElement('w:pict');
80        $xmlWriter->startElement('v:shape');
81        $xmlWriter->writeAttribute('type', '#_x0000_t75');
82        $xmlWriter->writeAttribute('stroked', 'f');
83
84        $styleWriter->write();
85
86        $xmlWriter->startElement('v:imagedata');
87        $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
88        $xmlWriter->writeAttribute('o:title', '');
89        $xmlWriter->endElement(); // v:imagedata
90
91        $xmlWriter->endElement(); // v:shape
92        $xmlWriter->endElement(); // w:pict
93        $xmlWriter->endElement(); // w:r
94
95        $this->endElementP();
96    }
97
98    /**
99     * Write watermark element.
100     */
101    private function writeWatermark(XMLWriter $xmlWriter, ImageElement $element): void
102    {
103        $rId = $element->getRelationId();
104        $style = $element->getStyle();
105        $style->setPositioning('absolute');
106        $styleWriter = new ImageStyleWriter($xmlWriter, $style);
107
108        if (!$this->withoutP) {
109            $xmlWriter->startElement('w:p');
110        }
111        $xmlWriter->startElement('w:r');
112        $xmlWriter->startElement('w:pict');
113        $xmlWriter->startElement('v:shape');
114        $xmlWriter->writeAttribute('type', '#_x0000_t75');
115        $xmlWriter->writeAttribute('stroked', 'f');
116
117        $styleWriter->write();
118
119        $xmlWriter->startElement('v:imagedata');
120        $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
121        $xmlWriter->writeAttribute('o:title', '');
122        $xmlWriter->endElement(); // v:imagedata
123        $xmlWriter->endElement(); // v:shape
124        $xmlWriter->endElement(); // w:pict
125        $xmlWriter->endElement(); // w:r
126        if (!$this->withoutP) {
127            $xmlWriter->endElement(); // w:p
128        }
129    }
130}