| Total Complexity | 83 |
| Total Lines | 638 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ImagemagickDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImagemagickDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class ImagemagickDriver extends AbstractDriver |
||
| 31 | {
|
||
| 32 | /** |
||
| 33 | * ImagemagickDriver::__construct |
||
| 34 | * |
||
| 35 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||
| 36 | */ |
||
| 37 | public function __construct() |
||
| 38 | {
|
||
| 39 | if ( ! class_exists('Imagick', false)) {
|
||
| 40 | throw new BadPhpExtensionCallException('IMAGE_E_PHP_EXTENSION', 0, ['imagick']);
|
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | // ------------------------------------------------------------------------ |
||
| 45 | |||
| 46 | /** |
||
| 47 | * ImagemagickDriver::__destruct |
||
| 48 | */ |
||
| 49 | public function __destruct() |
||
| 50 | {
|
||
| 51 | if (is_object($this->sourceImageResource)) {
|
||
| 52 | $this->sourceImageResource->destroy(); |
||
| 53 | } |
||
| 54 | |||
| 55 | if (is_object($this->resampleImageResource)) {
|
||
| 56 | $this->resampleImageResource->destroy(); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | // ------------------------------------------------------------------------ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * ImagemagickDriver::createFromString |
||
| 64 | * |
||
| 65 | * Create an image resource from image string. |
||
| 66 | * |
||
| 67 | * @param string $imageString Image string. |
||
| 68 | * |
||
| 69 | * @return bool |
||
| 70 | * @throws \ImagickException |
||
| 71 | */ |
||
| 72 | public function createFromString($imageString) |
||
| 73 | {
|
||
| 74 | $this->sourceImageResource = new \Imagick(); |
||
| 75 | |||
| 76 | try {
|
||
| 77 | |||
| 78 | return $this->sourceImageResource->readImageBlob($imageString); |
||
|
|
|||
| 79 | |||
| 80 | } catch (\ImagickException $e) {
|
||
| 81 | |||
| 82 | $this->errors[ $e->getCode() ] = $e->getMessage(); |
||
| 83 | |||
| 84 | } |
||
| 85 | |||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | // ------------------------------------------------------------------------ |
||
| 90 | |||
| 91 | /** |
||
| 92 | * ImagemagickDriver::rotate |
||
| 93 | * |
||
| 94 | * Rotate an image with a given angle. |
||
| 95 | * |
||
| 96 | * @param float $degrees Image rotation degrees. |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public function rotate($degrees) |
||
| 101 | {
|
||
| 102 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 103 | |||
| 104 | return $resampleImageResource->rotateImage('#000000', $degrees);
|
||
| 105 | } |
||
| 106 | |||
| 107 | // ------------------------------------------------------------------------ |
||
| 108 | |||
| 109 | /** |
||
| 110 | * ImagemagickDriver::flip |
||
| 111 | * |
||
| 112 | * Flip an image with a given axis. |
||
| 113 | * |
||
| 114 | * @param int $axis Flip axis. |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function flip($axis) |
||
| 119 | {
|
||
| 120 | $gdAxis = [ |
||
| 121 | 1 => IMG_FLIP_HORIZONTAL, |
||
| 122 | 2 => IMG_FLIP_VERTICAL, |
||
| 123 | 3 => IMG_FLIP_BOTH, |
||
| 124 | ]; |
||
| 125 | |||
| 126 | if (array_key_exists($axis, $gdAxis)) {
|
||
| 127 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 128 | |||
| 129 | try {
|
||
| 130 | |||
| 131 | switch ($axis) {
|
||
| 132 | case 1: |
||
| 133 | $resampleImageResource->flopImage(); |
||
| 134 | break; |
||
| 135 | case 2: |
||
| 136 | $resampleImageResource->flipImage(); |
||
| 137 | break; |
||
| 138 | case 3: |
||
| 139 | $resampleImageResource->flopImage(); |
||
| 140 | $resampleImageResource->flipImage(); |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | |||
| 144 | return true; |
||
| 145 | |||
| 146 | } catch (\ImagickException $e) {
|
||
| 147 | $this->errors[ $e->getCode() ] = $e->getMessage(); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return false; |
||
| 152 | } |
||
| 153 | |||
| 154 | // ------------------------------------------------------------------------ |
||
| 155 | |||
| 156 | /** |
||
| 157 | * ImagemagickDriver::resize |
||
| 158 | * |
||
| 159 | * Resize an image using the given new width and height. |
||
| 160 | * |
||
| 161 | * @param bool $crop Perform auto crop or not |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function resize($crop = false) |
||
| 166 | {
|
||
| 167 | if ($crop) {
|
||
| 168 | return $this->resizeCrop(); |
||
| 169 | } else {
|
||
| 170 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||
| 171 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||
| 172 | |||
| 173 | if (($sourceDimension->getWidth() <= $resampleDimension->getWidth()) && ($sourceDimension->getHeight() <= $resampleDimension->getHeight())) {
|
||
| 174 | return true; |
||
| 175 | } //no resizing needed |
||
| 176 | |||
| 177 | //try max width first... |
||
| 178 | $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth(); |
||
| 179 | $resizeWidth = $resampleDimension->getWidth(); |
||
| 180 | $resizeHeight = $sourceDimension->getHeight() * $resizeRatio; |
||
| 181 | |||
| 182 | //if that didn't work |
||
| 183 | if ($resizeHeight > $resampleDimension->getHeight()) {
|
||
| 184 | $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight(); |
||
| 185 | $resizeHeight = $resampleDimension->getHeight(); |
||
| 186 | $resizeWidth = $sourceDimension->getWidth() * $resizeRatio; |
||
| 187 | } |
||
| 188 | |||
| 189 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 190 | |||
| 191 | return $resampleImageResource->resizeImage( |
||
| 192 | $resizeWidth, |
||
| 193 | $resizeHeight, |
||
| 194 | \Imagick::FILTER_CATROM, 0.9, true); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | // ------------------------------------------------------------------------ |
||
| 199 | |||
| 200 | /** |
||
| 201 | * ImagemagickDriver::resizeCrop |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function resizeCrop() |
||
| 206 | {
|
||
| 207 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||
| 208 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||
| 209 | |||
| 210 | //try max width first... |
||
| 211 | $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth(); |
||
| 212 | $resizeWidth = $resampleDimension->getWidth(); |
||
| 213 | $resizeHeight = $sourceDimension->getHeight() * $resizeRatio; |
||
| 214 | |||
| 215 | //if that didn't work |
||
| 216 | if ($resizeHeight > $resampleDimension->getHeight()) {
|
||
| 217 | $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight(); |
||
| 218 | $resizeHeight = $resampleDimension->getHeight(); |
||
| 219 | $resizeWidth = $sourceDimension->getWidth() * $resizeRatio; |
||
| 220 | } |
||
| 221 | |||
| 222 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 223 | |||
| 224 | if ($resampleDimension->getOrientation() === 'SQUARE') {
|
||
| 225 | if ($resampleImageResource->resizeImage($resizeWidth, $resizeHeight, \Imagick::FILTER_LANCZOS, 0.9, |
||
| 226 | true) |
||
| 227 | ) {
|
||
| 228 | $resampleAxis = new Dimension\Axis( |
||
| 229 | ($resizeWidth - $resampleDimension->getWidth()) / 2, |
||
| 230 | ($resizeHeight - $resampleDimension->getWidth()) / 2 |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | } else {
|
||
| 234 | switch ($resampleDimension->getFocus()) {
|
||
| 235 | default: |
||
| 236 | case 'CENTER': |
||
| 237 | $resampleAxis = new Dimension\Axis( |
||
| 238 | ($sourceDimension->getWidth() / 2) - ($resizeWidth / 2), |
||
| 239 | ($sourceDimension->getHeight() / 2) - ($resizeHeight / 2) |
||
| 240 | ); |
||
| 241 | break; |
||
| 242 | case 'NORTH': |
||
| 243 | $resampleAxis = new Dimension\Axis( |
||
| 244 | ($sourceDimension->getWidth() - $resizeWidth) / 2, |
||
| 245 | 0 |
||
| 246 | ); |
||
| 247 | break; |
||
| 248 | case 'NORTHWEST': |
||
| 249 | $resampleAxis = new Dimension\Axis( |
||
| 250 | 0, |
||
| 251 | 0 |
||
| 252 | ); |
||
| 253 | break; |
||
| 254 | case 'NORTHEAST': |
||
| 255 | $resampleAxis = new Dimension\Axis( |
||
| 256 | $sourceDimension->getWidth() - $resizeWidth, |
||
| 257 | 0 |
||
| 258 | ); |
||
| 259 | break; |
||
| 260 | case 'SOUTH': |
||
| 261 | $resampleAxis = new Dimension\Axis( |
||
| 262 | ($sourceDimension->getWidth() - $resizeWidth) / 2, |
||
| 263 | $sourceDimension->getHeight() - $resizeHeight |
||
| 264 | ); |
||
| 265 | break; |
||
| 266 | case 'SOUTHWEST': |
||
| 267 | $resampleAxis = new Dimension\Axis( |
||
| 268 | 0, |
||
| 269 | $sourceDimension->getHeight() - $resizeHeight |
||
| 270 | ); |
||
| 271 | break; |
||
| 272 | case 'SOUTHEAST': |
||
| 273 | $resampleAxis = new Dimension\Axis( |
||
| 274 | $sourceDimension->getWidth() - $resizeWidth, |
||
| 275 | $sourceDimension->getHeight() - $resizeHeight |
||
| 276 | ); |
||
| 277 | break; |
||
| 278 | case 'WEST': |
||
| 279 | $resampleAxis = new Dimension\Axis( |
||
| 280 | 0, |
||
| 281 | ($sourceDimension->getHeight() - $resizeHeight) / 2 |
||
| 282 | ); |
||
| 283 | break; |
||
| 284 | case 'EAST': |
||
| 285 | $resampleAxis = new Dimension\Axis( |
||
| 286 | $sourceDimension->getWidth() - $resizeWidth, |
||
| 287 | ($sourceDimension->getHeight() - $resizeHeight) / 2 |
||
| 288 | ); |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | |||
| 292 | if ( ! $resampleImageResource->resizeImage( |
||
| 293 | $sourceDimension->getWidth(), |
||
| 294 | $sourceDimension->getHeight(), |
||
| 295 | \Imagick::FILTER_CATROM, 0.9, true) |
||
| 296 | ) {
|
||
| 297 | return false; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | if (isset($resampleAxis)) {
|
||
| 302 | return $this->crop($resampleDimension->withAxis($resampleAxis)); |
||
| 303 | } |
||
| 304 | |||
| 305 | return false; |
||
| 306 | } |
||
| 307 | |||
| 308 | // ------------------------------------------------------------------------ |
||
| 309 | |||
| 310 | /** |
||
| 311 | * ImagemagickDriver::crop |
||
| 312 | * |
||
| 313 | * Crop an image. |
||
| 314 | * |
||
| 315 | * @param \O2System\Image\Dimension $dimension |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function crop(Dimension $dimension) |
||
| 320 | {
|
||
| 321 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 322 | |||
| 323 | try {
|
||
| 324 | return $resampleImageResource->cropImage( |
||
| 325 | $dimension->getWidth(), |
||
| 326 | $dimension->getHeight(), |
||
| 327 | $dimension->getAxis()->getX(), |
||
| 328 | $dimension->getAxis()->getY() |
||
| 329 | ); |
||
| 330 | } catch (\ImagickException $e) {
|
||
| 331 | $this->errors[ $e->getCode() ] = $e->getMessage(); |
||
| 332 | } |
||
| 333 | |||
| 334 | return false; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * ImagemagickDriver::watermark |
||
| 339 | * |
||
| 340 | * Watermark an image. |
||
| 341 | * |
||
| 342 | * @param \O2System\Image\Abstracts\AbstractWatermark $watermark |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function watermark(AbstractWatermark $watermark) |
||
| 507 | } |
||
| 508 | |||
| 509 | // ------------------------------------------------------------------------ |
||
| 510 | |||
| 511 | /** |
||
| 512 | * ImagemagickDriver::createFromSource |
||
| 513 | * |
||
| 514 | * Create an image resource from source file. |
||
| 515 | * |
||
| 516 | * @return static |
||
| 517 | */ |
||
| 518 | public function createFromSource() |
||
| 521 | } |
||
| 522 | |||
| 523 | // ------------------------------------------------------------------------ |
||
| 524 | |||
| 525 | /** |
||
| 526 | * ImagemagickDriver::scale |
||
| 527 | * |
||
| 528 | * Scale an image with a given scale. |
||
| 529 | * |
||
| 530 | * @return bool |
||
| 531 | */ |
||
| 532 | public function scale() |
||
| 533 | {
|
||
| 534 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||
| 535 | |||
| 536 | $resampleImageResource =& $this->getResampleImageResource(); |
||
| 537 | |||
| 538 | try {
|
||
| 539 | return $resampleImageResource->scaleImage($resampleDimension->getWidth(), $resampleDimension->getHeight(), |
||
| 540 | true); |
||
| 541 | } catch (\ImagickException $e) {
|
||
| 542 | $this->errors[ $e->getCode() ] = $e->getMessage(); |
||
| 543 | } |
||
| 544 | |||
| 545 | return false; |
||
| 546 | } |
||
| 547 | |||
| 548 | // ------------------------------------------------------------------------ |
||
| 549 | |||
| 550 | /** |
||
| 551 | * ImagemagickDriver::display |
||
| 552 | * |
||
| 553 | * Display an image. |
||
| 554 | * |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | public function display($quality = 100, $mime = null) |
||
| 581 | } |
||
| 582 | |||
| 583 | // ------------------------------------------------------------------------ |
||
| 584 | |||
| 585 | /** |
||
| 586 | * ImagemagickDriver::blob |
||
| 587 | * |
||
| 588 | * Returns image string blob. |
||
| 589 | * |
||
| 590 | * @return string |
||
| 591 | */ |
||
| 592 | public function blob($quality = 100, $mime = null) |
||
| 617 | } |
||
| 618 | |||
| 619 | // ------------------------------------------------------------------------ |
||
| 620 | |||
| 621 | /** |
||
| 622 | * ImagemagickDriver::save |
||
| 623 | * |
||
| 624 | * Save an image. |
||
| 625 | * |
||
| 626 | * @param string $imageTargetFilePath |
||
| 627 | * @param int $quality |
||
| 628 | * |
||
| 629 | * @return bool |
||
| 630 | */ |
||
| 631 | public function save($imageTargetFilePath, $quality = 100) |
||
| 668 | } |
||
| 669 | } |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: