| Total Complexity | 48 |
| Total Lines | 318 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ImageResize 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 ImageResize, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ImageResize implements ResizeInterface |
||
| 23 | { |
||
| 24 | |||
| 25 | |||
| 26 | private $file; |
||
| 27 | private $image; |
||
| 28 | private $width; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public function getFile() |
||
| 34 | { |
||
| 35 | return $this->file; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return resource |
||
| 40 | */ |
||
| 41 | public function getImage() |
||
| 42 | { |
||
| 43 | return $this->image; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | public function getWidth() |
||
| 50 | { |
||
| 51 | return $this->width; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return mixed |
||
| 56 | */ |
||
| 57 | public function getHeight() |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return mixed |
||
| 64 | */ |
||
| 65 | public function getBits() |
||
| 66 | { |
||
| 67 | return $this->bits; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return mixed |
||
| 72 | */ |
||
| 73 | public function getMime() |
||
| 74 | { |
||
| 75 | return $this->mime; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param mixed $file |
||
| 80 | */ |
||
| 81 | public function setFile($file) |
||
| 82 | { |
||
| 83 | $this->file = $file; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param resource $image |
||
| 88 | */ |
||
| 89 | public function setImage($image) |
||
| 90 | { |
||
| 91 | $this->image = $image; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param mixed $width |
||
| 96 | */ |
||
| 97 | public function setWidth($width) |
||
| 98 | { |
||
| 99 | $this->width = $width; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param mixed $height |
||
| 104 | */ |
||
| 105 | public function setHeight($height) |
||
| 106 | { |
||
| 107 | $this->height = $height; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $bits |
||
| 112 | */ |
||
| 113 | public function setBits(string $bits) |
||
| 114 | { |
||
| 115 | $this->bits = $bits; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $mime |
||
| 120 | */ |
||
| 121 | public function setMime(string $mime) |
||
| 124 | } |
||
| 125 | |||
| 126 | private $height; |
||
| 127 | private $bits; |
||
| 128 | private $mime; |
||
| 129 | |||
| 130 | public function upload($file) |
||
| 131 | { |
||
| 132 | if (file_exists($file)) { |
||
| 133 | $this->file = $file; |
||
| 134 | |||
| 135 | $info = getimagesize($file); |
||
| 136 | |||
| 137 | $this->setWidth($info[0]); |
||
| 138 | $this->setHeight($info[1]); |
||
| 139 | $this->setBits(isset($info['bits']) ? $info['bits'] : ''); |
||
| 140 | $this->setMime(isset($info['mime']) ? $info['mime'] : ''); |
||
| 141 | |||
| 142 | if ($this->getMime() == 'image/gif') { |
||
| 143 | $this->image = imagecreatefromgif($file); |
||
| 144 | $this->setImage($this->image); |
||
|
|
|||
| 145 | } elseif ($this->mime == 'image/png') { |
||
| 146 | $this->image = imagecreatefrompng($file); |
||
| 147 | $this->setImage($this->image); |
||
| 148 | |||
| 149 | } elseif ($this->mime == 'image/jpeg') { |
||
| 150 | $this->image = imagecreatefromjpeg($file); |
||
| 151 | $this->setImage($this->image); |
||
| 152 | |||
| 153 | } |
||
| 154 | } else { |
||
| 155 | exit('Error: Could not load image ' . $file . '!'); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * |
||
| 162 | * @param type $file Inser you file |
||
| 163 | * @param type $quality Isert optional quality for image |
||
| 164 | * Exemple save('exemple.jpg, 100); |
||
| 165 | */ |
||
| 166 | public function save(string $savePath, int $imageQuality = 100) |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * |
||
| 187 | * @param type $width Inser Width for you image |
||
| 188 | * @param type $height Inser height for you image |
||
| 189 | * @param type $default nser you scale (where w = width end h = height) |
||
| 190 | * @return type |
||
| 191 | * Exemplae: resize(800, 600, 'w'); |
||
| 192 | */ |
||
| 193 | public function resizeImage($width = 0, $height = 0, $option = 'auto') |
||
| 194 | { |
||
| 195 | if (!$this->width || !$this->height) { |
||
| 196 | return; |
||
| 197 | } |
||
| 198 | |||
| 199 | $xpos = 0; |
||
| 200 | $ypos = 0; |
||
| 201 | $scale = 1; |
||
| 202 | |||
| 203 | $scale_w = $width / $this->width; |
||
| 204 | $scale_h = $height / $this->height; |
||
| 205 | |||
| 206 | if ($option == 'w') { |
||
| 207 | $scale = $scale_w; |
||
| 208 | } elseif ($option == 'h') { |
||
| 209 | $scale = $scale_h; |
||
| 210 | } else { |
||
| 211 | $scale = min($scale_w, $scale_h); |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') { |
||
| 215 | return; |
||
| 216 | } |
||
| 217 | |||
| 218 | $new_width = (int)($this->width * $scale); |
||
| 219 | $new_height = (int)($this->height * $scale); |
||
| 220 | $xpos = (int)(($width - $new_width) / 2); |
||
| 221 | $ypos = (int)(($height - $new_height) / 2); |
||
| 222 | |||
| 223 | $image_old = $this->image; |
||
| 224 | $this->image = imagecreatetruecolor($width, $height); |
||
| 225 | |||
| 226 | if ($this->mime == 'image/png') { |
||
| 227 | imagealphablending($this->image, false); |
||
| 228 | imagesavealpha($this->image, true); |
||
| 229 | $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); |
||
| 230 | imagecolortransparent($this->image, $background); |
||
| 231 | } else { |
||
| 232 | $background = imagecolorallocate($this->image, 255, 255, 255); |
||
| 233 | } |
||
| 234 | |||
| 235 | imagefilledrectangle($this->image, 0, 0, $width, $height, $background); |
||
| 236 | |||
| 237 | imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height); |
||
| 238 | imagedestroy($image_old); |
||
| 239 | |||
| 240 | $this->width = $width; |
||
| 241 | $this->height = $height; |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | public function crop($top_x, $top_y, $bottom_x, $bottom_y) |
||
| 246 | { |
||
| 247 | $image_old = $this->image; |
||
| 248 | $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y); |
||
| 249 | |||
| 250 | imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->width, $this->height); |
||
| 251 | imagedestroy($image_old); |
||
| 252 | |||
| 253 | $this->width = $bottom_x - $top_x; |
||
| 254 | $this->height = $bottom_y - $top_y; |
||
| 255 | } |
||
| 256 | |||
| 257 | public function imageRotate(int $degree, $color = '000000') |
||
| 258 | { |
||
| 259 | |||
| 260 | $rgb = $this->html2rgb($color); |
||
| 261 | |||
| 262 | $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); |
||
| 263 | |||
| 264 | $this->width = imagesx($this->image); |
||
| 265 | $this->height = imagesy($this->image); |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * |
||
| 271 | * @param type $watermark |
||
| 272 | * @param type $position |
||
| 273 | */ |
||
| 274 | public function watermark($watermark, $position = 'bottomright') |
||
| 275 | { |
||
| 276 | $watermark = imagecreatefromjpeg('DDDDD'); |
||
| 277 | |||
| 278 | switch ($position) { |
||
| 279 | case 'topleft': |
||
| 280 | $watermark_pos_x = 0; |
||
| 281 | $watermark_pos_y = 0; |
||
| 282 | break; |
||
| 283 | case 'topright': |
||
| 284 | $watermark_pos_x = $this->width - 10; |
||
| 285 | $watermark_pos_y = 0; |
||
| 286 | break; |
||
| 287 | case 'bottomleft': |
||
| 288 | $watermark_pos_x = 0; |
||
| 289 | $watermark_pos_y = $this->height - 10; |
||
| 290 | break; |
||
| 291 | case 'bottomright': |
||
| 292 | $watermark_pos_x = $this->width - 5; |
||
| 293 | $watermark_pos_y = $this->height - 5; |
||
| 294 | break; |
||
| 295 | } |
||
| 296 | |||
| 297 | imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, $this->getWidth(), $this->getHeight()); |
||
| 298 | |||
| 299 | imagedestroy($watermark); |
||
| 300 | } |
||
| 301 | |||
| 302 | private function filter() |
||
| 303 | { |
||
| 304 | $args = func_get_args(); |
||
| 305 | |||
| 306 | call_user_func_array('imagefilter', $args); |
||
| 307 | } |
||
| 308 | |||
| 309 | private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') |
||
| 310 | { |
||
| 311 | $rgb = $this->html2rgb($color); |
||
| 312 | |||
| 313 | imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2])); |
||
| 314 | } |
||
| 315 | |||
| 316 | private function merge($merge, $x = 0, $y = 0, $opacity = 100) |
||
| 319 | } |
||
| 320 | |||
| 321 | private function html2rgb($color) |
||
| 322 | { |
||
| 323 | if ($color[0] == '#') { |
||
| 324 | $color = substr($color, 1); |
||
| 325 | } |
||
| 326 | |||
| 340 | } |
||
| 341 | |||
| 342 | } |
||
| 343 |