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