-
Notifications
You must be signed in to change notification settings - Fork 58
/
Drawing.php
292 lines (258 loc) · 6.1 KB
/
Drawing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* This file is part of PHPOffice Common
*
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/Common
*
* @copyright 2009-2016 PHPOffice Common contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\Common;
class Drawing
{
public const DPI_96 = 96;
/**
* Convert pixels to EMU
*
* @param float $pValue Value in pixels
*
* @return float
*/
public static function pixelsToEmu(float $pValue = 0): float
{
return round($pValue * 9525);
}
/**
* Convert EMU to pixels
*
* @param int $pValue Value in EMU
*
* @return int
*/
public static function emuToPixels(int $pValue = 0): int
{
if ($pValue == 0) {
return 0;
}
return (int) round($pValue / 9525);
}
/**
* Convert pixels to points
*
* @param int $pValue Value in pixels
*
* @return float
*/
public static function pixelsToPoints(int $pValue = 0): float
{
return $pValue * 0.75;
}
/**
* Convert points width to centimeters
*
* @param float $pValue Value in points
*
* @return float
*/
public static function pointsToCentimeters(float $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return (($pValue / 0.75) / self::DPI_96) * 2.54;
}
/**
* Convert centimeters width to points
*
* @param float $pValue Value in centimeters
*
* @return float
*/
public static function centimetersToPoints(float $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return ($pValue / 2.54) * self::DPI_96 * 0.75;
}
/**
* Convert points width to pixels
*
* @param float $pValue Value in points
*
* @return float
*/
public static function pointsToPixels(float $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return $pValue / 0.75;
}
/**
* Convert pixels to centimeters
*
* @param int $pValue Value in pixels
*
* @return float
*/
public static function pixelsToCentimeters(int $pValue = 0): float
{
// return $pValue * 0.028;
return ($pValue / self::DPI_96) * 2.54;
}
/**
* Convert centimeters width to pixels
*
* @param float $pValue Value in centimeters
*
* @return int
*/
public static function centimetersToPixels(float $pValue = 0): int
{
if ($pValue == 0) {
return 0;
}
return (int) round(($pValue / 2.54) * self::DPI_96);
}
/**
* Convert degrees to angle
*
* @param int $pValue Degrees
*
* @return int
*/
public static function degreesToAngle(int $pValue = 0): int
{
return (int) round($pValue * 60000);
}
/**
* Convert angle to degrees
*
* @param int $pValue Angle
*
* @return float
*/
public static function angleToDegrees(int $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return round($pValue / 60000);
}
/**
* Convert centimeters width to twips
*
* @param int $pValue
*
* @return float
*/
public static function centimetersToTwips(int $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return $pValue * 566.928;
}
/**
* Convert twips width to centimeters
*
* @param int $pValue
*
* @return float
*/
public static function twipsToCentimeters(int $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return $pValue / 566.928;
}
/**
* Convert inches width to twips
*
* @param int $pValue
*
* @return int
*/
public static function inchesToTwips(int $pValue = 0): int
{
if ($pValue == 0) {
return 0;
}
return $pValue * 1440;
}
/**
* Convert twips width to inches
*
* @param int $pValue
*
* @return float
*/
public static function twipsToInches(int $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return $pValue / 1440;
}
/**
* Convert twips width to pixels
*
* @param int $pValue
*
* @return float
*/
public static function twipsToPixels(int $pValue = 0): float
{
if ($pValue == 0) {
return 0;
}
return round($pValue / 15);
}
/**
* Convert points to emu
*
* @param float $pValue
*
* @return int
*/
public static function pointsToEmu(float $pValue = 0): int
{
if ($pValue == 0) {
return 0;
}
return (int) round(($pValue / 0.75) / 9525);
}
/**
* Convert HTML hexadecimal to RGB
*
* @param string $pValue HTML Color in hexadecimal
*
* @return array<int, int>|null Value in RGB
*/
public static function htmlToRGB(string $pValue): ?array
{
if ($pValue[0] == '#') {
$pValue = substr($pValue, 1);
}
if (strlen($pValue) == 6) {
list($colorR, $colorG, $colorB) = [$pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]];
} elseif (strlen($pValue) == 3) {
list($colorR, $colorG, $colorB) = [$pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]];
} else {
return null;
}
$colorR = hexdec($colorR);
$colorG = hexdec($colorG);
$colorB = hexdec($colorB);
return [$colorR, $colorG, $colorB];
}
}