1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared; |
4
|
|
|
|
5
|
|
|
use GdImage; |
6
|
|
|
|
7
|
|
|
class Drawing |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Convert pixels to EMU. |
11
|
|
|
* |
12
|
|
|
* @param int $pValue Value in pixels |
13
|
|
|
* |
14
|
|
|
* @return int Value in EMU |
15
|
|
|
*/ |
16
|
29 |
|
public static function pixelsToEMU($pValue) |
17
|
|
|
{ |
18
|
29 |
|
return round($pValue * 9525); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Convert EMU to pixels. |
23
|
|
|
* |
24
|
|
|
* @param int $pValue Value in EMU |
25
|
|
|
* |
26
|
|
|
* @return int Value in pixels |
27
|
|
|
*/ |
28
|
12 |
|
public static function EMUToPixels($pValue) |
29
|
|
|
{ |
30
|
12 |
|
if ($pValue != 0) { |
31
|
12 |
|
return round($pValue / 9525); |
32
|
|
|
} |
33
|
|
|
|
34
|
12 |
|
return 0; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Convert pixels to column width. Exact algorithm not known. |
39
|
|
|
* By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875 |
40
|
|
|
* This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional. |
41
|
|
|
* |
42
|
|
|
* @param int $pValue Value in pixels |
43
|
|
|
* @param \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont Default font of the workbook |
44
|
|
|
* |
45
|
|
|
* @return int Value in cell dimension |
46
|
|
|
*/ |
47
|
20 |
|
public static function pixelsToCellDimension($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont) |
48
|
|
|
{ |
49
|
|
|
// Font name and size |
50
|
20 |
|
$name = $pDefaultFont->getName(); |
51
|
20 |
|
$size = $pDefaultFont->getSize(); |
52
|
|
|
|
53
|
20 |
|
if (isset(Font::$defaultColumnWidths[$name][$size])) { |
54
|
|
|
// Exact width can be determined |
55
|
20 |
|
$colWidth = $pValue * Font::$defaultColumnWidths[$name][$size]['width'] / Font::$defaultColumnWidths[$name][$size]['px']; |
56
|
|
|
} else { |
57
|
|
|
// We don't have data for this particular font and size, use approximation by |
58
|
|
|
// extrapolating from Calibri 11 |
59
|
|
|
$colWidth = $pValue * 11 * Font::$defaultColumnWidths['Calibri'][11]['width'] / Font::$defaultColumnWidths['Calibri'][11]['px'] / $size; |
60
|
|
|
} |
61
|
|
|
|
62
|
20 |
|
return $colWidth; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Convert column width from (intrinsic) Excel units to pixels. |
67
|
|
|
* |
68
|
|
|
* @param float $pValue Value in cell dimension |
69
|
|
|
* @param \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont Default font of the workbook |
70
|
|
|
* |
71
|
|
|
* @return int Value in pixels |
72
|
|
|
*/ |
73
|
22 |
|
public static function cellDimensionToPixels($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont) |
74
|
|
|
{ |
75
|
|
|
// Font name and size |
76
|
22 |
|
$name = $pDefaultFont->getName(); |
77
|
22 |
|
$size = $pDefaultFont->getSize(); |
78
|
|
|
|
79
|
22 |
|
if (isset(Font::$defaultColumnWidths[$name][$size])) { |
80
|
|
|
// Exact width can be determined |
81
|
22 |
|
$colWidth = $pValue * Font::$defaultColumnWidths[$name][$size]['px'] / Font::$defaultColumnWidths[$name][$size]['width']; |
82
|
|
|
} else { |
83
|
|
|
// We don't have data for this particular font and size, use approximation by |
84
|
|
|
// extrapolating from Calibri 11 |
85
|
|
|
$colWidth = $pValue * $size * Font::$defaultColumnWidths['Calibri'][11]['px'] / Font::$defaultColumnWidths['Calibri'][11]['width'] / 11; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Round pixels to closest integer |
89
|
22 |
|
$colWidth = (int) round($colWidth); |
90
|
|
|
|
91
|
22 |
|
return $colWidth; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Convert pixels to points. |
96
|
|
|
* |
97
|
|
|
* @param int $pValue Value in pixels |
98
|
|
|
* |
99
|
|
|
* @return float Value in points |
100
|
|
|
*/ |
101
|
12 |
|
public static function pixelsToPoints($pValue) |
102
|
|
|
{ |
103
|
12 |
|
return $pValue * 0.75; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Convert points to pixels. |
108
|
|
|
* |
109
|
|
|
* @param int $pValue Value in points |
110
|
|
|
* |
111
|
|
|
* @return int Value in pixels |
112
|
|
|
*/ |
113
|
3 |
|
public static function pointsToPixels($pValue) |
114
|
|
|
{ |
115
|
3 |
|
if ($pValue != 0) { |
116
|
3 |
|
return (int) ceil($pValue / 0.75); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Convert degrees to angle. |
124
|
|
|
* |
125
|
|
|
* @param int $pValue Degrees |
126
|
|
|
* |
127
|
|
|
* @return int Angle |
128
|
|
|
*/ |
129
|
15 |
|
public static function degreesToAngle($pValue) |
130
|
|
|
{ |
131
|
15 |
|
return (int) round($pValue * 60000); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Convert angle to degrees. |
136
|
|
|
* |
137
|
|
|
* @param int $pValue Angle |
138
|
|
|
* |
139
|
|
|
* @return int Degrees |
140
|
|
|
*/ |
141
|
9 |
|
public static function angleToDegrees($pValue) |
142
|
|
|
{ |
143
|
9 |
|
if ($pValue != 0) { |
144
|
2 |
|
return round($pValue / 60000); |
145
|
|
|
} |
146
|
|
|
|
147
|
9 |
|
return 0; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Create a new image from file. By alexander at alexauto dot nl. |
152
|
|
|
* |
153
|
|
|
* @see http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214 |
154
|
|
|
* |
155
|
|
|
* @param string $p_sFile Path to Windows DIB (BMP) image |
156
|
|
|
* |
157
|
|
|
* @return GdImage|resource |
158
|
|
|
*/ |
159
|
1 |
|
public static function imagecreatefrombmp($p_sFile) |
160
|
|
|
{ |
161
|
|
|
// Load the image into a string |
162
|
1 |
|
$file = fopen($p_sFile, 'rb'); |
163
|
1 |
|
$read = fread($file, 10); |
164
|
1 |
|
while (!feof($file) && ($read != '')) { |
165
|
1 |
|
$read .= fread($file, 1024); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
$temp = unpack('H*', $read); |
169
|
1 |
|
$hex = $temp[1]; |
170
|
1 |
|
$header = substr($hex, 0, 108); |
171
|
|
|
|
172
|
|
|
// Process the header |
173
|
|
|
// Structure: http://www.fastgraph.com/help/bmp_header_format.html |
174
|
1 |
|
$width = 0; |
175
|
1 |
|
$height = 0; |
176
|
1 |
|
if (substr($header, 0, 4) == '424d') { |
177
|
|
|
// Cut it in parts of 2 bytes |
178
|
1 |
|
$header_parts = str_split($header, 2); |
179
|
|
|
|
180
|
|
|
// Get the width 4 bytes |
181
|
1 |
|
$width = hexdec($header_parts[19] . $header_parts[18]); |
182
|
|
|
|
183
|
|
|
// Get the height 4 bytes |
184
|
1 |
|
$height = hexdec($header_parts[23] . $header_parts[22]); |
185
|
|
|
|
186
|
|
|
// Unset the header params |
187
|
1 |
|
unset($header_parts); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// Define starting X and Y |
191
|
1 |
|
$x = 0; |
192
|
1 |
|
$y = 1; |
193
|
|
|
|
194
|
|
|
// Create newimage |
195
|
1 |
|
$image = imagecreatetruecolor($width, $height); |
|
|
|
|
196
|
|
|
|
197
|
|
|
// Grab the body from the image |
198
|
1 |
|
$body = substr($hex, 108); |
199
|
|
|
|
200
|
|
|
// Calculate if padding at the end-line is needed |
201
|
|
|
// Divided by two to keep overview. |
202
|
|
|
// 1 byte = 2 HEX-chars |
203
|
1 |
|
$body_size = (strlen($body) / 2); |
204
|
1 |
|
$header_size = ($width * $height); |
205
|
|
|
|
206
|
|
|
// Use end-line padding? Only when needed |
207
|
1 |
|
$usePadding = ($body_size > ($header_size * 3) + 4); |
208
|
|
|
|
209
|
|
|
// Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption |
210
|
|
|
// Calculate the next DWORD-position in the body |
211
|
1 |
|
for ($i = 0; $i < $body_size; $i += 3) { |
212
|
|
|
// Calculate line-ending and padding |
213
|
1 |
|
if ($x >= $width) { |
214
|
|
|
// If padding needed, ignore image-padding |
215
|
|
|
// Shift i to the ending of the current 32-bit-block |
216
|
1 |
|
if ($usePadding) { |
217
|
1 |
|
$i += $width % 4; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// Reset horizontal position |
221
|
1 |
|
$x = 0; |
222
|
|
|
|
223
|
|
|
// Raise the height-position (bottom-up) |
224
|
1 |
|
++$y; |
225
|
|
|
|
226
|
|
|
// Reached the image-height? Break the for-loop |
227
|
1 |
|
if ($y > $height) { |
228
|
1 |
|
break; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// Calculation of the RGB-pixel (defined as BGR in image-data) |
233
|
|
|
// Define $i_pos as absolute position in the body |
234
|
1 |
|
$i_pos = $i * 2; |
235
|
1 |
|
$r = hexdec($body[$i_pos + 4] . $body[$i_pos + 5]); |
236
|
1 |
|
$g = hexdec($body[$i_pos + 2] . $body[$i_pos + 3]); |
237
|
1 |
|
$b = hexdec($body[$i_pos] . $body[$i_pos + 1]); |
238
|
|
|
|
239
|
|
|
// Calculate and draw the pixel |
240
|
1 |
|
$color = imagecolorallocate($image, $r, $g, $b); |
|
|
|
|
241
|
1 |
|
imagesetpixel($image, $x, $height - $y, $color); |
|
|
|
|
242
|
|
|
|
243
|
|
|
// Raise the horizontal position |
244
|
1 |
|
++$x; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// Unset the body / free the memory |
248
|
1 |
|
unset($body); |
249
|
|
|
|
250
|
|
|
// Return image-object |
251
|
1 |
|
return $image; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|