| Total Complexity | 61 | 
| Total Lines | 371 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like ThumbsNails 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 ThumbsNails, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 28 | class ThumbsNails | ||
| 29 | { | ||
| 30 | public $_img_name = 'blank.gif'; | ||
| 31 | public $_img_path = 'uploads'; | ||
| 32 | public $_img_savepath = 'thumbs'; | ||
| 33 | |||
| 34 | public $_source_path = ''; | ||
| 35 | public $_save_path = ''; | ||
| 36 | public $_source_url = ''; | ||
| 37 | public $_source_image = ''; | ||
| 38 | public $_save_image = ''; | ||
| 39 | |||
| 40 | public $_usethumbs = 0; | ||
| 41 | public $_image_type = 'gd2'; | ||
| 42 | public $_return_fullpath = 0; | ||
| 43 | |||
| 44 | public $img_width = 100; | ||
| 45 | public $img_height = 100; | ||
| 46 | public $img_quality = 100; | ||
| 47 | public $img_update = 1; | ||
| 48 | public $img_aspect = 1; | ||
| 49 | |||
| 50 | /** | ||
| 51 | * @access private | ||
| 52 | */ | ||
| 53 | public $_img_info = []; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Constructor | ||
| 57 | * | ||
| 58 | * @param null $img_name | ||
|  | |||
| 59 | * @param null $img_path | ||
| 60 | * @param null $img_savepath | ||
| 61 | * | ||
| 62 | * @internal param string $_img_name | ||
| 63 | * @internal param string $_img_path | ||
| 64 | * @internal param string $_img_savepath | ||
| 65 | */ | ||
| 66 | public function __construct($img_name = null, $img_path = null, $img_savepath = null) | ||
| 67 |     { | ||
| 68 |         if (!\preg_match("/\.(jpg|gif|png|jpeg)$/i", $img_name)) { | ||
| 69 | // return false; | ||
| 70 | } | ||
| 71 | |||
| 72 | /* | ||
| 73 | * The actual image we will be processing | ||
| 74 | */ | ||
| 75 |         if (null !== $img_name) { | ||
| 76 | $this->_img_name = \trim($img_name); | ||
| 77 | } | ||
| 78 | |||
| 79 | /* | ||
| 80 | * The image path | ||
| 81 | */ | ||
| 82 |         if (null !== $img_path) { | ||
| 83 | $this->_img_path = \trim($img_path); | ||
| 84 | } | ||
| 85 | |||
| 86 | /* | ||
| 87 | * The image save path | ||
| 88 | */ | ||
| 89 |         if (null !== $img_savepath) { | ||
| 90 | $this->_img_savepath = \trim($img_savepath); | ||
| 91 | } | ||
| 92 | |||
| 93 | $path_to_check = XOOPS_ROOT_PATH . "/$img_path/$img_savepath"; | ||
| 94 | |||
| 95 |         if (!\is_dir($path_to_check)) { | ||
| 96 |             if (!mkdir($path_to_check, 0777) && !is_dir($path_to_check)) { | ||
| 97 | // return false; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | // return null; | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * WfThumbsNails::setUseThumbs() | ||
| 105 | * | ||
| 106 | * @param int $value | ||
| 107 | */ | ||
| 108 | public function setUseThumbs($value = 1) | ||
| 109 |     { | ||
| 110 | $this->_usethumbs = $value; | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * WfThumbsNails::setImageType() | ||
| 115 | * | ||
| 116 | * @param string $value | ||
| 117 | */ | ||
| 118 | public function setImageType($value = 'gd2') | ||
| 119 |     { | ||
| 120 | $this->_image_type = $value; | ||
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * ThumbsNails::createThumb() | ||
| 125 | * | ||
| 126 | * @param int|null $img_width | ||
| 127 | * @param int|null $img_height | ||
| 128 | * @param int|null $img_quality | ||
| 129 | * @param int|null $img_update | ||
| 130 | * @param int|null $img_aspect | ||
| 131 | * | ||
| 132 | * @return bool|string | ||
| 133 | */ | ||
| 134 | public function createThumb( | ||
| 135 | $img_width = null, | ||
| 136 | $img_height = null, | ||
| 137 | $img_quality = null, | ||
| 138 | $img_update = null, | ||
| 139 | $img_aspect = null | ||
| 140 |     ) { | ||
| 141 |         $this->_source_path  = XOOPS_ROOT_PATH . "/{$this->_img_path}"; | ||
| 142 |         $this->_save_path    = XOOPS_ROOT_PATH . "/{$this->_img_path}/{$this->_img_savepath}"; | ||
| 143 |         $this->_source_url   = XOOPS_URL . "/{$this->_img_path}"; | ||
| 144 |         $this->_source_image = "{$this->_source_path}/{$this->_img_name}"; | ||
| 145 | |||
| 146 |         if (null !== $img_width) { | ||
| 147 | $this->img_width = (int)$img_width; | ||
| 148 | } | ||
| 149 | |||
| 150 |         if (null !== $img_height) { | ||
| 151 | $this->img_height = (int)$img_height; | ||
| 152 | } | ||
| 153 | |||
| 154 |         if (null !== $img_quality) { | ||
| 155 | $this->img_quality = (int)$img_quality; | ||
| 156 | } | ||
| 157 | |||
| 158 |         if (null !== $img_update) { | ||
| 159 | $this->img_update = (int)$img_update; | ||
| 160 | } | ||
| 161 | |||
| 162 |         if (null !== $img_aspect) { | ||
| 163 | $this->img_aspect = (int)$img_aspect; | ||
| 164 | } | ||
| 165 | |||
| 166 | /** | ||
| 167 | * Return false if we are not using thumb nails | ||
| 168 | */ | ||
| 169 |         if (!$this->isUsingThumbs()) { | ||
| 170 | return $this->_source_url . '/' . $this->_img_name; | ||
| 171 | } | ||
| 172 | /** | ||
| 173 | * Return false if the server does not have gd lib installed or activated | ||
| 174 | */ | ||
| 175 |         if (!$this->checkGdLibrary()) { | ||
| 176 | return $this->_source_url . '/' . $this->_img_name; | ||
| 177 | } | ||
| 178 | |||
| 179 | /** | ||
| 180 | * Return false if the paths to the file are wrong | ||
| 181 | */ | ||
| 182 |         if (!$this->checkPaths()) { | ||
| 183 | return DEFAULT_PATH; | ||
| 184 | } | ||
| 185 | |||
| 186 |         if (!$this->checkImage()) { | ||
| 187 | return DEFAULT_PATH; | ||
| 188 | } | ||
| 189 | |||
| 190 | $image = $this->resizeImage(); | ||
| 191 |         if (false === $image) { | ||
| 192 | return DEFAULT_PATH; | ||
| 193 | } | ||
| 194 | |||
| 195 | return $image; | ||
| 196 | } | ||
| 197 | |||
| 198 | /** | ||
| 199 | * @param $value | ||
| 200 | */ | ||
| 201 | public function setImgName($value) | ||
| 202 |     { | ||
| 203 | $this->_img_name = \trim($value); | ||
| 204 | } | ||
| 205 | |||
| 206 | /** | ||
| 207 | * @param $value | ||
| 208 | */ | ||
| 209 | public function setImgPath($value) | ||
| 210 |     { | ||
| 211 | $this->_img_path = \trim($value); | ||
| 212 | } | ||
| 213 | |||
| 214 | /** | ||
| 215 | * @param $value | ||
| 216 | */ | ||
| 217 | public function setImgSavePath($value) | ||
| 218 |     { | ||
| 219 | $this->_img_savepath = \trim($value); | ||
| 220 | } | ||
| 221 | |||
| 222 | /** | ||
| 223 | * ThumbsNails::resizeImage() | ||
| 224 | * | ||
| 225 | * @return bool|string | ||
| 226 | */ | ||
| 227 | public function resizeImage() | ||
| 228 |     { | ||
| 229 | /** @var Wflinks\Helper $helper */ | ||
| 230 | $helper = Wflinks\Helper::getInstance(); | ||
| 231 | |||
| 232 | // $this->_img_info = info array to the image being resized | ||
| 233 | // $this->_img_info[0] == width | ||
| 234 | // $this->_img_info[1] == height | ||
| 235 | // $this->_img_info[2] == is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order) | ||
| 236 | // $this->_img_info[3] == is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag | ||
| 237 | /** | ||
| 238 | * Get image size and scale ratio | ||
| 239 | */ | ||
| 240 | $scale = \min($this->img_width / $this->_img_info[0], $this->img_height / $this->_img_info[1]); | ||
| 241 | /** | ||
| 242 | * If the image is larger than the max shrink it | ||
| 243 | */ | ||
| 244 | $newWidth = $this->img_width; | ||
| 245 | $newHeight = $this->img_height; | ||
| 246 |         if ($scale < 1 && 1 == $this->img_aspect) { | ||
| 247 | $newWidth = \floor($scale * $this->_img_info[0]); | ||
| 248 | $newHeight = \floor($scale * $this->_img_info[1]); | ||
| 249 | } | ||
| 250 | $newWidth = ($newWidth > $this->_img_info[0]) ? $this->_img_info[0] : $newWidth; | ||
| 251 | $newHeight = ($newHeight > $this->_img_info[0]) ? $this->_img_info[0] : $newHeight; | ||
| 252 | |||
| 253 |         $savefile          = "{$newWidth}x{$newHeight}_{$this->_img_name}"; | ||
| 254 |         $this->_save_image = "{$this->_save_path}/{$savefile}"; | ||
| 255 | |||
| 256 |         if (0 == $this->img_update && \file_exists($this->_save_image)) { | ||
| 257 |             if (1 == $this->_return_fullpath) { | ||
| 258 |                 return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; | ||
| 259 | } | ||
| 260 | |||
| 261 |             return "{$this->_img_savepath}/{$savefile}"; | ||
| 262 | } | ||
| 263 | |||
| 264 |         switch ($this->_image_type) { | ||
| 265 | case 'im': | ||
| 266 |                 if (!empty($helper->getConfig('path_magick')) && \is_dir($helper->getConfig('path_magick'))) { | ||
| 267 |                     if (\preg_match('#[A-Z]:|\\\\#Ai', __FILE__)) { | ||
| 268 | $cur_dir = __DIR__; | ||
| 269 |                         $src_file_im = '"' . $cur_dir . '\\' . \str_replace('/', '\\', $this->_source_image) . '"'; | ||
| 270 |                         $new_file_im = '"' . $cur_dir . '\\' . \str_replace('/', '\\', $this->_save_image) . '"'; | ||
| 271 |                     } else { | ||
| 272 | $src_file_im = \escapeshellarg($this->_source_image); | ||
| 273 | $new_file_im = \escapeshellarg($this->_save_image); | ||
| 274 | } | ||
| 275 |                     $magick_command = $helper->getConfig('path_magick') . '/convert -quality {$helper->getConfig("imagequality")} -antialias -sample {$newWidth}x{$newHeight} {$src_file_im} +profile "*" ' . \str_replace('\\', '/', $new_file_im) . ''; | ||
| 276 | \passthru($magick_command); | ||
| 277 | |||
| 278 |                     return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; | ||
| 279 | } | ||
| 280 | |||
| 281 | return false; | ||
| 282 | break; | ||
| 283 | case 'gd1': | ||
| 284 | case 'gd2': | ||
| 285 | default: | ||
| 286 | |||
| 287 |                 $imageCreateFunction = (\function_exists('imagecreatetruecolor') | ||
| 288 | && 'gd2' === $this->_image_type) ? 'imagecreatetruecolor' : 'imagecreate'; | ||
| 289 |                 $imageCopyfunction   = (\function_exists('imagecopyresampled') | ||
| 290 | && 'gd2' === $this->_image_type) ? 'imagecopyresampled' : 'imagecopyresized'; | ||
| 291 | |||
| 292 |                 switch ($this->_img_info[2]) { | ||
| 293 | case 1: | ||
| 294 | // GIF image | ||
| 295 |                         $img     = \function_exists('imagecreatefromgif') ? \imagecreatefromgif($this->_source_image) : \imagecreatefrompng($this->_source_image); | ||
| 296 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); | ||
| 297 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); | ||
| 298 |                         if (\function_exists('imagegif')) { | ||
| 299 | \imagegif($tmp_img, $this->_save_image); | ||
| 300 |                         } else { | ||
| 301 | \imagepng($tmp_img, $this->_save_image); | ||
| 302 | } | ||
| 303 | \imagedestroy($tmp_img); | ||
| 304 | break; | ||
| 305 | case 2: | ||
| 306 | // echo $this->_save_image; | ||
| 307 |                         $img     = \function_exists('imagecreatefromjpeg') ? \imagecreatefromjpeg($this->_source_image) : \imagecreatefrompng($this->_source_image); | ||
| 308 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); | ||
| 309 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); | ||
| 310 |                         if (\function_exists('imagejpeg')) { | ||
| 311 | \imagejpeg($tmp_img, $this->_save_image, $this->img_quality); | ||
| 312 |                         } else { | ||
| 313 | \imagepng($tmp_img, $this->_save_image); | ||
| 314 | } | ||
| 315 | \imagedestroy($tmp_img); | ||
| 316 | break; | ||
| 317 | case 3: | ||
| 318 | // PNG image | ||
| 319 | $img = \imagecreatefrompng($this->_source_image); | ||
| 320 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); | ||
| 321 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); | ||
| 322 | \imagepng($tmp_img, $this->_save_image); | ||
| 323 | \imagedestroy($tmp_img); | ||
| 324 | break; | ||
| 325 | default: | ||
| 326 | return false; | ||
| 327 | } | ||
| 328 |                 if (1 == $this->_return_fullpath) { | ||
| 329 |                     return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; | ||
| 330 | } | ||
| 331 | |||
| 332 |                 return "{$this->_img_savepath}/{$savefile}"; | ||
| 333 | break; | ||
| 334 | } | ||
| 335 | |||
| 336 | return false; | ||
| 337 | } | ||
| 338 | |||
| 339 | /** | ||
| 340 | * ThumbsNails::checkPaths() | ||
| 341 | * | ||
| 342 | * @return bool | ||
| 343 | */ | ||
| 344 | public function checkPaths() | ||
| 354 | } | ||
| 355 | |||
| 356 | /** | ||
| 357 | * @return bool | ||
| 358 | */ | ||
| 359 | public function checkImage() | ||
| 360 |     { | ||
| 361 | $this->_img_info = \getimagesize($this->_source_image, $imageinfo); | ||
| 362 | // if ( $this->_img_info[0] < $this->img_width && $this->_img_info[1] < $this->img_height ) | ||
| 363 | // return false; | ||
| 364 | return !(null === $this->_img_info); | ||
| 365 | } | ||
| 366 | |||
| 367 | /** | ||
| 368 | * wfsThumbs::checkGdLibrary() | ||
| 369 | * | ||
| 370 | * Private function | ||
| 371 | * | ||
| 372 | * @return array|false if gd lib not found on the system | ||
| 373 | */ | ||
| 374 | public function checkGdLibrary() | ||
| 375 |     { | ||
| 376 |         if (!\extension_loaded('gd')) { | ||
| 377 | return false; | ||
| 378 | } | ||
| 379 |         $gdlib = \function_exists('gd_info'); | ||
| 380 |         if (false === $gdlib = gd_info()) { | ||
| 381 | return false; | ||
| 382 | } | ||
| 383 | |||
| 384 | return $gdlib; | ||
| 385 | } | ||
| 386 | |||
| 387 | /** | ||
| 388 | * ThumbsNails::isUsingThumbs() | ||
| 389 | * | ||
| 390 | * @return bool | ||
| 391 | */ | ||
| 392 | public function isUsingThumbs() | ||
| 399 | } | ||
| 400 | } | ||
| 401 |