Conditions | 34 |
Paths | 1422 |
Total Lines | 179 |
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 |
||
90 | public static function resizeMulti(\Imagick $source, array $boxSizes, array $options = []) : array |
||
91 | { |
||
92 | //algorithm inspired from http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html |
||
93 | //use of 2x2 binning is arguably the best quality one will get downsizing and is what lots of hardware does in |
||
94 | //the photography field, while being reasonably fast. Upsizing is more subjective but you can't get much |
||
95 | //better than bicubic which is what is used here. |
||
96 | |||
97 | $options = $options + self::DEFAULT_OPTIONS; |
||
98 | $color = $options['color']; |
||
99 | Util::ensure(true, is_string($color), InvalidArgumentException::class, ['$options["color"] was not a string']); |
||
100 | |||
101 | $upsize = $options['upsize']; |
||
102 | Util::ensure(true, is_bool($upsize), InvalidArgumentException::class, ['$options["upsize"] was not a bool']); |
||
103 | |||
104 | $bestfit = $options['bestfit']; |
||
105 | Util::ensure(true, is_bool($bestfit), InvalidArgumentException::class, ['$options["bestfit"] was not a bool']); |
||
106 | |||
107 | $blurBackground = $options['blurBackground']; |
||
108 | Util::ensure( |
||
109 | true, |
||
110 | is_bool($blurBackground), |
||
111 | InvalidArgumentException::class, |
||
112 | ['$options["blurBackground"] was not a bool'] |
||
113 | ); |
||
114 | |||
115 | $maxWidth = $options['maxWidth']; |
||
116 | Util::ensure(true, is_int($maxWidth), InvalidArgumentException::class, ['$options["maxWidth"] was not an int']); |
||
117 | |||
118 | $maxHeight = $options['maxHeight']; |
||
119 | Util::ensure( |
||
120 | true, |
||
121 | is_int($maxHeight), |
||
122 | InvalidArgumentException::class, |
||
123 | ['$options["maxHeight"] was not an int'] |
||
124 | ); |
||
125 | |||
126 | foreach ($boxSizes as $boxSizeKey => $boxSize) { |
||
127 | if (!isset($boxSize['width']) || !is_int($boxSize['width'])) { |
||
128 | throw new InvalidArgumentException('a width in a $boxSizes value was not an int'); |
||
129 | } |
||
130 | |||
131 | if (!isset($boxSize['height']) || !is_int($boxSize['height'])) { |
||
132 | throw new InvalidArgumentException('a height in a $boxSizes value was not an int'); |
||
133 | } |
||
134 | |||
135 | if ($boxSize['width'] > $maxWidth || $boxSize['width'] <= 0) { |
||
136 | throw new InvalidArgumentException('a $boxSizes width was not between 0 and $options["maxWidth"]'); |
||
137 | } |
||
138 | |||
139 | if ($boxSize['height'] > $maxHeight || $boxSize['height'] <= 0) { |
||
140 | throw new InvalidArgumentException('a $boxSizes height was not between 0 and $options["maxHeight"]'); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $results = []; |
||
145 | $cloneCache = []; |
||
146 | foreach ($boxSizes as $boxSizeKey => $boxSize) { |
||
147 | $boxWidth = $boxSize['width']; |
||
148 | $boxHeight = $boxSize['height']; |
||
149 | |||
150 | $clone = clone $source; |
||
151 | |||
152 | $orientation = $clone->getImageOrientation(); |
||
153 | switch ($orientation) { |
||
154 | case \Imagick::ORIENTATION_BOTTOMRIGHT: |
||
155 | $clone->rotateimage('#fff', 180); |
||
156 | $clone->stripImage(); |
||
157 | break; |
||
158 | case \Imagick::ORIENTATION_RIGHTTOP: |
||
159 | $clone->rotateimage('#fff', 90); |
||
160 | $clone->stripImage(); |
||
161 | break; |
||
162 | case \Imagick::ORIENTATION_LEFTBOTTOM: |
||
163 | $clone->rotateimage('#fff', -90); |
||
164 | $clone->stripImage(); |
||
165 | break; |
||
166 | } |
||
167 | |||
168 | $width = $clone->getImageWidth(); |
||
169 | $height = $clone->getImageHeight(); |
||
170 | |||
171 | //ratio over 1 is horizontal, under 1 is vertical |
||
172 | $boxRatio = $boxWidth / $boxHeight; |
||
173 | //height should be positive since I didnt find a way you could get zero into imagick |
||
174 | $originalRatio = $width / $height; |
||
175 | |||
176 | $targetWidth = null; |
||
|
|||
177 | $targetHeight = null; |
||
178 | $targetX = null; |
||
179 | $targetY = null; |
||
180 | if ($width < $boxWidth && $height < $boxHeight && !$upsize) { |
||
181 | $targetWidth = $width; |
||
182 | $targetHeight = $height; |
||
183 | $targetX = ($boxWidth - $width) / 2; |
||
184 | $targetY = ($boxHeight - $height) / 2; |
||
185 | } else { |
||
186 | //if box is more vertical than original |
||
187 | if ($boxRatio < $originalRatio) { |
||
188 | $targetWidth = $boxWidth; |
||
189 | $targetHeight = (int)((double)$boxWidth / $originalRatio); |
||
190 | $targetX = 0; |
||
191 | $targetY = ($boxHeight - $targetHeight) / 2; |
||
192 | } else { |
||
193 | $targetWidth = (int)((double)$boxHeight * $originalRatio); |
||
194 | $targetHeight = $boxHeight; |
||
195 | $targetX = ($boxWidth - $targetWidth) / 2; |
||
196 | $targetY = 0; |
||
197 | } |
||
198 | } |
||
199 | |||
200 | //do iterative downsize by halfs (2x2 binning is a common name) on dimensions that are bigger than target |
||
201 | //width and height |
||
202 | while (true) { |
||
203 | $widthReduced = false; |
||
204 | $widthIsHalf = false; |
||
205 | if ($width > $targetWidth) { |
||
206 | $width = (int)($width / 2); |
||
207 | $widthReduced = true; |
||
208 | $widthIsHalf = true; |
||
209 | if ($width < $targetWidth) { |
||
210 | $width = $targetWidth; |
||
211 | $widthIsHalf = false; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | $heightReduced = false; |
||
216 | $heightIsHalf = false; |
||
217 | if ($height > $targetHeight) { |
||
218 | $height = (int)($height / 2); |
||
219 | $heightReduced = true; |
||
220 | $heightIsHalf = true; |
||
221 | if ($height < $targetHeight) { |
||
222 | $height = $targetHeight; |
||
223 | $heightIsHalf = false; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | if (!$widthReduced && !$heightReduced) { |
||
228 | break; |
||
229 | } |
||
230 | |||
231 | $cacheKey = "{$width}x{$height}"; |
||
232 | if (isset($cloneCache[$cacheKey])) { |
||
233 | $clone = clone $cloneCache[$cacheKey]; |
||
234 | continue; |
||
235 | } |
||
236 | |||
237 | if ($clone->resizeImage($width, $height, \Imagick::FILTER_BOX, 1.0) !== true) { |
||
238 | //cumbersome to test |
||
239 | throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore |
||
240 | } |
||
241 | |||
242 | if ($widthIsHalf && $heightIsHalf) { |
||
243 | $cloneCache[$cacheKey] = clone $clone; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | if ($upsize && ($width < $targetWidth || $height < $targetHeight)) { |
||
248 | if ($clone->resizeImage($targetWidth, $targetHeight, \Imagick::FILTER_CUBIC, 1.0, $bestfit) !== true) { |
||
249 | //cumbersome to test |
||
250 | throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore |
||
251 | } |
||
252 | } |
||
253 | |||
254 | //put image in box |
||
255 | $canvas = self::getBackgroundCanvas($source, $color, $blurBackground, $boxWidth, $boxHeight); |
||
256 | if ($canvas->compositeImage($clone, \Imagick::COMPOSITE_ATOP, $targetX, $targetY) !== true) { |
||
257 | //cumbersome to test |
||
258 | throw new \Exception('Imagick::compositeImage() did not return true');//@codeCoverageIgnore |
||
259 | } |
||
260 | |||
261 | //reason we are not supporting the options in self::write() here is because format, and strip headers are |
||
262 | //only relevant once written Imagick::stripImage() doesnt even have an effect until written |
||
263 | //also the user can just call that function with the resultant $canvas |
||
264 | $results[$boxSizeKey] = $canvas; |
||
265 | } |
||
266 | |||
267 | return $results; |
||
268 | } |
||
269 | |||
414 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.