Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Image
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
3 / 3
12
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%
29 / 29
100.00% covered (success)
100.00%
1 / 1
6
 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        if ($element->getAltText() != null) {
85            $xmlWriter->writeAttribute('alt', $element->getAltText());
86        }
87
88        $styleWriter->write();
89
90        $xmlWriter->startElement('v:imagedata');
91        $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
92        $xmlWriter->writeAttribute('o:title', '');
93        $xmlWriter->endElement(); // v:imagedata
94
95        $xmlWriter->endElement(); // v:shape
96        $xmlWriter->endElement(); // w:pict
97        $xmlWriter->endElement(); // w:r
98
99        $this->endElementP();
100    }
101
102    /**
103     * Write watermark element.
104     */
105    private function writeWatermark(XMLWriter $xmlWriter, ImageElement $element): void
106    {
107        $rId = $element->getRelationId();
108        $style = $element->getStyle();
109        $style->setPositioning('absolute');
110        $styleWriter = new ImageStyleWriter($xmlWriter, $style);
111
112        if (!$this->withoutP) {
113            $xmlWriter->startElement('w:p');
114        }
115        $xmlWriter->startElement('w:r');
116        $xmlWriter->startElement('w:pict');
117        $xmlWriter->startElement('v:shape');
118        $xmlWriter->writeAttribute('type', '#_x0000_t75');
119        $xmlWriter->writeAttribute('stroked', 'f');
120
121        $styleWriter->write();
122
123        $xmlWriter->startElement('v:imagedata');
124        $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
125        $xmlWriter->writeAttribute('o:title', '');
126        $xmlWriter->endElement(); // v:imagedata
127        $xmlWriter->endElement(); // v:shape
128        $xmlWriter->endElement(); // w:pict
129        $xmlWriter->endElement(); // w:r
130        if (!$this->withoutP) {
131            $xmlWriter->endElement(); // w:p
132        }
133    }
134}