| Conditions | 14 |
| Paths | 3 |
| Total Lines | 76 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 70 | public function resizeOrCropImageToSizes( $sourceImage, $destinationFolder, array $sizes, array $settings = [] ) |
||
| 71 | { |
||
| 72 | $resizedImages = []; |
||
| 73 | |||
| 74 | $sourceImage = get_path_to( $sourceImage ); |
||
| 75 | if ( $this->disk->exists( $sourceImage ) ) |
||
| 76 | { |
||
| 77 | // Now we have to do it like this because the intervention library needs the full path. |
||
| 78 | $fullPathToImage = get_path_to( $this->diskBasePath, $sourceImage ); |
||
| 79 | $image = $this->intervention->make( $fullPathToImage ); |
||
| 80 | |||
| 81 | if ( $image ) |
||
| 82 | { |
||
| 83 | $image->orientate(); |
||
| 84 | $settings = $this->parseSettings( $settings ); |
||
| 85 | |||
| 86 | foreach( $sizes as $index => $size ) |
||
| 87 | { |
||
| 88 | $workWithThisImage = clone $image; |
||
| 89 | |||
| 90 | $sizesArray = explode( 'x', $size ); |
||
| 91 | |||
| 92 | if ( count( $sizesArray ) == 2 ) |
||
| 93 | { |
||
| 94 | $width = $sizesArray[0]; |
||
| 95 | $height = $sizesArray[1]; |
||
| 96 | |||
| 97 | $width = $width ? $width : NULL; |
||
| 98 | $height = $height ? $height : NULL; |
||
| 99 | |||
| 100 | $crop = @$settings['crop']; |
||
| 101 | $maintainAspectRatio = @$settings['maintain_aspect_ratio']; |
||
| 102 | $preventUpsizing = @$settings['prevent_upsizing']; |
||
| 103 | |||
| 104 | if ( !$crop ) |
||
| 105 | { |
||
| 106 | $workWithThisImage->resize( $width, $height, function ($constraint) use ( $maintainAspectRatio, $preventUpsizing ) |
||
| 107 | { |
||
| 108 | if ( $maintainAspectRatio ) $constraint->aspectRatio(); |
||
| 109 | if ( $preventUpsizing ) $constraint->upsize(); |
||
| 110 | }); |
||
| 111 | } else |
||
| 112 | { |
||
| 113 | $workWithThisImage->fit( $width, $height, function ($constraint) use ( $preventUpsizing ) { |
||
| 114 | if ( $preventUpsizing ) $constraint->upsize(); |
||
| 115 | }); |
||
| 116 | } |
||
| 117 | |||
| 118 | $folderName = '_'.$size; |
||
| 119 | |||
| 120 | $pathInfo = pathinfo( $sourceImage ); |
||
| 121 | $filename = @$pathInfo['filename']; |
||
| 122 | |||
| 123 | $extension = @$settings['extension']; |
||
| 124 | $extension = is_null( $extension ) ? $pathInfo['extension'] : $extension; |
||
| 125 | $quality = (int)@$settings['quality']; |
||
| 126 | |||
| 127 | $pathToFolder = get_path_to( $destinationFolder, $folderName ); |
||
| 128 | |||
| 129 | if ( !$this->disk->exists( $pathToFolder ) ) $this->disk->makeDirectory( $pathToFolder, 0775 ); |
||
| 130 | |||
| 131 | // Once again: intervention is not able to save the image if we don't provide the full path. |
||
| 132 | $finalFileName = $filename . '.' . $extension; |
||
| 133 | $finalPathToProcessedFile = get_path_to( $pathToFolder, $finalFileName ); |
||
| 134 | $finalFullPathToProcessedFile = get_path_to( $this->diskBasePath, $finalPathToProcessedFile ); |
||
| 135 | |||
| 136 | if ( !$this->disk->exists( $finalPathToProcessedFile ) ) $workWithThisImage->save( $finalFullPathToProcessedFile, $quality); |
||
| 137 | |||
| 138 | // Return the base path. |
||
| 139 | $resizedImages[ $folderName ] = $finalPathToProcessedFile; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return $resizedImages; |
||
| 146 | } |
||
| 165 |