| Total Complexity | 69 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like image_gmagick 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_gmagick, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class image_gmagick 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 | $w = $this->image->getImageWidth(); |
||
| 55 | $h = $this->image->getImageHeight(); |
||
| 56 | } catch (\Exception $e) { |
||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | if (false === $background) { |
||
| 61 | $this->width = $w; |
||
| 62 | $this->height = $h; |
||
| 63 | return true; |
||
| 64 | } else { |
||
| 65 | try { |
||
| 66 | $this->image->setImageBackgroundColor($background); |
||
| 67 | $x = round(($width - $w) / 2); |
||
| 68 | $y = round(($height - $h) / 2); |
||
| 69 | $img = new \Gmagick(); |
||
| 70 | $img->newImage($width, $height, $background); |
||
| 71 | $img->compositeImage($this->image, 1, $x, $y); |
||
| 72 | } catch (\Exception $e) { |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | $this->image = $img; |
||
| 76 | $this->width = $width; |
||
| 77 | $this->height = $height; |
||
| 78 | return true; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | public function resizeCrop($width, $height, $offset = false) |
||
| 83 | { |
||
| 84 | if (!$width) { |
||
| 85 | $width = 1; |
||
| 86 | } |
||
| 87 | if (!$height) { |
||
| 88 | $height = 1; |
||
| 89 | } |
||
| 90 | |||
| 91 | if (($this->width / $this->height) > ($width / $height)) { |
||
| 92 | $h = $height; |
||
| 93 | $w = ($this->width * $h) / $this->height; |
||
| 94 | $y = 0; |
||
| 95 | if (false !== $offset) { |
||
| 96 | if ($offset > 0) { |
||
| 97 | $offset = -$offset; |
||
| 98 | } |
||
| 99 | if (($w + $offset) <= $width) { |
||
| 100 | $offset = $width - $w; |
||
| 101 | } |
||
| 102 | $x = $offset; |
||
| 103 | } else { |
||
| 104 | $x = ($width - $w) / 2; |
||
| 105 | } |
||
| 106 | } else { |
||
| 107 | $w = $width; |
||
| 108 | $h = ($this->height * $w) / $this->width; |
||
| 109 | $x = 0; |
||
| 110 | if (false !== $offset) { |
||
| 111 | if ($offset > 0) { |
||
| 112 | $offset = -$offset; |
||
| 113 | } |
||
| 114 | if (($h + $offset) <= $height) { |
||
| 115 | $offset = $height - $h; |
||
| 116 | } |
||
| 117 | $y = $offset; |
||
| 118 | } else { |
||
| 119 | $y = ($height - $h) / 2; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | $x = round($x); |
||
| 124 | $y = round($y); |
||
| 125 | $w = round($w); |
||
| 126 | $h = round($h); |
||
| 127 | if (!$w) { |
||
| 128 | $w = 1; |
||
| 129 | } |
||
| 130 | if (!$h) { |
||
| 131 | $h = 1; |
||
| 132 | } |
||
| 133 | |||
| 134 | try { |
||
| 135 | $this->image->scaleImage($w, $h); |
||
| 136 | $this->image->cropImage($width, $height, -$x, -$y); |
||
| 137 | } catch (\Exception $e) { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->width = $width; |
||
| 142 | $this->height = $height; |
||
| 143 | return true; |
||
| 144 | } |
||
| 145 | |||
| 146 | public function rotate($angle, $background = '#000000') |
||
| 147 | { |
||
| 148 | try { |
||
| 149 | $this->image->rotateImage($background, $angle); |
||
| 150 | $w = $this->image->getImageWidth(); |
||
| 151 | $h = $this->image->getImageHeight(); |
||
| 152 | } catch (\Exception $e) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | $this->width = $w; |
||
| 156 | $this->height = $h; |
||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function flipHorizontal() |
||
| 161 | { |
||
| 162 | try { |
||
| 163 | $this->image->flopImage(); |
||
| 164 | } catch (\Exception $e) { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | return true; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function flipVertical() |
||
| 178 | } |
||
| 179 | |||
| 180 | public function watermark($file, $left = false, $top = false) |
||
| 181 | { |
||
| 182 | try { |
||
| 183 | $wm = new \Gmagick($file); |
||
| 184 | $w = $wm->getImageWidth(); |
||
| 185 | $h = $wm->getImageHeight(); |
||
| 186 | } catch (\Exception $e) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | $x = (true === $left) ? 0 : ((null === $left) ? round(($this->width - $w) / 2) : (((false === $left) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left)); |
||
| 191 | $y = (true === $top) ? 0 : ((null === $top) ? round(($this->height - $h) / 2) : (((false === $top) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top)); |
||
| 192 | |||
| 193 | if ((($x + $w) > $this->width) |
||
| 194 | || (($y + $h) > $this->height) |
||
| 195 | || ($x < 0) |
||
| 196 | || ($y < 0)) { |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | try { |
||
| 201 | $this->image->compositeImage($wm, 1, $x, $y); |
||
| 202 | } catch (\Exception $e) { |
||
| 203 | return false; |
||
| 204 | } |
||
| 205 | return true; |
||
| 206 | } |
||
| 207 | |||
| 208 | // ABSTRACT PROTECTED METHODS |
||
| 209 | |||
| 210 | protected function getBlankImage($width, $height) |
||
| 211 | { |
||
| 212 | try { |
||
| 213 | $img = new \Gmagick(); |
||
| 214 | $img->newImage($width, $height, 'none'); |
||
| 215 | } catch (\Exception $e) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | return $img; |
||
| 219 | } |
||
| 220 | |||
| 221 | protected function getImage($image, &$width, &$height) |
||
| 222 | { |
||
| 223 | if (is_object($image) && ($image instanceof image_gmagick)) { |
||
| 224 | $width = $image->width; |
||
| 225 | $height = $image->height; |
||
| 226 | return $image->image; |
||
| 227 | } elseif (is_object($image) && ($image instanceof \Gmagick)) { |
||
| 228 | try { |
||
| 229 | $w = $image->getImageWidth(); |
||
| 230 | $h = $image->getImageHeight(); |
||
| 231 | } catch (\Exception $e) { |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | $width = $w; |
||
| 235 | $height = $h; |
||
| 236 | return $image; |
||
| 237 | } elseif (is_string($image)) { |
||
| 238 | try { |
||
| 239 | $image = new \Gmagick($image); |
||
| 240 | $w = $image->getImageWidth(); |
||
| 241 | $h = $image->getImageHeight(); |
||
| 242 | } catch (\Exception $e) { |
||
| 243 | return false; |
||
| 244 | } |
||
| 245 | $width = $w; |
||
| 246 | $height = $h; |
||
| 247 | return $image; |
||
| 248 | } else { |
||
| 249 | return false; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | // PSEUDO-ABSTRACT STATIC METHODS |
||
| 254 | |||
| 255 | public static function available() |
||
| 256 | { |
||
| 257 | return class_exists('Gmagick'); |
||
| 258 | } |
||
| 259 | |||
| 260 | public static function checkImage($file) |
||
| 261 | { |
||
| 262 | try { |
||
| 263 | $img = new \Gmagick($file); |
||
| 264 | } catch (\Exception $e) { |
||
| 265 | return false; |
||
| 266 | } |
||
| 267 | return true; |
||
| 268 | } |
||
| 269 | |||
| 270 | // INHERIT METHODS |
||
| 271 | |||
| 272 | public function output($type = 'jpeg', array $options = []) |
||
| 273 | { |
||
| 274 | $type = strtolower($type); |
||
| 275 | try { |
||
| 276 | $this->image->setImageFormat($type); |
||
| 277 | } catch (\Exception $e) { |
||
| 278 | return false; |
||
| 279 | } |
||
| 280 | $method = "optimize_$type"; |
||
| 281 | if (method_exists($this, $method) && !$this->$method($options)) { |
||
| 282 | return false; |
||
| 283 | } |
||
| 284 | |||
| 285 | if (!isset($options['file'])) { |
||
| 286 | if (!headers_sent()) { |
||
| 287 | $mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type"; |
||
| 288 | header("Content-Type: $mime"); |
||
| 289 | } |
||
| 290 | echo $this->image; |
||
| 291 | } else { |
||
| 292 | $file = $options['file'] . ".$type"; |
||
| 293 | try { |
||
| 294 | $this->image->writeImage($file); |
||
| 295 | } catch (\Exception $e) { |
||
| 296 | @unlink($file); |
||
| 297 | return false; |
||
| 298 | } |
||
| 299 | |||
| 300 | if (!@rename($file, $options['file'])) { |
||
| 301 | @unlink($file); |
||
| 302 | return false; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | return true; |
||
| 307 | } |
||
| 308 | |||
| 309 | // OWN METHODS |
||
| 310 | |||
| 311 | protected function optimize_jpeg(array $options = []) |
||
| 320 | } |
||
| 321 | |||
| 322 | } |
||
| 323 |