| Total Complexity | 70 |
| Total Lines | 306 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like image_imagick 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_imagick, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class image_imagick extends image |
||
| 18 | { |
||
| 19 | |||
| 20 | static public $MIMES = [//'tif' => "image/tiff" |
||
| 21 | ]; |
||
| 22 | |||
| 23 | // ABSTRACT PUBLIC METHODS |
||
| 24 | |||
| 25 | public function resize($width, $height) |
||
| 26 | {// |
||
| 27 | if (!$width) { |
||
| 28 | $width = 1; |
||
| 29 | } |
||
| 30 | if (!$height) { |
||
| 31 | $height = 1; |
||
| 32 | } |
||
| 33 | try { |
||
| 34 | $this->image->scaleImage($width, $height); |
||
| 35 | } catch (\Exception $e) { |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | $this->width = $width; |
||
| 39 | $this->height = $height; |
||
| 40 | return true; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function resizeFit($width, $height, $background = false) |
||
| 44 | {// |
||
| 45 | if (!$width) { |
||
| 46 | $width = 1; |
||
| 47 | } |
||
| 48 | if (!$height) { |
||
| 49 | $height = 1; |
||
| 50 | } |
||
| 51 | |||
| 52 | try { |
||
| 53 | $this->image->scaleImage($width, $height, true); |
||
| 54 | $size = $this->image->getImageGeometry(); |
||
| 55 | } catch (\Exception $e) { |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | |||
| 59 | if (false === $background) { |
||
| 60 | $this->width = $size['width']; |
||
| 61 | $this->height = $size['height']; |
||
| 62 | return true; |
||
| 63 | } else { |
||
| 64 | try { |
||
| 65 | $this->image->setImageBackgroundColor($background); |
||
| 66 | $x = -round(($width - $size['width']) / 2); |
||
| 67 | $y = -round(($height - $size['height']) / 2); |
||
| 68 | $this->image->extentImage($width, $height, $x, $y); |
||
| 69 | } catch (\Exception $e) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | $this->width = $width; |
||
| 73 | $this->height = $height; |
||
| 74 | return true; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | public function resizeCrop($width, $height, $offset = false) |
||
| 79 | { |
||
| 80 | if (!$width) { |
||
| 81 | $width = 1; |
||
| 82 | } |
||
| 83 | if (!$height) { |
||
| 84 | $height = 1; |
||
| 85 | } |
||
| 86 | |||
| 87 | if (($this->width / $this->height) > ($width / $height)) { |
||
| 88 | $h = $height; |
||
| 89 | $w = ($this->width * $h) / $this->height; |
||
| 90 | $y = 0; |
||
| 91 | if (false !== $offset) { |
||
| 92 | if ($offset > 0) { |
||
| 93 | $offset = -$offset; |
||
| 94 | } |
||
| 95 | if (($w + $offset) <= $width) { |
||
| 96 | $offset = $width - $w; |
||
| 97 | } |
||
| 98 | $x = $offset; |
||
| 99 | } else { |
||
| 100 | $x = ($width - $w) / 2; |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | $w = $width; |
||
| 104 | $h = ($this->height * $w) / $this->width; |
||
| 105 | $x = 0; |
||
| 106 | if (false !== $offset) { |
||
| 107 | if ($offset > 0) { |
||
| 108 | $offset = -$offset; |
||
| 109 | } |
||
| 110 | if (($h + $offset) <= $height) { |
||
| 111 | $offset = $height - $h; |
||
| 112 | } |
||
| 113 | $y = $offset; |
||
| 114 | } else { |
||
| 115 | $y = ($height - $h) / 2; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $x = round($x); |
||
| 120 | $y = round($y); |
||
| 121 | $w = round($w); |
||
| 122 | $h = round($h); |
||
| 123 | if (!$w) { |
||
| 124 | $w = 1; |
||
| 125 | } |
||
| 126 | if (!$h) { |
||
| 127 | $h = 1; |
||
| 128 | } |
||
| 129 | |||
| 130 | try { |
||
| 131 | $this->image->scaleImage($w, $h); |
||
| 132 | $this->image->cropImage($width, $height, -$x, -$y); |
||
| 133 | } catch (\Exception $e) { |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->width = $width; |
||
| 138 | $this->height = $height; |
||
| 139 | return true; |
||
| 140 | } |
||
| 141 | |||
| 142 | public function rotate($angle, $background = '#000000') |
||
| 143 | { |
||
| 144 | try { |
||
| 145 | $this->image->rotateImage(new \ImagickPixel($background), $angle); |
||
| 146 | $size = $this->image->getImageGeometry(); |
||
| 147 | } catch (\Exception $e) { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | $this->width = $size['width']; |
||
| 151 | $this->height = $size['height']; |
||
| 152 | return true; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function flipHorizontal() |
||
| 163 | } |
||
| 164 | |||
| 165 | public function flipVertical() |
||
| 166 | { |
||
| 167 | try { |
||
| 168 | $this->image->flipImage(); |
||
| 169 | } catch (\Exception $e) { |
||
| 170 | return false; |
||
| 171 | } |
||
| 172 | return true; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function watermark($file, $left = false, $top = false) |
||
| 176 | { |
||
| 177 | try { |
||
| 178 | $wm = new \Imagick($file); |
||
| 179 | $size = $wm->getImageGeometry(); |
||
| 180 | } catch (\Exception $e) { |
||
| 181 | return false; |
||
| 182 | } |
||
| 183 | |||
| 184 | $w = $size['width']; |
||
| 185 | $h = $size['height']; |
||
| 186 | $x = (true === $left) ? 0 : ((null === $left) ? round(($this->width - $w) / 2) : (((false === $left) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left)); |
||
| 187 | $y = (true === $top) ? 0 : ((null === $top) ? round(($this->height - $h) / 2) : (((false === $top) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top)); |
||
| 188 | |||
| 189 | if ((($x + $w) > $this->width) |
||
| 190 | || (($y + $h) > $this->height) |
||
| 191 | || ($x < 0) |
||
| 192 | || ($y < 0)) { |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | |||
| 196 | try { |
||
| 197 | $this->image->compositeImage($wm, \Imagick::COMPOSITE_DEFAULT, $x, $y); |
||
| 198 | } catch (\Exception $e) { |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | return true; |
||
| 202 | } |
||
| 203 | |||
| 204 | // ABSTRACT PROTECTED METHODS |
||
| 205 | |||
| 206 | protected function getBlankImage($width, $height) |
||
| 216 | } |
||
| 217 | |||
| 218 | protected function getImage($image, &$width, &$height) |
||
| 219 | { |
||
| 220 | if (is_object($image) && ($image instanceof image_imagick)) { |
||
| 221 | try { |
||
| 222 | $image->image->setImageCompressionQuality(100); |
||
| 223 | } catch (\Exception $e) { |
||
| 224 | return false; |
||
| 225 | } |
||
| 226 | $width = $image->width; |
||
| 227 | $height = $image->height; |
||
| 228 | return $image->image; |
||
| 229 | } elseif (is_object($image) && ($image instanceof \Imagick)) { |
||
| 230 | try { |
||
| 231 | $image->setImageCompressionQuality(100); |
||
| 232 | $size = $image->getImageGeometry(); |
||
| 233 | } catch (\Exception $e) { |
||
| 234 | return false; |
||
| 235 | } |
||
| 236 | $width = $size['width']; |
||
| 237 | $height = $size['height']; |
||
| 238 | return $image; |
||
| 239 | } elseif (is_string($image)) { |
||
| 240 | try { |
||
| 241 | $image = new \Imagick($image); |
||
| 242 | $image->setImageCompressionQuality(100); |
||
| 243 | $size = $image->getImageGeometry(); |
||
| 244 | } catch (\Exception $e) { |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | $width = $size['width']; |
||
| 248 | $height = $size['height']; |
||
| 249 | return $image; |
||
| 250 | } else { |
||
| 251 | return false; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | // PSEUDO-ABSTRACT STATIC METHODS |
||
| 256 | |||
| 257 | public static function available() |
||
| 260 | } |
||
| 261 | |||
| 262 | public static function checkImage($file) |
||
| 263 | { |
||
| 264 | try { |
||
| 265 | $img = new \Imagick($file); |
||
|
|
|||
| 266 | } catch (\Exception $e) { |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | return true; |
||
| 270 | } |
||
| 271 | |||
| 272 | // INHERIT METHODS |
||
| 273 | |||
| 274 | public function output($type = 'jpeg', array $options = []) |
||
| 275 | { |
||
| 276 | $type = strtolower($type); |
||
| 277 | try { |
||
| 278 | $this->image->setImageFormat($type); |
||
| 279 | } catch (\Exception $e) { |
||
| 280 | return false; |
||
| 281 | } |
||
| 282 | $method = "optimize_$type"; |
||
| 283 | if (method_exists($this, $method) && !$this->$method($options)) { |
||
| 284 | return false; |
||
| 285 | } |
||
| 286 | |||
| 287 | if (!isset($options['file'])) { |
||
| 288 | if (!headers_sent()) { |
||
| 289 | $mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type"; |
||
| 290 | header("Content-Type: $mime"); |
||
| 291 | } |
||
| 292 | echo $this->image; |
||
| 293 | } else { |
||
| 294 | $file = $options['file'] . ".$type"; |
||
| 295 | try { |
||
| 296 | $this->image->writeImage($file); |
||
| 297 | } catch (\Exception $e) { |
||
| 298 | @unlink($file); |
||
| 299 | return false; |
||
| 300 | } |
||
| 301 | |||
| 302 | if (!@rename($file, $options['file'])) { |
||
| 303 | @unlink($file); |
||
| 304 | return false; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | return true; |
||
| 309 | } |
||
| 310 | |||
| 311 | // OWN METHODS |
||
| 312 | |||
| 313 | protected function optimize_jpeg(array $options = []) |
||
| 323 | } |
||
| 324 | |||
| 325 | } |
||
| 326 |