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