Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OC_Image 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 OC_Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class OC_Image implements \OCP\IImage { |
||
|
|
|||
| 46 | /** @var false|resource */ |
||
| 47 | protected $resource = false; // tmp resource. |
||
| 48 | /** @var int */ |
||
| 49 | protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. |
||
| 50 | /** @var string */ |
||
| 51 | protected $mimeType = 'image/png'; // Default to png |
||
| 52 | /** @var int */ |
||
| 53 | protected $bitDepth = 24; |
||
| 54 | /** @var null|string */ |
||
| 55 | protected $filePath = null; |
||
| 56 | /** @var finfo */ |
||
| 57 | private $fileInfo; |
||
| 58 | /** @var \OCP\ILogger */ |
||
| 59 | private $logger; |
||
| 60 | /** @var \OCP\IConfig */ |
||
| 61 | private $config; |
||
| 62 | /** @var array */ |
||
| 63 | private $exif; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Constructor. |
||
| 67 | * |
||
| 68 | * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by |
||
| 69 | * an imagecreate* function. |
||
| 70 | * @param \OCP\ILogger $logger |
||
| 71 | * @param \OCP\IConfig $config |
||
| 72 | * @throws \InvalidArgumentException in case the $imageRef parameter is not null |
||
| 73 | */ |
||
| 74 | public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { |
||
| 75 | $this->logger = $logger; |
||
| 76 | if ($logger === null) { |
||
| 77 | $this->logger = \OC::$server->getLogger(); |
||
| 78 | } |
||
| 79 | $this->config = $config; |
||
| 80 | if ($config === null) { |
||
| 81 | $this->config = \OC::$server->getConfig(); |
||
| 82 | } |
||
| 83 | |||
| 84 | if (\OC_Util::fileInfoLoaded()) { |
||
| 85 | $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($imageRef !== null) { |
||
| 89 | throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Determine whether the object contains an image resource. |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function valid() { // apparently you can't name a method 'empty'... |
||
| 99 | return is_resource($this->resource); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the MIME type of the image or an empty string if no image is loaded. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function mimeType() { |
||
| 108 | return $this->valid() ? $this->mimeType : ''; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the width of the image or -1 if no image is loaded. |
||
| 113 | * |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | public function width() { |
||
| 117 | return $this->valid() ? imagesx($this->resource) : -1; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the height of the image or -1 if no image is loaded. |
||
| 122 | * |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | public function height() { |
||
| 126 | return $this->valid() ? imagesy($this->resource) : -1; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns the width when the image orientation is top-left. |
||
| 131 | * |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function widthTopLeft() { |
|
| 135 | $o = $this->getOrientation(); |
||
| 136 | $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
||
| 137 | switch ($o) { |
||
| 138 | case -1: |
||
| 139 | case 1: |
||
| 140 | case 2: // Not tested |
||
| 141 | case 3: |
||
| 142 | case 4: // Not tested |
||
| 143 | return $this->width(); |
||
| 144 | case 5: // Not tested |
||
| 145 | case 6: |
||
| 146 | case 7: // Not tested |
||
| 147 | case 8: |
||
| 148 | return $this->height(); |
||
| 149 | } |
||
| 150 | return $this->width(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the height when the image orientation is top-left. |
||
| 155 | * |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function heightTopLeft() { |
|
| 159 | $o = $this->getOrientation(); |
||
| 160 | $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
||
| 161 | switch ($o) { |
||
| 162 | case -1: |
||
| 163 | case 1: |
||
| 164 | case 2: // Not tested |
||
| 165 | case 3: |
||
| 166 | case 4: // Not tested |
||
| 167 | return $this->height(); |
||
| 168 | case 5: // Not tested |
||
| 169 | case 6: |
||
| 170 | case 7: // Not tested |
||
| 171 | case 8: |
||
| 172 | return $this->width(); |
||
| 173 | } |
||
| 174 | return $this->height(); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Outputs the image. |
||
| 179 | * |
||
| 180 | * @param string $mimeType |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function show($mimeType = null) { |
||
| 184 | if ($mimeType === null) { |
||
| 185 | $mimeType = $this->mimeType(); |
||
| 186 | } |
||
| 187 | header('Content-Type: ' . $mimeType); |
||
| 188 | return $this->_output(null, $mimeType); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Saves the image. |
||
| 193 | * |
||
| 194 | * @param string $filePath |
||
| 195 | * @param string $mimeType |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | |||
| 199 | public function save($filePath = null, $mimeType = null) { |
||
| 200 | if ($mimeType === null) { |
||
| 201 | $mimeType = $this->mimeType(); |
||
| 202 | } |
||
| 203 | if ($filePath === null) { |
||
| 204 | if ($this->filePath === null) { |
||
| 205 | $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
||
| 206 | return false; |
||
| 207 | } else { |
||
| 208 | $filePath = $this->filePath; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | return $this->_output($filePath, $mimeType); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Outputs/saves the image. |
||
| 216 | * |
||
| 217 | * @param string $filePath |
||
| 218 | * @param string $mimeType |
||
| 219 | * @return bool |
||
| 220 | * @throws Exception |
||
| 221 | */ |
||
| 222 | private function _output($filePath = null, $mimeType = null) { |
||
| 223 | if ($filePath) { |
||
| 224 | if (!file_exists(dirname($filePath))) { |
||
| 225 | mkdir(dirname($filePath), 0777, true); |
||
| 226 | } |
||
| 227 | $isWritable = is_writable(dirname($filePath)); |
||
| 228 | if (!$isWritable) { |
||
| 229 | $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
||
| 230 | return false; |
||
| 231 | } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
||
| 232 | $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
||
| 233 | return false; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | if (!$this->valid()) { |
||
| 237 | return false; |
||
| 238 | } |
||
| 239 | |||
| 240 | $imageType = $this->imageType; |
||
| 241 | if ($mimeType !== null) { |
||
| 242 | switch ($mimeType) { |
||
| 243 | case 'image/gif': |
||
| 244 | $imageType = IMAGETYPE_GIF; |
||
| 245 | break; |
||
| 246 | case 'image/jpeg': |
||
| 247 | $imageType = IMAGETYPE_JPEG; |
||
| 248 | break; |
||
| 249 | case 'image/png': |
||
| 250 | $imageType = IMAGETYPE_PNG; |
||
| 251 | break; |
||
| 252 | case 'image/x-xbitmap': |
||
| 253 | $imageType = IMAGETYPE_XBM; |
||
| 254 | break; |
||
| 255 | case 'image/bmp': |
||
| 256 | case 'image/x-ms-bmp': |
||
| 257 | $imageType = IMAGETYPE_BMP; |
||
| 258 | break; |
||
| 259 | default: |
||
| 260 | throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | switch ($imageType) { |
||
| 265 | case IMAGETYPE_GIF: |
||
| 266 | $retVal = imagegif($this->resource, $filePath); |
||
| 267 | break; |
||
| 268 | case IMAGETYPE_JPEG: |
||
| 269 | $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality()); |
||
| 270 | break; |
||
| 271 | case IMAGETYPE_PNG: |
||
| 272 | $retVal = imagepng($this->resource, $filePath); |
||
| 273 | break; |
||
| 274 | case IMAGETYPE_XBM: |
||
| 275 | if (function_exists('imagexbm')) { |
||
| 276 | $retVal = imagexbm($this->resource, $filePath); |
||
| 277 | } else { |
||
| 278 | throw new Exception('\OC_Image::_output(): imagexbm() is not supported.'); |
||
| 279 | } |
||
| 280 | |||
| 281 | break; |
||
| 282 | case IMAGETYPE_WBMP: |
||
| 283 | $retVal = imagewbmp($this->resource, $filePath); |
||
| 284 | break; |
||
| 285 | case IMAGETYPE_BMP: |
||
| 286 | $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); |
||
| 287 | break; |
||
| 288 | default: |
||
| 289 | $retVal = imagepng($this->resource, $filePath); |
||
| 290 | } |
||
| 291 | return $retVal; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Prints the image when called as $image(). |
||
| 296 | */ |
||
| 297 | public function __invoke() { |
||
| 298 | return $this->show(); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param resource Returns the image resource in any. |
||
| 303 | * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
||
| 304 | */ |
||
| 305 | public function setResource($resource) { |
||
| 306 | if (get_resource_type($resource) === 'gd') { |
||
| 307 | $this->resource = $resource; |
||
| 308 | return; |
||
| 309 | } |
||
| 310 | throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return resource Returns the image resource in any. |
||
| 315 | */ |
||
| 316 | public function resource() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return string Returns the mimetype of the data. Returns the empty string |
||
| 322 | * if the data is not valid. |
||
| 323 | */ |
||
| 324 | public function dataMimeType() { |
||
| 325 | if (!$this->valid()) { |
||
| 326 | return ''; |
||
| 327 | } |
||
| 328 | |||
| 329 | switch ($this->mimeType) { |
||
| 330 | case 'image/png': |
||
| 331 | case 'image/jpeg': |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return null|string Returns the raw image data. |
||
| 341 | */ |
||
| 342 | public function data() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return string - base64 encoded, which is suitable for embedding in a VCard. |
||
| 375 | */ |
||
| 376 | public function __toString() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return int|null |
||
| 382 | */ |
||
| 383 | protected function getJpegQuality() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * (I'm open for suggestions on better method name ;) |
||
| 393 | * Get the orientation based on EXIF data. |
||
| 394 | * |
||
| 395 | * @return int The orientation or -1 if no EXIF data is available. |
||
| 396 | */ |
||
| 397 | public function getOrientation() { |
||
| 428 | |||
| 429 | public function readExif($data) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * (I'm open for suggestions on better method name ;) |
||
| 451 | * Fixes orientation based on EXIF data. |
||
| 452 | * |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | public function fixOrientation() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Loads an image from an open file handle. |
||
| 521 | * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. |
||
| 522 | * |
||
| 523 | * @param resource $handle |
||
| 524 | * @return resource|false An image resource or false on error |
||
| 525 | */ |
||
| 526 | public function loadFromFileHandle($handle) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Loads an image from a local file. |
||
| 536 | * |
||
| 537 | * @param bool|string $imagePath The path to a local file. |
||
| 538 | * @return bool|resource An image resource or false on error |
||
| 539 | */ |
||
| 540 | public function loadFromFile($imagePath = false) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Loads an image from a string of data. |
||
| 637 | * |
||
| 638 | * @param string $str A string of image data as read from a file. |
||
| 639 | * @return bool|resource An image resource or false on error |
||
| 640 | */ |
||
| 641 | public function loadFromData($str) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Loads an image from a base64 encoded string. |
||
| 663 | * |
||
| 664 | * @param string $str A string base64 encoded string of image data. |
||
| 665 | * @return bool|resource An image resource or false on error |
||
| 666 | */ |
||
| 667 | public function loadFromBase64($str) { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Create a new image from file or URL |
||
| 689 | * |
||
| 690 | * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm |
||
| 691 | * @version 1.00 |
||
| 692 | * @param string $fileName <p> |
||
| 693 | * Path to the BMP image. |
||
| 694 | * </p> |
||
| 695 | * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors. |
||
| 696 | */ |
||
| 697 | private function imagecreatefrombmp($fileName) { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Resizes the image preserving ratio. |
||
| 843 | * |
||
| 844 | * @param integer $maxSize The maximum size of either the width or height. |
||
| 845 | * @return bool |
||
| 846 | */ |
||
| 847 | public function resize($maxSize) { |
||
| 867 | |||
| 868 | /** |
||
| 869 | * @param int $width |
||
| 870 | * @param int $height |
||
| 871 | * @return bool |
||
| 872 | */ |
||
| 873 | public function preciseResize(int $width, int $height): bool { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Crops the image to the middle square. If the image is already square it just returns. |
||
| 908 | * |
||
| 909 | * @param int $size maximum size for the result (optional) |
||
| 910 | * @return bool for success or failure |
||
| 911 | */ |
||
| 912 | public function centerCrop($size = 0) { |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Crops the image from point $x$y with dimension $wx$h. |
||
| 966 | * |
||
| 967 | * @param int $x Horizontal position |
||
| 968 | * @param int $y Vertical position |
||
| 969 | * @param int $w Width |
||
| 970 | * @param int $h Height |
||
| 971 | * @return bool for success or failure |
||
| 972 | */ |
||
| 973 | public function crop(int $x, int $y, int $w, int $h): bool { |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Resizes the image to fit within a boundary while preserving ratio. |
||
| 1005 | * |
||
| 1006 | * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up |
||
| 1007 | * |
||
| 1008 | * @param integer $maxWidth |
||
| 1009 | * @param integer $maxHeight |
||
| 1010 | * @return bool |
||
| 1011 | */ |
||
| 1012 | public function fitIn($maxWidth, $maxHeight) { |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Shrinks larger images to fit within specified boundaries while preserving ratio. |
||
| 1030 | * |
||
| 1031 | * @param integer $maxWidth |
||
| 1032 | * @param integer $maxHeight |
||
| 1033 | * @return bool |
||
| 1034 | */ |
||
| 1035 | public function scaleDownToFit($maxWidth, $maxHeight) { |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Destroys the current image and resets the object |
||
| 1052 | */ |
||
| 1053 | public function destroy() { |
||
| 1059 | |||
| 1060 | public function __destruct() { |
||
| 1063 | } |
||
| 1064 | |||
| 1197 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.