Conditions | 8 |
Paths | 12 |
Total Lines | 93 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Tests | 39 |
CRAP Score | 8 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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); |
|
2 ignored issues
–
show
|
|||
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); |
|
3 ignored issues
–
show
|
|||
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 | } |
||
254 |