ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Drawing.php
Go to the documentation of this file.
1<?php
2
4
5use GdImage;
6
7class Drawing
8{
16 public static function pixelsToEMU($pValue)
17 {
18 return $pValue * 9525;
19 }
20
28 public static function EMUToPixels($pValue)
29 {
30 if ($pValue != 0) {
31 return (int) round($pValue / 9525);
32 }
33
34 return 0;
35 }
36
47 public static function pixelsToCellDimension($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont)
48 {
49 // Font name and size
50 $name = $pDefaultFont->getName();
51 $size = $pDefaultFont->getSize();
52
54 // Exact width can be determined
55 $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 return $colWidth;
63 }
64
73 public static function cellDimensionToPixels($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont)
74 {
75 // Font name and size
76 $name = $pDefaultFont->getName();
77 $size = $pDefaultFont->getSize();
78
80 // Exact width can be determined
81 $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 $colWidth = (int) round($colWidth);
90
91 return $colWidth;
92 }
93
101 public static function pixelsToPoints($pValue)
102 {
103 return $pValue * 0.75;
104 }
105
113 public static function pointsToPixels($pValue)
114 {
115 if ($pValue != 0) {
116 return (int) ceil($pValue / 0.75);
117 }
118
119 return 0;
120 }
121
129 public static function degreesToAngle($pValue)
130 {
131 return (int) round($pValue * 60000);
132 }
133
141 public static function angleToDegrees($pValue)
142 {
143 if ($pValue != 0) {
144 return (int) round($pValue / 60000);
145 }
146
147 return 0;
148 }
149
159 public static function imagecreatefrombmp($p_sFile)
160 {
161 // Load the image into a string
162 $file = fopen($p_sFile, 'rb');
163 $read = fread($file, 10);
164 while (!feof($file) && ($read != '')) {
165 $read .= fread($file, 1024);
166 }
167
168 $temp = unpack('H*', $read);
169 $hex = $temp[1];
170 $header = substr($hex, 0, 108);
171
172 // Process the header
173 // Structure: http://www.fastgraph.com/help/bmp_header_format.html
174 $width = 0;
175 $height = 0;
176 if (substr($header, 0, 4) == '424d') {
177 // Cut it in parts of 2 bytes
178 $header_parts = str_split($header, 2);
179
180 // Get the width 4 bytes
181 $width = hexdec($header_parts[19] . $header_parts[18]);
182
183 // Get the height 4 bytes
184 $height = hexdec($header_parts[23] . $header_parts[22]);
185
186 // Unset the header params
187 unset($header_parts);
188 }
189
190 // Define starting X and Y
191 $x = 0;
192 $y = 1;
193
194 // Create newimage
195 $image = imagecreatetruecolor($width, $height);
196
197 // Grab the body from the image
198 $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 $body_size = (strlen($body) / 2);
204 $header_size = ($width * $height);
205
206 // Use end-line padding? Only when needed
207 $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 for ($i = 0; $i < $body_size; $i += 3) {
212 // Calculate line-ending and padding
213 if ($x >= $width) {
214 // If padding needed, ignore image-padding
215 // Shift i to the ending of the current 32-bit-block
216 if ($usePadding) {
217 $i += $width % 4;
218 }
219
220 // Reset horizontal position
221 $x = 0;
222
223 // Raise the height-position (bottom-up)
224 ++$y;
225
226 // Reached the image-height? Break the for-loop
227 if ($y > $height) {
228 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 $i_pos = $i * 2;
235 $r = hexdec($body[$i_pos + 4] . $body[$i_pos + 5]);
236 $g = hexdec($body[$i_pos + 2] . $body[$i_pos + 3]);
237 $b = hexdec($body[$i_pos] . $body[$i_pos + 1]);
238
239 // Calculate and draw the pixel
240 $color = imagecolorallocate($image, $r, $g, $b);
241 imagesetpixel($image, $x, $height - $y, $color);
242
243 // Raise the horizontal position
244 ++$x;
245 }
246
247 // Unset the body / free the memory
248 unset($body);
249
250 // Return image-object
251 return $image;
252 }
253}
$size
Definition: RandomTest.php:84
An exception for terminatinating execution or to throw for unit testing.
static degreesToAngle($pValue)
Convert degrees to angle.
Definition: Drawing.php:129
static pointsToPixels($pValue)
Convert points to pixels.
Definition: Drawing.php:113
static pixelsToPoints($pValue)
Convert pixels to points.
Definition: Drawing.php:101
static angleToDegrees($pValue)
Convert angle to degrees.
Definition: Drawing.php:141
static pixelsToCellDimension($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont)
Convert pixels to column width.
Definition: Drawing.php:47
static imagecreatefrombmp($p_sFile)
Create a new image from file.
Definition: Drawing.php:159
static EMUToPixels($pValue)
Convert EMU to pixels.
Definition: Drawing.php:28
static cellDimensionToPixels($pValue, \PhpOffice\PhpSpreadsheet\Style\Font $pDefaultFont)
Convert column width from (intrinsic) Excel units to pixels.
Definition: Drawing.php:73
static pixelsToEMU($pValue)
Convert pixels to EMU.
Definition: Drawing.php:16
$x
Definition: complexTest.php:9
$i
Definition: disco.tpl.php:19
$y
Definition: example_007.php:83
$r
Definition: example_031.php:79