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