Completed
Push — next ( d59298...80f341 )
by Thomas
06:20 queued 01:49
created

imagebmp.inc.php ➔ imagebmpCustom()   F

Complexity

Conditions 37
Paths 15360

Size

Total Lines 164

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 37
nc 15360
nop 3
dl 0
loc 164
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

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 imagebmpCustom($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
Bug introduced by
The variable $BitCount 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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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
Bug introduced by
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
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
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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
Bug introduced by
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
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
Bug introduced by
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
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
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
223
        $Reserved1 = freadword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
224
        $Reserved2 = freadword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
        $FirstByteOfImage = freaddword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
226
227
        $SizeBITMAPINFOHEADER = freaddword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
228
        $Width = freaddword($f);
229
        $Height = freaddword($f);
230
        $biPlanes = freadword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
231
        $biBitCount = freadword($f);
232
        $RLECompression = freaddword($f);
233
        $WidthxHeight = freaddword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
234
        $biXPelsPerMeter = freaddword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
235
        $biYPelsPerMeter = freaddword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
236
        $NumberOfPalettesUsed = freaddword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
237
        $NumberOfImportantColors = freaddword($f);
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
247
                $Palette[] = imagecolorallocate($img, $R, $G, $B);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$Palette was never initialized. Although not strictly required by PHP, it is generally a good practice to add $Palette = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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
Bug introduced by
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
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
Bug introduced by
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
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
Bug introduced by
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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
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)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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