| Total Complexity | 45 |
| Total Lines | 328 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Image_Transform_Driver_Imagick2 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 Image_Transform_Driver_Imagick2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Image_Transform_Driver_Imagick2 extends Image_Transform |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * Handler of the imagick image ressource |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | public $imageHandle = null; |
||
| 56 | /** |
||
| 57 | * Handler of the image ressource before |
||
| 58 | * the last transformation |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | public $oldImage; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @see __construct() |
||
| 65 | */ |
||
| 66 | public function Image_Transform_Driver_Imagick2() |
||
| 69 | } |
||
| 70 | |||
| 71 | // End Image_Transform_Driver_Imagick2 |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @see http://www.imagemagick.org/www/formats.html |
||
| 75 | */ |
||
| 76 | public function __construct() |
||
| 77 | { |
||
| 78 | if (PEAR::loadExtension('imagick')) { |
||
| 79 | require_once __DIR__ . '/Image/Transform/Driver/Imagick/ImageTypes.php'; |
||
| 80 | } else { |
||
| 81 | $this->isError(PEAR::raiseError('Couldn\'t find the imagick extension.', IMAGE_TRANSFORM_ERROR_UNSUPPORTED)); |
||
|
|
|||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Loads an image |
||
| 87 | * |
||
| 88 | * @param string $image filename |
||
| 89 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 90 | * @access public |
||
| 91 | */ |
||
| 92 | public function load($image) |
||
| 93 | { |
||
| 94 | if (!($this->imageHandle = imagick_readimage($image))) { |
||
| 95 | $this->free(); |
||
| 96 | |||
| 97 | return $this->raiseError('Couldn\'t load image.', IMAGE_TRANSFORM_ERROR_IO); |
||
| 98 | } |
||
| 99 | if (imagick_iserror($this->imageHandle)) { |
||
| 100 | return $this->raiseError('Couldn\'t load image.', IMAGE_TRANSFORM_ERROR_IO); |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->image = $image; |
||
| 104 | $result = $this->_get_image_details($image); |
||
| 105 | if (PEAR::isError($result)) { |
||
| 106 | return $result; |
||
| 107 | } |
||
| 108 | |||
| 109 | return true; |
||
| 110 | } |
||
| 111 | |||
| 112 | // End load |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Resize Action |
||
| 116 | * |
||
| 117 | * @param int $new_x New width |
||
| 118 | * @param int $new_y New height |
||
| 119 | * @param mixed $options Optional parameters |
||
| 120 | * |
||
| 121 | * @return bool|PEAR_Error TRUE or PEAR_Error object on error |
||
| 122 | * @access protected |
||
| 123 | */ |
||
| 124 | public function _resize($new_x, $new_y, $options = null) |
||
| 125 | { |
||
| 126 | if (!imagick_resize($this->imageHandle, $new_x, $new_y, IMAGICK_FILTER_UNKNOWN, 1, '!')) { |
||
| 127 | return $this->raiseError('Couldn\'t resize image.', IMAGE_TRANSFORM_ERROR_FAILED); |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->new_x = $new_x; |
||
| 131 | $this->new_y = $new_y; |
||
| 132 | |||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | // End resize |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Rotates the current image |
||
| 140 | * Note: color mask are currently not supported |
||
| 141 | * |
||
| 142 | * @param $angle |
||
| 143 | * @param null $options |
||
| 144 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 145 | * @access public |
||
| 146 | */ |
||
| 147 | public function rotate($angle, $options = null) |
||
| 148 | { |
||
| 149 | if (0 == ($angle % 360)) { |
||
| 150 | return true; |
||
| 151 | } |
||
| 152 | if (!imagick_rotate($this->imageHandle, $angle)) { |
||
| 153 | return $this->raiseError('Cannot create a new imagick image for the rotation.', IMAGE_TRANSFORM_ERROR_FAILED); |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->new_x = imagick_getwidth($this->imageHandle); |
||
| 157 | $this->new_y = imagick_getheight($this->imageHandle); |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | // End rotate |
||
| 163 | |||
| 164 | /** |
||
| 165 | * addText |
||
| 166 | * |
||
| 167 | * @param mixed $params |
||
| 168 | * |
||
| 169 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 170 | * @access public |
||
| 171 | */ |
||
| 172 | public function addText($params) |
||
| 200 | } |
||
| 201 | |||
| 202 | // End addText |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Saves the image to a file |
||
| 206 | * |
||
| 207 | * @param string $filename the name of the file to write to |
||
| 208 | * @param string $type |
||
| 209 | * @param null $quality |
||
| 210 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 211 | * @access public |
||
| 212 | */ |
||
| 213 | public function save($filename, $type = '', $quality = null) |
||
| 214 | { |
||
| 215 | $options = is_array($quality) ? $quality : []; |
||
| 216 | if (is_numeric($quality)) { |
||
| 217 | $options['quality'] = $quality; |
||
| 218 | } |
||
| 219 | $quality = $this->_getOption('quality', $options, 75); |
||
| 220 | imagick_setcompressionquality($this->imageHandle, $quality); |
||
| 221 | |||
| 222 | if ($type && strcasecmp($type, $this->type) |
||
| 223 | && !imagick_convert($this->imageHandle, $type)) { |
||
| 224 | return $this->raiseError('Couldn\'t save image to file (conversion failed).', IMAGE_TRANSFORM_ERROR_FAILED); |
||
| 225 | } |
||
| 226 | |||
| 227 | if (!imagick_write($this->imageHandle, $filename)) { |
||
| 228 | return $this->raiseError('Couldn\'t save image to file.', IMAGE_TRANSFORM_ERROR_IO); |
||
| 229 | } |
||
| 230 | |||
| 231 | if (!$this->keep_settings_on_save) { |
||
| 232 | $this->free(); |
||
| 233 | } |
||
| 234 | |||
| 235 | return true; |
||
| 236 | } |
||
| 237 | |||
| 238 | // End save |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Displays image without saving and lose changes |
||
| 242 | * |
||
| 243 | * This method adds the Content-type HTTP header |
||
| 244 | * |
||
| 245 | * @param mixed $type |
||
| 246 | * @param null|mixed $quality |
||
| 247 | * |
||
| 248 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 249 | * @access public |
||
| 250 | */ |
||
| 251 | public function display($type = '', $quality = null) |
||
| 252 | { |
||
| 253 | $options = is_array($quality) ? $quality : []; |
||
| 254 | if (is_numeric($quality)) { |
||
| 255 | $options['quality'] = $quality; |
||
| 256 | } |
||
| 257 | $quality = $this->_getOption('quality', $options, 75); |
||
| 258 | imagick_setcompressionquality($this->imageHandle, $quality); |
||
| 259 | |||
| 260 | if ($type && strcasecmp($type, $this->type) |
||
| 261 | && !imagick_convert($this->imageHandle, $type)) { |
||
| 262 | return $this->raiseError('Couldn\'t save image to file (conversion failed).', IMAGE_TRANSFORM_ERROR_FAILED); |
||
| 263 | } |
||
| 264 | if (!($image = imagick_image2blob($this->imageHandle))) { |
||
| 265 | return $this->raiseError('Couldn\'t display image.', IMAGE_TRANSFORM_ERROR_IO); |
||
| 266 | } |
||
| 267 | header('Content-type: ' . imagick_getmimetype($this->imageHandle)); |
||
| 268 | echo $image; |
||
| 269 | $this->free(); |
||
| 270 | |||
| 271 | return true; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Adjusts the image gamma |
||
| 276 | * |
||
| 277 | * @param float $outputgamma |
||
| 278 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 279 | * @access public |
||
| 280 | */ |
||
| 281 | public function gamma($outputgamma = 1.0) |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Crops the image |
||
| 292 | * |
||
| 293 | * @param mixed $width |
||
| 294 | * @param mixed $height |
||
| 295 | * @param mixed $x |
||
| 296 | * @param mixed $y |
||
| 297 | * |
||
| 298 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 299 | * @access public |
||
| 300 | */ |
||
| 301 | public function crop($width, $height, $x = 0, $y = 0) |
||
| 302 | { |
||
| 303 | // Sanity check |
||
| 304 | if (!$this->intersects($width, $height, $x, $y)) { |
||
| 305 | return PEAR::raiseError('Nothing to crop', IMAGE_TRANSFORM_ERROR_OUTOFBOUND); |
||
| 306 | } |
||
| 307 | if (!imagick_crop($this->imageHandle, $x, $y, $width, $height)) { |
||
| 308 | return $this->raiseError('Couldn\'t crop image.', IMAGE_TRANSFORM_ERROR_FAILED); |
||
| 309 | } |
||
| 310 | |||
| 311 | // I think that setting img_x/y is wrong, but scaleByLength() & friends |
||
| 312 | // mess up the aspect after a crop otherwise. |
||
| 313 | $this->new_x = $width; |
||
| 314 | $this->new_y = $height; |
||
| 315 | |||
| 316 | return true; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Horizontal mirroring |
||
| 321 | * |
||
| 322 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 323 | * @access public |
||
| 324 | */ |
||
| 325 | public function mirror() |
||
| 326 | { |
||
| 327 | if (!imagick_flop($this->imageHandle)) { |
||
| 328 | return $this->raiseError('Couldn\'t mirror the image.', IMAGE_TRANSFORM_ERROR_FAILED); |
||
| 329 | } |
||
| 330 | |||
| 331 | return true; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Vertical mirroring |
||
| 336 | * |
||
| 337 | * @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
||
| 338 | * @access public |
||
| 339 | */ |
||
| 340 | public function flip() |
||
| 341 | { |
||
| 342 | if (!imagick_flip($this->imageHandle)) { |
||
| 343 | return $this->raiseError('Couldn\'t flip the image.', IMAGE_TRANSFORM_ERROR_FAILED); |
||
| 344 | } |
||
| 345 | |||
| 346 | return true; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Destroy image handle |
||
| 351 | * |
||
| 352 | * @access public |
||
| 353 | */ |
||
| 354 | public function free() |
||
| 355 | { |
||
| 356 | if (is_resource($this->imageHandle)) { |
||
| 357 | imagick_destroyhandle($this->imageHandle); |
||
| 358 | } |
||
| 359 | $this->imageHandle = null; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * RaiseError Method - shows imagick Raw errors. |
||
| 364 | * |
||
| 365 | * @param string $message message = prefixed message.. |
||
| 366 | * @param int $code error code |
||
| 367 | * @return PEAR error object |
||
| 368 | * @access protected |
||
| 369 | */ |
||
| 370 | public function raiseError($message, $code = 0) |
||
| 377 | } |
||
| 378 | } // End class Image_Transform_Driver_Imagick2 |
||
| 379 |