phpthumb_ico::GD2ICOstring()   F
last analyzed

Complexity

Conditions 14
Paths 728

Size

Total Lines 101
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 14
eloc 70
nc 728
nop 1
dl 0
loc 101
rs 2.6367
c 4
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
//   phpThumb() by James Heinrich <[email protected]>   //
4
//        available at http://phpthumb.sourceforge.net      //
5
//         and/or https://github.com/JamesHeinrich/phpThumb //
6
//////////////////////////////////////////////////////////////
7
///                                                         //
8
// phpthumb.ico.php - .ICO output format functions          //
9
//                                                         ///
10
//////////////////////////////////////////////////////////////
11
12
class phpthumb_ico
13
{
14
    public function GD2ICOstring(&$gd_image_array)
15
    {
16
        $ImageWidths  = [];
17
        $ImageHeights = [];
18
        $bpp          = [];
19
        $totalcolors  = [];
20
        $icXOR        = [];
21
        $icAND        = [];
22
        $icANDmask    = [];
23
        foreach ($gd_image_array as $key => $gd_image) {
24
            $ImageWidths[$key]  = imagesx($gd_image);
25
            $ImageHeights[$key] = imagesy($gd_image);
26
            $bpp[$key]          = imageistruecolor($gd_image) ? 32 : 24;
27
            $totalcolors[$key]  = imagecolorstotal($gd_image);
28
29
            $icXOR[$key] = '';
30
            for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
31
                for ($x = 0; $x < $ImageWidths[$key]; $x++) {
32
                    $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
33
                    $a    = round(255 * ((127 - $argb['alpha']) / 127));
34
                    $r    = $argb['red'];
35
                    $g    = $argb['green'];
36
                    $b    = $argb['blue'];
37
38
                    if ($bpp[$key] == 32) {
39
                        $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
0 ignored issues
show
Bug introduced by
$a of type double is incompatible with the type integer expected by parameter $codepoint of chr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                        $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr(/** @scrutinizer ignore-type */ $a);
Loading history...
40
                    } elseif ($bpp[$key] == 24) {
41
                        $icXOR[$key] .= chr($b) . chr($g) . chr($r);
42
                    }
43
44
                    if ($a < 128) {
45
                        @$icANDmask[$key][$y] .= '1';
46
                    } else {
47
                        @$icANDmask[$key][$y] .= '0';
48
                    }
49
                }
50
                // mask bits are 32-bit aligned per scanline
51
                while (strlen($icANDmask[$key][$y]) % 32) {
52
                    $icANDmask[$key][$y] .= '0';
53
                }
54
            }
55
            $icAND[$key] = '';
56
            foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
57
                for ($i = 0, $iMax = strlen($scanlinemaskbits); $i < $iMax; $i += 8) {
58
                    $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
59
                }
60
            }
61
        }
62
63
        foreach ($gd_image_array as $key => $gd_image) {
64
            $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
65
66
            // BITMAPINFOHEADER - 40 bytes
67
            $BitmapInfoHeader[$key] = '';
68
            $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00";                              // DWORD  biSize;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $BitmapInfoHeader seems to be defined later in this foreach loop on line 67. Are you sure it is defined here?
Loading history...
69
            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);      // LONG   biWidth;
70
            // The biHeight member specifies the combined
71
            // height of the XOR and AND masks.
72
            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG   biHeight;
73
            $BitmapInfoHeader[$key] .= "\x01\x00";                                      // WORD   biPlanes;
74
            $BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00";                          // wBitCount;
75
            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                              // DWORD  biCompression;
76
            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);            // DWORD  biSizeImage;
77
            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                              // LONG   biXPelsPerMeter;
78
            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                              // LONG   biYPelsPerMeter;
79
            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                              // DWORD  biClrUsed;
80
            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                              // DWORD  biClrImportant;
81
        }
82
83
        $icondata = "\x00\x00";                                      // idReserved;   // Reserved (must be 0)
84
        $icondata .= "\x01\x00";                                      // idType;       // Resource Type (1 for icons)
85
        $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);  // idCount;      // How many images?
86
87
        $dwImageOffset = 6 + (count($gd_image_array) * 16);
88
        foreach ($gd_image_array as $key => $gd_image) {
89
            // ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
90
91
            $icondata .= chr($ImageWidths[$key]);                     // bWidth;          // Width, in pixels, of the image
92
            $icondata .= chr($ImageHeights[$key]);                    // bHeight;         // Height, in pixels, of the image
93
            $icondata .= chr($totalcolors[$key]);                     // bColorCount;     // Number of colors in image (0 if >=8bpp)
94
            $icondata .= "\x00";                                      // bReserved;       // Reserved ( must be 0)
95
96
            $icondata .= "\x01\x00";                                  // wPlanes;         // Color Planes
97
            $icondata .= chr($bpp[$key]) . "\x00";                      // wBitCount;       // Bits per pixel
98
99
            $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
100
            $icondata     .= phpthumb_functions::LittleEndian2String($dwBytesInRes, 4);       // dwBytesInRes;    // How many bytes in this resource?
101
102
            $icondata      .= phpthumb_functions::LittleEndian2String($dwImageOffset, 4);      // dwImageOffset;   // Where in the file is this image?
103
            $dwImageOffset += strlen($BitmapInfoHeader[$key]);
104
            $dwImageOffset += strlen($icXOR[$key]);
105
            $dwImageOffset += strlen($icAND[$key]);
106
        }
107
108
        foreach ($gd_image_array as $key => $gd_image) {
109
            $icondata .= $BitmapInfoHeader[$key];
110
            $icondata .= $icXOR[$key];
111
            $icondata .= $icAND[$key];
112
        }
113
114
        return $icondata;
115
    }
116
}
117