Conditions | 8 |
Paths | 12 |
Total Lines | 94 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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 |
||
101 | public function CreateImageFromBmp($p_sFile) |
||
102 | { |
||
103 | // Load the image into a string |
||
104 | $file = fopen($p_sFile,"rb"); |
||
105 | $read = fread($file,10); |
||
106 | while(!feof($file)&&($read<>"")) |
||
107 | $read .= fread($file,1024); |
||
108 | |||
109 | $temp = unpack("H*",$read); |
||
110 | $hex = $temp[1]; |
||
111 | $header = substr($hex,0,108); |
||
112 | $width=null; |
||
113 | $height=null; |
||
114 | |||
115 | // Process the header |
||
116 | // Structure: http://www.fastgraph.com/help/bmp_header_format.html |
||
117 | if (substr($header,0,4)=="424d") |
||
118 | { |
||
119 | // Cut it in parts of 2 bytes |
||
120 | $header_parts = str_split($header,2); |
||
121 | |||
122 | // Get the width 4 bytes |
||
123 | $width = hexdec($header_parts[19].$header_parts[18]); |
||
124 | |||
125 | // Get the height 4 bytes |
||
126 | $height = hexdec($header_parts[23].$header_parts[22]); |
||
127 | |||
128 | // Unset the header params |
||
129 | unset($header_parts); |
||
130 | } |
||
131 | |||
132 | // Define starting X and Y |
||
133 | $x = 0; |
||
134 | $y = 1; |
||
135 | |||
136 | // Create newimage |
||
137 | $image = imagecreatetruecolor($width,$height); |
||
138 | |||
139 | // Grab the body from the image |
||
140 | $body = substr($hex,108); |
||
141 | |||
142 | // Calculate if padding at the end-line is needed |
||
143 | // Divided by two to keep overview. |
||
144 | // 1 byte = 2 HEX-chars |
||
145 | $body_size = (strlen($body)/2); |
||
146 | $header_size = ($width*$height); |
||
147 | |||
148 | // Use end-line padding? Only when needed |
||
149 | $usePadding = ($body_size>($header_size*3)+4); |
||
150 | |||
151 | // Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption |
||
152 | // Calculate the next DWORD-position in the body |
||
153 | for ($i=0;$i<$body_size;$i+=3) |
||
154 | { |
||
155 | // Calculate line-ending and padding |
||
156 | if ($x>=$width) |
||
157 | { |
||
158 | // If padding needed, ignore image-padding |
||
159 | // Shift i to the ending of the current 32-bit-block |
||
160 | if ($usePadding) |
||
161 | $i += $width%4; |
||
162 | |||
163 | // Reset horizontal position |
||
164 | $x = 0; |
||
165 | |||
166 | // Raise the height-position (bottom-up) |
||
167 | $y++; |
||
168 | |||
169 | // Reached the image-height? Break the for-loop |
||
170 | if ($y>$height) |
||
171 | break; |
||
172 | } |
||
173 | |||
174 | // Calculation of the RGB-pixel (defined as BGR in image-data) |
||
175 | // Define $i_pos as absolute position in the body |
||
176 | $i_pos = $i*2; |
||
177 | $r = hexdec($body[$i_pos+4].$body[$i_pos+5]); |
||
178 | $g = hexdec($body[$i_pos+2].$body[$i_pos+3]); |
||
179 | $b = hexdec($body[$i_pos].$body[$i_pos+1]); |
||
180 | |||
181 | // Calculate and draw the pixel |
||
182 | $color = imagecolorallocate($image,$r,$g,$b); |
||
183 | imagesetpixel($image,$x,$height-$y,$color); |
||
184 | |||
185 | // Raise the horizontal position |
||
186 | $x++; |
||
187 | } |
||
188 | |||
189 | // Unset the body / free the memory |
||
190 | unset($body); |
||
191 | |||
192 | // Return image-object |
||
193 | return $image; |
||
194 | } |
||
195 | |||
210 | } |