Conditions | 19 |
Paths | 262 |
Total Lines | 119 |
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 |
||
80 | public function getImage() |
||
81 | { |
||
82 | // decompress image data if required |
||
83 | switch ($this->infoArray['compressionType']) { |
||
84 | // case 2:, case 3: zip not supported yet.. |
||
85 | case 1: |
||
86 | // packed bits |
||
87 | $this->infoArray['scanLinesByteCounts'] = []; |
||
88 | for ($i = 0; $i < ($this->infoArray['rows'] * $this->infoArray['channels']); $i++) { |
||
89 | $this->infoArray['scanLinesByteCounts'][] = $this->_getInteger(2); |
||
90 | } |
||
91 | $this->tempFileName = tempnam(realpath('/tmp'), 'decompressedImageData'); |
||
92 | $tfp = fopen($this->tempFileName, 'wb'); |
||
93 | foreach ($this->infoArray['scanLinesByteCounts'] as $scanLinesByteCount) { |
||
94 | fwrite($tfp, $this->_getPackedBitsDecoded(fread($this->fp, $scanLinesByteCount))); |
||
95 | } |
||
96 | fclose($tfp); |
||
97 | fclose($this->fp); |
||
98 | $this->fp = fopen($this->tempFileName, 'rb'); |
||
99 | default: |
||
100 | // continue with current file handle; |
||
101 | break; |
||
102 | } |
||
103 | |||
104 | // let's write pixel by pixel.... |
||
105 | $image = imagecreatetruecolor($this->infoArray['columns'], $this->infoArray['rows']); |
||
106 | |||
107 | for ($rowPointer = 0; ($rowPointer < $this->infoArray['rows']); $rowPointer++) { |
||
108 | for ($columnPointer = 0; ($columnPointer < $this->infoArray['columns']); $columnPointer++) { |
||
109 | /* The color mode of the file. Supported values are: Bitmap=0; |
||
110 | Grayscale=1; Indexed=2; RGB=3; CMYK=4; Multichannel=7; |
||
111 | Duotone=8; Lab=9. |
||
112 | */ |
||
113 | switch ($this->infoArray['colorMode']) { |
||
114 | case 2: // indexed... info should be able to extract from color mode data section. not implemented yet, so is grayscale |
||
115 | exit; |
||
116 | break; |
||
117 | case 0: |
||
118 | // bit by bit |
||
119 | if (0 == $columnPointer) { |
||
120 | $bitPointer = 0; |
||
121 | } |
||
122 | if (0 == $bitPointer) { |
||
123 | $currentByteBits = str_pad(base_convert(bin2hex(fread($this->fp, 1)), 16, 2), 8, '0', STR_PAD_LEFT); |
||
124 | } |
||
125 | $r = $g = $b = (('1' == $currentByteBits[$bitPointer]) ? 0 : 255); |
||
126 | $bitPointer++; |
||
127 | if (8 == $bitPointer) { |
||
128 | $bitPointer = 0; |
||
129 | } |
||
130 | break; |
||
131 | |||
132 | case 1: |
||
133 | case 8: // 8 is indexed with 1 color..., so grayscale |
||
134 | $r = $g = $b = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
135 | break; |
||
136 | |||
137 | case 4: // CMYK |
||
138 | $c = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
139 | $currentPointerPos = ftell($this->fp); |
||
140 | fseek($this->fp, $this->colorBytesLength - 1, SEEK_CUR); |
||
141 | $m = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
142 | fseek($this->fp, $this->colorBytesLength - 1, SEEK_CUR); |
||
143 | $y = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
144 | fseek($this->fp, $this->colorBytesLength - 1, SEEK_CUR); |
||
145 | $k = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
146 | fseek($this->fp, $currentPointerPos); |
||
147 | $r = round(($c * $k) / (pow(2, $this->infoArray['colorDepth']) - 1)); |
||
148 | $g = round(($m * $k) / (pow(2, $this->infoArray['colorDepth']) - 1)); |
||
149 | $b = round(($y * $k) / (pow(2, $this->infoArray['colorDepth']) - 1)); |
||
150 | |||
151 | break; |
||
152 | |||
153 | case 9: // hunter Lab |
||
154 | // i still need an understandable lab2rgb convert algorithm... if you have one, please let me know! |
||
155 | $l = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
156 | $currentPointerPos = ftell($this->fp); |
||
157 | fseek($this->fp, $this->colorBytesLength - 1, SEEK_CUR); |
||
158 | $a = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
159 | fseek($this->fp, $this->colorBytesLength - 1, SEEK_CUR); |
||
160 | $b = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
161 | fseek($this->fp, $currentPointerPos); |
||
162 | |||
163 | $r = $l; |
||
164 | $g = $a; |
||
165 | $b = $b; |
||
166 | |||
167 | break; |
||
168 | default: |
||
169 | $r = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
170 | $currentPointerPos = ftell($this->fp); |
||
171 | fseek($this->fp, $this->colorBytesLength - 1, SEEK_CUR); |
||
172 | $g = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
173 | fseek($this->fp, $this->colorBytesLength - 1, SEEK_CUR); |
||
174 | $b = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']); |
||
175 | fseek($this->fp, $currentPointerPos); |
||
176 | break; |
||
177 | } |
||
178 | |||
179 | if ((2 == $this->infoArray['oneColorChannelPixelBytes'])) { |
||
180 | $r = $r >> 8; |
||
181 | $g = $g >> 8; |
||
182 | $b = $b >> 8; |
||
183 | } elseif ((4 == $this->infoArray['oneColorChannelPixelBytes'])) { |
||
184 | $r = $r >> 24; |
||
185 | $g = $g >> 24; |
||
186 | $b = $b >> 24; |
||
187 | } |
||
188 | |||
189 | $pixelColor = imagecolorallocate($image, $r, $g, $b); |
||
190 | imagesetpixel($image, $columnPointer, $rowPointer, $pixelColor); |
||
191 | } |
||
192 | } |
||
193 | fclose($this->fp); |
||
194 | if (isset($this->tempFileName)) { |
||
195 | unlink($this->tempFileName); |
||
196 | } |
||
197 | return $image; |
||
198 | } |
||
199 | |||
329 |