| Total Complexity | 55 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Standard |
||
| 22 | extends \Aimeos\MW\Media\Image\Base |
||
| 23 | implements \Aimeos\MW\Media\Image\Iface |
||
| 24 | { |
||
| 25 | private static $watermark; |
||
| 26 | |||
| 27 | private $options; |
||
| 28 | private $image; |
||
| 29 | private $mimetype; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Initializes the new image object. |
||
| 34 | * |
||
| 35 | * @param string $content File content |
||
| 36 | * @param string $mimetype Mime type of the media data |
||
| 37 | * @param array $options Associative list of configuration options |
||
| 38 | * @throws \Aimeos\MW\Media\Exception If image couldn't be retrieved from the given file name |
||
| 39 | */ |
||
| 40 | public function __construct( string $content, string $mimetype, array $options ) |
||
| 41 | { |
||
| 42 | parent::__construct( $mimetype ); |
||
| 43 | |||
| 44 | if( !self::$watermark && isset( $options['image']['watermark'] ) ) |
||
| 45 | { |
||
| 46 | if( ( $watermark = @file_get_contents( $options['image']['watermark'] ) ) === false ) |
||
| 47 | { |
||
| 48 | $msg = sprintf( 'Watermark image "%1$s" not found', $options['image']['watermark'] ); |
||
| 49 | throw new \Aimeos\MW\Media\Exception( $msg ); |
||
| 50 | } |
||
| 51 | |||
| 52 | if( ( $image = @imagecreatefromstring( $watermark ) ) === false ) { |
||
| 53 | throw new \Aimeos\MW\Media\Exception( sprintf( 'The watermark image isn\'t supported by GDlib' ) ); |
||
| 54 | } |
||
| 55 | |||
| 56 | self::$watermark = $image; |
||
| 57 | } |
||
| 58 | |||
| 59 | if( ( $this->image = @imagecreatefromstring( $content ) ) === false ) { |
||
| 60 | throw new \Aimeos\MW\Media\Exception( sprintf( 'The image type isn\'t supported by GDlib' ) ); |
||
| 61 | } |
||
| 62 | |||
| 63 | if( imagealphablending( $this->image, true ) === false ) { |
||
| 64 | throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagealphablending)' ) ); |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->options = $options; |
||
| 68 | $this->mimetype = $mimetype; |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Cleans up |
||
| 74 | */ |
||
| 75 | public function __destruct() |
||
| 76 | { |
||
| 77 | if( $this->image ) { |
||
| 78 | imagedestroy( $this->image ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the height of the image |
||
| 85 | * |
||
| 86 | * @return int Height in pixel |
||
| 87 | */ |
||
| 88 | public function getHeight() : int |
||
| 89 | { |
||
| 90 | return imagesy( $this->image ); |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the width of the image |
||
| 96 | * |
||
| 97 | * @return int Width in pixel |
||
| 98 | */ |
||
| 99 | public function getWidth() : int |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Stores the media data at the given file name. |
||
| 107 | * |
||
| 108 | * @param string|null $filename File name to save the data into or null to return the data |
||
| 109 | * @param string|null $mimetype Mime type to save the content as or null to leave the mime type unchanged |
||
| 110 | * @return string|null File content if file name is null or null if data is saved to the given file name |
||
| 111 | * @throws \Aimeos\MW\Media\Exception If image couldn't be saved to the given file name |
||
| 112 | */ |
||
| 113 | public function save( string $filename = null, string $mimetype = null ) : ?string |
||
| 186 | } |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Scales the image to the given width and height. |
||
| 191 | * |
||
| 192 | * @param int|null $width New width of the image or null for automatic calculation |
||
| 193 | * @param int|null $height New height of the image or null for automatic calculation |
||
| 194 | * @param int $fit "0" keeps image ratio, "1" adds padding while "2" crops image to enforce image size |
||
| 195 | * @return \Aimeos\MW\Media\Image\Iface Self object for method chaining |
||
| 196 | */ |
||
| 197 | public function scale( int $width = null, int $height = null, int $fit = 0 ) : Iface |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the supported image mime types |
||
| 223 | * |
||
| 224 | * @param array|string $mimetypes Mime type or list of mime types to check against |
||
| 225 | * @return array List of supported mime types |
||
| 226 | */ |
||
| 227 | public static function supports( $mimetypes = [] ) : array |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Resizes and crops the image if necessary |
||
| 249 | * |
||
| 250 | * @param int $scaleWidth Width of the image before cropping |
||
| 251 | * @param int $scaleHeight Height of the image before cropping |
||
| 252 | * @param int|null $width New width of the image |
||
| 253 | * @param int|null $height New height of the image |
||
| 254 | * @param bool $fit True to keep the width/height ratio of the image |
||
| 255 | * @return \Aimeos\MW\Media\Image\Iface Resized media object |
||
| 256 | */ |
||
| 257 | protected function resize( int $scaleWidth, int $scaleHeight, int $width = null, int $height = null, bool $fit = true ) : Iface |
||
| 258 | { |
||
| 259 | if( !( $width || $height ) ) { |
||
| 260 | return $this; |
||
| 261 | } |
||
| 262 | |||
| 263 | if( ( $result = imagescale( $this->image, $scaleWidth, $scaleHeight ) ) === false ) { |
||
| 264 | throw new \Aimeos\MW\Media\Exception( 'Unable to scale image' ); |
||
| 265 | } |
||
| 266 | |||
| 267 | $newMedia = clone $this; |
||
| 268 | |||
| 269 | $width = ( $width ?: $scaleWidth ); |
||
| 270 | $height = ( $height ?: $scaleHeight ); |
||
| 271 | |||
| 272 | $x0 = (int) ( $width / 2 - $scaleWidth / 2 ); |
||
| 273 | $y0 = (int) ( $height / 2 - $scaleHeight / 2 ); |
||
| 274 | |||
| 275 | if( $fit === false && ( $x0 || $y0 ) ) |
||
| 276 | { |
||
| 277 | if( ( $newImage = imagecreatetruecolor( $width, $height ) ) === false ) { |
||
| 278 | throw new \Aimeos\MW\Media\Exception( 'Unable to create new image' ); |
||
| 279 | } |
||
| 280 | |||
| 281 | imagesavealpha( $newImage, true ); |
||
| 282 | $alpha = in_array( $this->mimetype, ['image/gif', 'image/png'] ) ? 127 : 0; |
||
| 283 | |||
| 284 | if( ( $bg = imagecolorallocatealpha( $newImage, 255, 255, 255, $alpha ) ) === false ) { |
||
| 285 | throw new \Aimeos\MW\Media\Exception( 'Unable to allocate color' ); |
||
| 286 | } |
||
| 287 | |||
| 288 | if( imagefill( $newImage, 0, 0, $bg ) === false ) { |
||
| 289 | throw new \Aimeos\MW\Media\Exception( 'Unable to fill background' ); |
||
| 290 | } |
||
| 291 | |||
| 292 | if( imagecopy( $newImage, $result, $x0, $y0, 0, 0, $scaleWidth, $scaleHeight ) === false ) { |
||
| 293 | throw new \Aimeos\MW\Media\Exception( 'Unable to crop image' ); |
||
| 294 | } |
||
| 295 | |||
| 296 | imagedestroy( $result ); |
||
| 297 | $newMedia->image = $newImage; |
||
| 298 | } |
||
| 299 | else |
||
| 300 | { |
||
| 301 | $newMedia->image = $result; |
||
| 302 | } |
||
| 303 | |||
| 304 | return $newMedia; |
||
| 305 | } |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Adds the configured water mark to the image |
||
| 310 | */ |
||
| 311 | protected function watermark() |
||
| 332 | } |
||
| 333 | } |
||
| 334 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: