These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | *------------------------------------------------------------ |
||
4 | * BMP Image functions |
||
5 | *------------------------------------------------------------ |
||
6 | * By JPEXS |
||
7 | * |
||
8 | * This file is ISO-8859-1 encoded. If you intend to change it |
||
9 | * to UTF-8, take care that any string functions used are |
||
10 | * multibyte-aware. |
||
11 | */ |
||
12 | |||
13 | |||
14 | /* |
||
15 | *------------------------------------------------------------ |
||
16 | * ImageBMP |
||
17 | *------------------------------------------------------------ |
||
18 | * - Creates new BMP file |
||
19 | * |
||
20 | * Parameters: $img - Target image |
||
21 | * $file - Target file to store |
||
22 | * - if not specified, bmp is returned |
||
23 | * |
||
24 | * Returns: if $file specified - true if OK |
||
25 | if $file not specified - image data |
||
26 | */ |
||
27 | /** |
||
28 | * @param resource $img |
||
29 | * @param string $file |
||
30 | * @param int $RLE |
||
31 | * |
||
32 | * @return resource |
||
33 | */ |
||
34 | function imagebmp($img, $file = '', $RLE = 0) |
||
35 | { |
||
36 | $ColorCount = imagecolorstotal($img); |
||
37 | |||
38 | $Transparent = imagecolortransparent($img); |
||
39 | $IsTransparent = $Transparent !== -1; |
||
40 | |||
41 | |||
42 | if ($IsTransparent) { |
||
43 | $ColorCount--; |
||
44 | } |
||
45 | |||
46 | if ($ColorCount == 0) { |
||
47 | $ColorCount = 0; |
||
48 | $BitCount = 24; |
||
49 | }; |
||
50 | if (($ColorCount > 0) and ($ColorCount <= 2)) { |
||
51 | $ColorCount = 2; |
||
52 | $BitCount = 1; |
||
53 | }; |
||
54 | if (($ColorCount > 2) and ($ColorCount <= 16)) { |
||
55 | $ColorCount = 16; |
||
56 | $BitCount = 4; |
||
57 | }; |
||
58 | if (($ColorCount > 16) and ($ColorCount <= 256)) { |
||
59 | $ColorCount = 0; |
||
60 | $BitCount = 8; |
||
61 | }; |
||
62 | |||
63 | |||
64 | $Width = imagesx($img); |
||
65 | $Height = imagesy($img); |
||
66 | |||
67 | $Zbytek = (4 - ($Width / (8 / $BitCount)) % 4) % 4; |
||
0 ignored issues
–
show
|
|||
68 | |||
69 | if ($BitCount < 24) { |
||
70 | $palsize = pow(2, $BitCount) * 4; |
||
71 | } |
||
72 | |||
73 | $size = (floor($Width / (8 / $BitCount)) + $Zbytek) * $Height + 54; |
||
74 | $size += $palsize; |
||
0 ignored issues
–
show
The variable
$palsize does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
75 | $offset = 54 + $palsize; |
||
76 | |||
77 | // Bitmap File Header |
||
78 | $ret = 'BM'; // header (2b) |
||
79 | $ret .= int_to_dword($size); // size of file (4b) |
||
80 | $ret .= int_to_dword(0); // reserved (4b) |
||
81 | $ret .= int_to_dword($offset); // byte location in the file which is first byte of IMAGE (4b) |
||
82 | // Bitmap Info Header |
||
83 | $ret .= int_to_dword(40); // Size of BITMAPINFOHEADER (4b) |
||
84 | $ret .= int_to_dword($Width); // width of bitmap (4b) |
||
85 | $ret .= int_to_dword($Height); // height of bitmap (4b) |
||
86 | $ret .= int_to_word(1); // biPlanes = 1 (2b) |
||
87 | $ret .= int_to_word($BitCount); // biBitCount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 Mil)} (2b) |
||
88 | $ret .= int_to_dword($RLE); // RLE COMPRESSION (4b) |
||
89 | $ret .= int_to_dword(0); // width x height (4b) |
||
90 | $ret .= int_to_dword(0); // biXPelsPerMeter (4b) |
||
91 | $ret .= int_to_dword(0); // biYPelsPerMeter (4b) |
||
92 | $ret .= int_to_dword(0); // Number of palettes used (4b) |
||
93 | $ret .= int_to_dword(0); // Number of important colour (4b) |
||
94 | // image data |
||
95 | |||
96 | $CC = $ColorCount; |
||
97 | $sl1 = strlen($ret); |
||
0 ignored issues
–
show
$sl1 is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
98 | if ($CC == 0) { |
||
99 | $CC = 256; |
||
100 | } |
||
101 | if ($BitCount < 24) { |
||
102 | $ColorTotal = imagecolorstotal($img); |
||
103 | if ($IsTransparent) { |
||
104 | $ColorTotal--; |
||
105 | } |
||
106 | |||
107 | for ($p = 0; $p < $ColorTotal; $p++) { |
||
108 | $color = imagecolorsforindex($img, $p); |
||
109 | $ret .= inttobyte($color['blue']); |
||
110 | $ret .= inttobyte($color['green']); |
||
111 | $ret .= inttobyte($color['red']); |
||
112 | $ret .= inttobyte(0); //RESERVED |
||
113 | } |
||
114 | |||
115 | $CT = $ColorTotal; |
||
0 ignored issues
–
show
$CT is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
116 | for ($p = $ColorTotal; $p < $CC; $p++) { |
||
117 | $ret .= inttobyte(0); |
||
118 | $ret .= inttobyte(0); |
||
119 | $ret .= inttobyte(0); |
||
120 | $ret .= inttobyte(0); //RESERVED |
||
121 | } |
||
122 | } |
||
123 | |||
124 | |||
125 | if ($BitCount <= 8) { |
||
126 | for ($y = $Height - 1; $y >= 0; $y--) { |
||
127 | $bWrite = ''; |
||
128 | for ($x = 0; $x < $Width; $x++) { |
||
129 | $color = imagecolorat($img, $x, $y); |
||
130 | $bWrite .= decbinx($color, $BitCount); |
||
131 | if (strlen($bWrite) == 8) { |
||
132 | $retd .= inttobyte(bindec($bWrite)); |
||
0 ignored issues
–
show
The variable
$retd does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
133 | $bWrite = ''; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | if ((strlen($bWrite) < 8) && (strlen($bWrite) != 0)) { |
||
138 | $sl = strlen($bWrite); |
||
139 | for ($t = 0; $t < 8 - $sl; $t++) { |
||
140 | $sl .= '0'; |
||
141 | } |
||
142 | $retd .= inttobyte(bindec($bWrite)); |
||
143 | }; |
||
144 | for ($z = 0; $z < $Zbytek; $z++) { |
||
145 | $retd .= inttobyte(0); |
||
146 | } |
||
147 | }; |
||
148 | }; |
||
149 | |||
150 | if (($RLE == 1) && ($BitCount == 8)) { |
||
151 | for ($t = 0; $t < strlen($retd); $t += 4) { |
||
152 | if ($t != 0) { |
||
153 | if (($t) % $Width == 0) { |
||
154 | $ret .= chr(0) . chr(0); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | if (($t + 5) % $Width == 0) { |
||
159 | $ret .= chr(0) . chr(5) . substr($retd, $t, 5) . chr(0); |
||
160 | $t += 1; |
||
161 | } |
||
162 | if (($t + 6) % $Width == 0) { |
||
163 | $ret .= chr(0) . chr(6) . substr($retd, $t, 6); |
||
164 | $t += 2; |
||
165 | } else { |
||
166 | $ret .= chr(0) . chr(4) . substr($retd, $t, 4); |
||
167 | } |
||
168 | } |
||
169 | $ret .= chr(0) . chr(1); |
||
170 | } else { |
||
171 | $ret .= $retd; |
||
172 | } |
||
173 | |||
174 | |||
175 | if ($BitCount == 24) { |
||
176 | for ($z = 0; $z < $Zbytek; $z++) { |
||
177 | $Dopl .= chr(0); |
||
0 ignored issues
–
show
The variable
$Dopl does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
178 | } |
||
179 | |||
180 | for ($y = $Height - 1; $y >= 0; $y--) { |
||
181 | for ($x = 0; $x < $Width; $x++) { |
||
182 | $color = imagecolorsforindex($img, imagecolorat($img, $x, $y)); |
||
183 | $ret .= chr($color['blue']) . chr($color['green']) . chr($color['red']); |
||
184 | } |
||
185 | $ret .= $Dopl; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if ($file != '') { |
||
190 | $r = ($f = fopen($file, 'wb')); |
||
191 | $r = $r and fwrite($f, $ret); |
||
192 | $r = $r and fclose($f); |
||
193 | |||
194 | return $r; |
||
195 | } |
||
196 | echo $ret; |
||
197 | } |
||
198 | |||
199 | /* |
||
200 | *------------------------------------------------------------ |
||
201 | * ImageCreateFromBmp |
||
202 | *------------------------------------------------------------ |
||
203 | * - Reads image from a BMP file |
||
204 | * |
||
205 | * Parameters: $file - Target file to load |
||
206 | * |
||
207 | * Returns: Image ID |
||
208 | */ |
||
209 | /** |
||
210 | * @param $file |
||
211 | * |
||
212 | * @return resource |
||
213 | */ |
||
214 | function imagecreatefrombmp($file) |
||
215 | { |
||
216 | global $CurrentBit, $echoMode; |
||
217 | |||
218 | $f = fopen($file, 'r'); |
||
219 | $Header = fread($f, 2); |
||
220 | |||
221 | if ($Header == 'BM') { |
||
222 | $Size = freaddword($f); |
||
0 ignored issues
–
show
$Size is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
223 | $Reserved1 = freadword($f); |
||
0 ignored issues
–
show
$Reserved1 is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
224 | $Reserved2 = freadword($f); |
||
0 ignored issues
–
show
$Reserved2 is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
225 | $FirstByteOfImage = freaddword($f); |
||
0 ignored issues
–
show
$FirstByteOfImage is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
226 | |||
227 | $SizeBITMAPINFOHEADER = freaddword($f); |
||
0 ignored issues
–
show
$SizeBITMAPINFOHEADER is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
228 | $Width = freaddword($f); |
||
229 | $Height = freaddword($f); |
||
230 | $biPlanes = freadword($f); |
||
0 ignored issues
–
show
$biPlanes is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
231 | $biBitCount = freadword($f); |
||
232 | $RLECompression = freaddword($f); |
||
233 | $WidthxHeight = freaddword($f); |
||
0 ignored issues
–
show
$WidthxHeight is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
234 | $biXPelsPerMeter = freaddword($f); |
||
0 ignored issues
–
show
$biXPelsPerMeter is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
235 | $biYPelsPerMeter = freaddword($f); |
||
0 ignored issues
–
show
$biYPelsPerMeter is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
236 | $NumberOfPalettesUsed = freaddword($f); |
||
0 ignored issues
–
show
$NumberOfPalettesUsed is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
237 | $NumberOfImportantColors = freaddword($f); |
||
0 ignored issues
–
show
$NumberOfImportantColors is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
238 | |||
239 | if ($biBitCount < 24) { |
||
240 | $img = imagecreate($Width, $Height); |
||
241 | $Colors = pow(2, $biBitCount); |
||
242 | for ($p = 0; $p < $Colors; $p++) { |
||
243 | $B = freadbyte($f); |
||
244 | $G = freadbyte($f); |
||
245 | $R = freadbyte($f); |
||
246 | $Reserved = freadbyte($f); |
||
0 ignored issues
–
show
$Reserved is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
247 | $Palette[] = imagecolorallocate($img, $R, $G, $B); |
||
248 | }; |
||
249 | |||
250 | |||
251 | if ($RLECompression == 0) { |
||
252 | $Zbytek = (4 - ceil(($Width / (8 / $biBitCount))) % 4) % 4; |
||
253 | |||
254 | for ($y = $Height - 1; $y >= 0; $y--) { |
||
255 | $CurrentBit = 0; |
||
256 | for ($x = 0; $x < $Width; $x++) { |
||
257 | $C = freadbits($f, $biBitCount); |
||
258 | imagesetpixel($img, $x, $y, $Palette[$C]); |
||
0 ignored issues
–
show
The variable
$Palette does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
259 | } |
||
260 | if ($CurrentBit != 0) { |
||
261 | freadbyte($f); |
||
262 | } |
||
263 | for ($g = 0; $g < $Zbytek; $g++) { |
||
264 | freadbyte($f); |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | |||
270 | |||
271 | if ($RLECompression == 1) { //$BI_RLE8 |
||
272 | $y = $Height; |
||
273 | |||
274 | $pocetb = 0; |
||
275 | |||
276 | while (true) { |
||
277 | $y--; |
||
278 | $prefix = freadbyte($f); |
||
279 | $suffix = freadbyte($f); |
||
280 | $pocetb += 2; |
||
281 | |||
282 | $echoit = false; |
||
283 | |||
284 | if ($echoit) { |
||
285 | echo "Prefix: $prefix Suffix: $suffix<BR>"; |
||
286 | } |
||
287 | if (($prefix == 0) && ($suffix == 1)) { |
||
288 | break; |
||
289 | } |
||
290 | if (feof($f)) { |
||
291 | break; |
||
292 | } |
||
293 | |||
294 | while (!(($prefix == 0) && ($suffix == 0))) { |
||
295 | if ($prefix == 0) { |
||
296 | $pocet = $suffix; |
||
297 | $Data .= fread($f, $pocet); |
||
0 ignored issues
–
show
The variable
$Data does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
298 | $pocetb += $pocet; |
||
299 | if ($pocetb % 2 == 1) { |
||
300 | freadbyte($f); |
||
301 | $pocetb++; |
||
302 | } |
||
303 | } |
||
304 | if ($prefix > 0) { |
||
305 | $pocet = $prefix; |
||
306 | for ($r = 0; $r < $pocet; $r++) { |
||
307 | $Data .= chr($suffix); |
||
308 | } |
||
309 | } |
||
310 | $prefix = freadbyte($f); |
||
311 | $suffix = freadbyte($f); |
||
312 | $pocetb += 2; |
||
313 | if ($echoit) { |
||
314 | echo "Prefix: $prefix Suffix: $suffix<BR>"; |
||
315 | } |
||
316 | } |
||
317 | |||
318 | View Code Duplication | for ($x = 0; $x < strlen($Data); $x++) { |
|
319 | imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]); |
||
0 ignored issues
–
show
The variable
$img does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
320 | } |
||
321 | $Data = ''; |
||
322 | } |
||
323 | } |
||
324 | |||
325 | if ($RLECompression == 2) { //$BI_RLE4 |
||
326 | $y = $Height; |
||
327 | $pocetb = 0; |
||
328 | |||
329 | /*while(!feof($f)) |
||
330 | echo freadbyte($f)."_".freadbyte($f)."<BR>";*/ |
||
331 | while (true) { |
||
332 | //break; |
||
333 | $y--; |
||
334 | $prefix = freadbyte($f); |
||
335 | $suffix = freadbyte($f); |
||
336 | $pocetb += 2; |
||
337 | |||
338 | $echoit = false; |
||
339 | |||
340 | if ($echoit) { |
||
341 | echo "Prefix: $prefix Suffix: $suffix<BR>"; |
||
342 | } |
||
343 | if (($prefix == 0) and ($suffix == 1)) { |
||
344 | break; |
||
345 | } |
||
346 | if (feof($f)) { |
||
347 | break; |
||
348 | } |
||
349 | |||
350 | while (!(($prefix == 0) && ($suffix == 0))) { |
||
351 | if ($prefix == 0) { |
||
352 | $pocet = $suffix; |
||
353 | |||
354 | $CurrentBit = 0; |
||
355 | for ($h = 0; $h < $pocet; $h++) { |
||
356 | $Data .= chr(freadbits($f, 4)); |
||
357 | } |
||
358 | if ($CurrentBit != 0) { |
||
359 | freadbits($f, 4); |
||
360 | } |
||
361 | $pocetb += ceil(($pocet / 2)); |
||
362 | if ($pocetb % 2 == 1) { |
||
363 | freadbyte($f); |
||
364 | $pocetb++; |
||
365 | } |
||
366 | } |
||
367 | |||
368 | if ($prefix > 0) { |
||
369 | $pocet = $prefix; |
||
370 | $i = 0; |
||
371 | for ($r = 0; $r < $pocet; $r++) { |
||
372 | if ($i % 2 == 0) { |
||
373 | $Data .= chr($suffix % 16); |
||
374 | } else { |
||
375 | $Data .= chr(floor($suffix / 16)); |
||
376 | }; |
||
377 | $i++; |
||
378 | }; |
||
379 | }; |
||
380 | $prefix = freadbyte($f); |
||
381 | $suffix = freadbyte($f); |
||
382 | $pocetb += 2; |
||
383 | if ($echoit) { |
||
384 | echo "Prefix: $prefix Suffix: $suffix<BR>"; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | View Code Duplication | for ($x = 0; $x < strlen($Data); $x++) { |
|
389 | imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]); |
||
390 | } |
||
391 | $Data = ''; |
||
392 | } |
||
393 | } |
||
394 | |||
395 | |||
396 | if ($biBitCount == 24) { |
||
397 | $img = imagecreatetruecolor($Width, $Height); |
||
398 | $Zbytek = $Width % 4; |
||
399 | |||
400 | for ($y = $Height - 1; $y >= 0; $y--) { |
||
401 | for ($x = 0; $x < $Width; $x++) { |
||
402 | $B = freadbyte($f); |
||
403 | $G = freadbyte($f); |
||
404 | $R = freadbyte($f); |
||
405 | $color = imagecolorexact($img, $R, $G, $B); |
||
406 | if ($color === -1) { |
||
407 | $color = imagecolorallocate($img, $R, $G, $B); |
||
408 | } |
||
409 | imagesetpixel($img, $x, $y, $color); |
||
410 | } |
||
411 | for ($z = 0; $z < $Zbytek; $z++) { |
||
412 | freadbyte($f); |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | |||
417 | return $img; |
||
418 | } |
||
419 | |||
420 | fclose($f); |
||
421 | } |
||
422 | |||
423 | |||
424 | /* |
||
425 | * Helping functions: |
||
426 | *------------------------- |
||
427 | * |
||
428 | * freadbyte($file) - reads 1 byte from $file |
||
429 | * freadword($file) - reads 2 bytes (1 word) from $file |
||
430 | * freaddword($file) - reads 4 bytes (1 dword) from $file |
||
431 | * freadlngint($file) - same as freaddword($file) |
||
432 | * decbin8($d) - returns binary string of d zero filled to 8 |
||
433 | * RetBits($byte,$start,$len) - returns bits $start->$start+$len from $byte |
||
434 | * freadbits($file,$count) - reads next $count bits from $file |
||
435 | * RGBToHex($R,$G,$B) - convert $R, $G, $B to hex |
||
436 | * int_to_dword($n) - returns 4 byte representation of $n |
||
437 | * int_to_word($n) - returns 2 byte representation of $n |
||
438 | */ |
||
439 | |||
440 | /** |
||
441 | * @param $f |
||
442 | * |
||
443 | * @return int |
||
444 | */ |
||
445 | function freadbyte($f) |
||
446 | { |
||
447 | return ord(fread($f, 1)); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * @param $f |
||
452 | * |
||
453 | * @return int |
||
454 | */ |
||
455 | View Code Duplication | function freadword($f) |
|
456 | { |
||
457 | $b1 = freadbyte($f); |
||
458 | $b2 = freadbyte($f); |
||
459 | |||
460 | return $b2 * 256 + $b1; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param $f |
||
465 | * |
||
466 | * @return int |
||
467 | */ |
||
468 | function freadlngint($f) |
||
469 | { |
||
470 | return freaddword($f); |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * @param $f |
||
475 | * |
||
476 | * @return int |
||
477 | */ |
||
478 | View Code Duplication | function freaddword($f) |
|
479 | { |
||
480 | $b1 = freadword($f); |
||
481 | $b2 = freadword($f); |
||
482 | |||
483 | return $b2 * 65536 + $b1; |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * @param int $byte |
||
488 | * @param $start |
||
489 | * @param int $len |
||
490 | * |
||
491 | * @return number |
||
492 | */ |
||
493 | function RetBits($byte, $start, $len) |
||
494 | { |
||
495 | $bin = decbin8($byte); |
||
496 | $r = bindec(substr($bin, $start, $len)); |
||
497 | |||
498 | return $r; |
||
499 | } |
||
500 | |||
501 | $CurrentBit = 0; |
||
502 | /** |
||
503 | * @param resource $f |
||
504 | * @param int $count |
||
505 | * |
||
506 | * @return number |
||
507 | */ |
||
508 | function freadbits($f, $count) |
||
509 | { |
||
510 | global $CurrentBit, $SMode; |
||
511 | $Byte = freadbyte($f); |
||
512 | $LastCBit = $CurrentBit; |
||
513 | $CurrentBit += $count; |
||
514 | if ($CurrentBit == 8) { |
||
515 | $CurrentBit = 0; |
||
516 | } else { |
||
517 | fseek($f, ftell($f) - 1); |
||
518 | } |
||
519 | |||
520 | return RetBits($Byte, $LastCBit, $count); |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * @param $Red |
||
525 | * @param $Green |
||
526 | * @param $Blue |
||
527 | * |
||
528 | * @return string |
||
529 | */ |
||
530 | function RGBToHex($Red, $Green, $Blue) |
||
531 | { |
||
532 | $hRed = dechex($Red); |
||
533 | if (strlen($hRed) == 1) { |
||
534 | $hRed = "0$hRed"; |
||
535 | } |
||
536 | $hGreen = dechex($Green); |
||
537 | if (strlen($hGreen) == 1) { |
||
538 | $hGreen = "0$hGreen"; |
||
539 | } |
||
540 | $hBlue = dechex($Blue); |
||
541 | if (strlen($hBlue) == 1) { |
||
542 | $hBlue = "0$hBlue"; |
||
543 | } |
||
544 | |||
545 | return ($hRed . $hGreen . $hBlue); |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * @param $n |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | function int_to_dword($n) |
||
554 | { |
||
555 | return chr($n & 255) . chr(($n >> 8) & 255) . chr(($n >> 16) & 255) . chr(($n >> 24) & 255); |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * @param int $n |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | function int_to_word($n) |
||
564 | { |
||
565 | return chr($n & 255) . chr(($n >> 8) & 255); |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * @param int $d |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | function decbin8($d) |
||
574 | { |
||
575 | return decbinx($d, 8); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @param $d |
||
580 | * @param int $n |
||
581 | * |
||
582 | * @return string |
||
583 | */ |
||
584 | function decbinx($d, $n) |
||
585 | { |
||
586 | $bin = decbin($d); |
||
587 | $sbin = strlen($bin); |
||
588 | for ($j = 0; $j < $n - $sbin; $j++) { |
||
589 | $bin = "0$bin"; |
||
590 | } |
||
591 | |||
592 | return $bin; |
||
593 | } |
||
594 | |||
595 | /** |
||
596 | * @param $n |
||
597 | * |
||
598 | * @return string |
||
599 | */ |
||
600 | function inttobyte($n) |
||
601 | { |
||
602 | return chr($n); |
||
603 | } |
||
604 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: