Conditions | 13 |
Paths | 16 |
Total Lines | 73 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
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 |
||
105 | public static function imagecreatefromstring($data) |
||
106 | { |
||
107 | //read header |
||
108 | $pos = 0; |
||
109 | $header = substr($data, 0, 54); |
||
110 | $pos = 54; |
||
111 | |||
112 | if (strlen($header) < 54) { |
||
113 | return false; |
||
114 | } |
||
115 | |||
116 | $header = unpack( 'c2identifier/Vfile_size/Vreserved/Vbitmap_data/Vheader_size/' . |
||
117 | 'Vwidth/Vheight/vplanes/vbits_per_pixel/Vcompression/Vdata_size/'. |
||
118 | 'Vh_resolution/Vv_resolution/Vcolors/Vimportant_colors', $header); |
||
119 | |||
120 | if ($header['identifier1'] != 66 or $header['identifier2'] != 77) { |
||
121 | return false; |
||
122 | } |
||
123 | |||
124 | if (!in_array($header['bits_per_pixel'], array(24, 32, 8, 4, 1))) { |
||
125 | return false; |
||
126 | } |
||
127 | |||
128 | $bps = $header['bits_per_pixel']; //bits per pixel |
||
129 | $wid2 = ceil(($bps/8 * $header['width']) / 4) * 4; |
||
130 | $colors = $header['colors']; |
||
131 | // The absolute height value is necessary because ImageHeight can be negative |
||
132 | // If Height is a positive number, then the image is a "bottom-up" bitmap with the origin in the lower-left corner. |
||
133 | // If Height is a negative number, then the image is a "top-down" bitmap with the origin in the upper-left corner. |
||
134 | $header['height'] = abs($header['height']); |
||
135 | |||
136 | $wid = $header['width']; |
||
137 | $hei = $header['height']; |
||
138 | |||
139 | $img = imagecreatetruecolor($header['width'], $header['height']); |
||
140 | if (!$img) { |
||
141 | return false; // Invalid width or height |
||
142 | } |
||
143 | //read palette |
||
144 | $palette = []; |
||
145 | |||
146 | if ($bps < 9) { |
||
147 | // A color table is mandatory for color depths <= 8 |
||
148 | if (isset($header['colors']) && $header['colors'] > 0) { |
||
149 | // The number of entries in the palette is either 2n |
||
150 | // or a smaller number specified in the header |
||
151 | $colors = min($header['colors'], $colors); |
||
152 | } |
||
153 | for ($i = 0; $i < $colors; $i++) { |
||
154 | $palette[] = self::undword(substr($data, $pos, 4)); |
||
155 | $pos += 4; |
||
156 | } |
||
157 | } else { |
||
158 | if ($bps == 32) { |
||
159 | imagealphablending($img, false); |
||
160 | imagesavealpha($img, true); |
||
161 | } |
||
162 | |||
163 | $palette = array(); |
||
164 | } |
||
165 | |||
166 | //read pixels |
||
167 | for ($y = $hei-1; $y >= 0; $y--) { |
||
168 | $row = substr($data, $pos, $wid2); |
||
169 | $pos += $wid2; |
||
170 | $pixels = self::str_split2($row, $bps, $palette); |
||
171 | |||
172 | for ($x = 0; $x < $wid; $x++) { |
||
173 | self::makepixel($img, $x, $y, $pixels[$x], $bps); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | return $img; |
||
178 | } |
||
271 |