Test Failed
Push — master ( 398493...d4ef72 )
by Michael
11:04
created

phpthumb_ico::GD2ICOstring()   F

Complexity

Conditions 14
Paths 728

Size

Total Lines 103
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 70
nc 728
nop 1
dl 0
loc 103
rs 2.4369
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
//   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
13
class phpthumb_ico {
14
15
	public function GD2ICOstring(&$gd_image_array) {
16
		$ImageWidths  = array();
17
		$ImageHeights = array();
18
		$bpp          = array();
19
		$totalcolors  = array();
20
		$icXOR        = array();
21
		$icAND        = array();
22
		$icANDmask    = array();
23
		foreach ($gd_image_array as $key => $gd_image) {
24
25
			$ImageWidths[$key]  = imagesx($gd_image);
26
			$ImageHeights[$key] = imagesy($gd_image);
27
			$bpp[$key]          = imageistruecolor($gd_image) ? 32 : 24;
28
			$totalcolors[$key]  = imagecolorstotal($gd_image);
29
30
			$icXOR[$key] = '';
31
			for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
32
				for ($x = 0; $x < $ImageWidths[$key]; $x++) {
33
					$argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
34
					$a = round(255 * ((127 - $argb['alpha']) / 127));
35
					$r = $argb['red'];
36
					$g = $argb['green'];
37
					$b = $argb['blue'];
38
39
					if ($bpp[$key] == 32) {
40
						$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 $ascii 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

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