| Conditions | 14 |
| Paths | 3 |
| Total Lines | 75 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 69 | public function resizeOrCropImageToSizes( $sourceImage, $destinationFolder, array $sizes, array $settings = [] ) |
||
| 70 | { |
||
| 71 | $resizedImages = []; |
||
| 72 | |||
| 73 | $sourceImage = get_path_to( $sourceImage ); |
||
| 74 | if ( $this->disk->exists( $sourceImage ) ) |
||
| 75 | { |
||
| 76 | // Now we have to do it like this because the intervention library needs the full path. |
||
| 77 | $fullPathToImage = get_path_to( $this->diskBasePath, $sourceImage ); |
||
| 78 | $image = $this->intervention->make( $fullPathToImage ); |
||
| 79 | |||
| 80 | if ( $image ) |
||
| 81 | { |
||
| 82 | $settings = $this->parseSettings( $settings ); |
||
| 83 | |||
| 84 | foreach( $sizes as $index => $size ) |
||
| 85 | { |
||
| 86 | $workWithThisImage = clone $image; |
||
| 87 | |||
| 88 | $sizesArray = explode( 'x', $size ); |
||
| 89 | |||
| 90 | if ( count( $sizesArray ) == 2 ) |
||
| 91 | { |
||
| 92 | $width = $sizesArray[0]; |
||
| 93 | $height = $sizesArray[1]; |
||
| 94 | |||
| 95 | $width = $width ? $width : NULL; |
||
| 96 | $height = $height ? $height : NULL; |
||
| 97 | |||
| 98 | $crop = @$settings['crop']; |
||
| 99 | $maintainAspectRatio = @$settings['maintain_aspect_ratio']; |
||
| 100 | $preventUpsizing = @$settings['prevent_upsizing']; |
||
| 101 | |||
| 102 | if ( !$crop ) |
||
| 103 | { |
||
| 104 | $workWithThisImage->resize( $width, $height, function ($constraint) use ( $maintainAspectRatio, $preventUpsizing ) |
||
| 105 | { |
||
| 106 | if ( $maintainAspectRatio ) $constraint->aspectRatio(); |
||
| 107 | if ( $preventUpsizing ) $constraint->upsize(); |
||
| 108 | }); |
||
| 109 | } else |
||
| 110 | { |
||
| 111 | $workWithThisImage->fit( $width, $height, function ($constraint) use ( $preventUpsizing ) { |
||
| 112 | if ( $preventUpsizing ) $constraint->upsize(); |
||
| 113 | }); |
||
| 114 | } |
||
| 115 | |||
| 116 | $folderName = '_'.$size; |
||
| 117 | |||
| 118 | $pathInfo = pathinfo( $sourceImage ); |
||
| 119 | $filename = @$pathInfo['filename']; |
||
| 120 | |||
| 121 | $extension = @$settings['extension']; |
||
| 122 | $extension = is_null( $extension ) ? $pathInfo['extension'] : $extension; |
||
| 123 | $quality = (int)@$settings['quality']; |
||
| 124 | |||
| 125 | $pathToFolder = get_path_to( $destinationFolder, $folderName ); |
||
| 126 | |||
| 127 | if ( !$this->disk->exists( $pathToFolder ) ) $this->disk->makeDirectory( $pathToFolder, 0775 ); |
||
| 128 | |||
| 129 | // Once again: intervention is not able to save the image if we don't provide the full path. |
||
| 130 | $finalFileName = $filename . '.' . $extension; |
||
| 131 | $finalPathToProcessedFile = get_path_to( $pathToFolder, $finalFileName ); |
||
| 132 | $finalFullPathToProcessedFile = get_path_to( $this->diskBasePath, $finalPathToProcessedFile ); |
||
| 133 | |||
| 134 | if ( !$this->disk->exists( $finalPathToProcessedFile ) ) $workWithThisImage->save( $finalFullPathToProcessedFile, $quality); |
||
| 135 | |||
| 136 | // Return the base path. |
||
| 137 | $resizedImages[ $folderName ] = $finalPathToProcessedFile; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $resizedImages; |
||
| 144 | } |
||
| 163 |