Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
15 / 15
CRAP
100.00% covered (success)
100.00%
1 / 1
Drawing
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
15 / 15
28
100.00% covered (success)
100.00%
1 / 1
 pixelsToEmu
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 emuToPixels
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 pixelsToPoints
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 pointsToCentimeters
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 pointsToPixels
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 pixelsToCentimeters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 centimetersToPixels
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 degreesToAngle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 angleToDegrees
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 centimetersToTwips
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 twipsToCentimeters
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 inchesToTwips
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 twipsToInches
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 twipsToPixels
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 htmlToRGB
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
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\Shared;
19
20/**
21 * Drawing.
22 */
23class Drawing
24{
25    const DPI_96 = 96;
26
27    /**
28     * Convert pixels to EMU.
29     *
30     * @param  int $pValue Value in pixels
31     *
32     * @return int
33     */
34    public static function pixelsToEmu($pValue = 0)
35    {
36        return round($pValue * 9525);
37    }
38
39    /**
40     * Convert EMU to pixels.
41     *
42     * @param  int $pValue Value in EMU
43     *
44     * @return int
45     */
46    public static function emuToPixels($pValue = 0)
47    {
48        if ($pValue == 0) {
49            return 0;
50        }
51
52        return round($pValue / 9525);
53    }
54
55    /**
56     * Convert pixels to points.
57     *
58     * @param  int $pValue Value in pixels
59     *
60     * @return float
61     */
62    public static function pixelsToPoints($pValue = 0)
63    {
64        return $pValue * 0.67777777;
65    }
66
67    /**
68     * Convert points width to centimeters.
69     *
70     * @param  int $pValue Value in points
71     *
72     * @return float
73     */
74    public static function pointsToCentimeters($pValue = 0)
75    {
76        if ($pValue == 0) {
77            return 0;
78        }
79
80        return (($pValue * 1.333333333) / self::DPI_96) * 2.54;
81    }
82
83    /**
84     * Convert points width to pixels.
85     *
86     * @param  int $pValue Value in points
87     *
88     * @return float
89     */
90    public static function pointsToPixels($pValue = 0)
91    {
92        if ($pValue == 0) {
93            return 0;
94        }
95
96        return $pValue * 1.333333333;
97    }
98
99    /**
100     * Convert pixels to centimeters.
101     *
102     * @param  int $pValue Value in pixels
103     *
104     * @return float
105     */
106    public static function pixelsToCentimeters($pValue = 0)
107    {
108        //return $pValue * 0.028;
109        return ($pValue / self::DPI_96) * 2.54;
110    }
111
112    /**
113     * Convert centimeters width to pixels.
114     *
115     * @param  int $pValue Value in centimeters
116     *
117     * @return float
118     */
119    public static function centimetersToPixels($pValue = 0)
120    {
121        if ($pValue == 0) {
122            return 0;
123        }
124
125        return ($pValue / 2.54) * self::DPI_96;
126    }
127
128    /**
129     * Convert degrees to angle.
130     *
131     * @param  int $pValue Degrees
132     *
133     * @return int
134     */
135    public static function degreesToAngle($pValue = 0)
136    {
137        return (int) round($pValue * 60000);
138    }
139
140    /**
141     * Convert angle to degrees.
142     *
143     * @param  int $pValue Angle
144     *
145     * @return int
146     */
147    public static function angleToDegrees($pValue = 0)
148    {
149        if ($pValue == 0) {
150            return 0;
151        }
152
153        return round($pValue / 60000);
154    }
155
156    /**
157     * Convert centimeters width to twips.
158     *
159     * @param int $pValue
160     *
161     * @return float
162     */
163    public static function centimetersToTwips($pValue = 0)
164    {
165        if ($pValue == 0) {
166            return 0;
167        }
168
169        return $pValue * 566.928;
170    }
171
172    /**
173     * Convert twips width to centimeters.
174     *
175     * @param int $pValue
176     *
177     * @return float
178     */
179    public static function twipsToCentimeters($pValue = 0)
180    {
181        if ($pValue == 0) {
182            return 0;
183        }
184
185        return $pValue / 566.928;
186    }
187
188    /**
189     * Convert inches width to twips.
190     *
191     * @param int $pValue
192     *
193     * @return float
194     */
195    public static function inchesToTwips($pValue = 0)
196    {
197        if ($pValue == 0) {
198            return 0;
199        }
200
201        return $pValue * 1440;
202    }
203
204    /**
205     * Convert twips width to inches.
206     *
207     * @param int $pValue
208     *
209     * @return float
210     */
211    public static function twipsToInches($pValue = 0)
212    {
213        if ($pValue == 0) {
214            return 0;
215        }
216
217        return $pValue / 1440;
218    }
219
220    /**
221     * Convert twips width to pixels.
222     *
223     * @param int $pValue
224     *
225     * @return float
226     */
227    public static function twipsToPixels($pValue = 0)
228    {
229        if ($pValue == 0) {
230            return 0;
231        }
232
233        return round($pValue / 15.873984);
234    }
235
236    /**
237     * Convert HTML hexadecimal to RGB.
238     *
239     * @param string $pValue HTML Color in hexadecimal
240     *
241     * @return array|false Value in RGB
242     */
243    public static function htmlToRGB($pValue)
244    {
245        if ($pValue[0] == '#') {
246            $pValue = substr($pValue, 1);
247        }
248
249        if (strlen($pValue) == 6) {
250            [$colorR, $colorG, $colorB] = [$pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]];
251        } elseif (strlen($pValue) == 3) {
252            [$colorR, $colorG, $colorB] = [$pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]];
253        } else {
254            return false;
255        }
256
257        $colorR = hexdec($colorR);
258        $colorG = hexdec($colorG);
259        $colorB = hexdec($colorB);
260
261        return [$colorR, $colorG, $colorB];
262    }
263}