Passed
Push — master ( 1120e7...dcb672 )
by Jesús
02:28
created

ImageProcessingService::resizeOrCropImageToSizes()   C

Complexity

Conditions 14
Paths 3

Size

Total Lines 75
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 39
nc 3
nop 4
dl 0
loc 75
rs 5.3217
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace OkayBueno\Images\Services\src;
4
5
use OkayBueno\Images\Services\ImageProcessingServiceInterface;
6
use Illuminate\Support\Facades\Storage;
7
use Intervention\Image\ImageManager;
8
9
/**
10
 * Class ImageProcessingService
11
 * @package OkayBueno\Images\Services\src
12
 */
13
class ImageProcessingService implements ImageProcessingServiceInterface
14
{
15
16
    protected $intervention;
17
    protected $disk;
18
    protected $diskBasePath;
19
    protected $availableSettings;
20
21
22
    /**
23
     * @param ImageManager $imageManager
24
     */
25
    public function __construct( ImageManager $imageManager )
26
    {
27
        $this->intervention = $imageManager;
28
        $this->disk = Storage::disk( config( 'images.local_disk_name', 'local' ) );
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->disk = Storage::disk( /** @scrutinizer ignore-call */ config( 'images.local_disk_name', 'local' ) );
Loading history...
29
        $this->diskBasePath = trim( $this->disk->getAdapter()->getPathPrefix(), '/' );
30
        $this->availableSettings = config( 'images.processing_settings' );
31
    }
32
33
34
    /**
35
     * @param $imageDataOrURL
36
     * @param $destinationFolder
37
     * @return bool|string
38
     */
39
    public function createImageFromImageDataOrURL( $imageDataOrURL, $destinationFolder )
40
    {
41
        $filename = md5( $imageDataOrURL  ) . '.' . $this->availableSettings['extension'];
42
        $image = $this->intervention->make( $imageDataOrURL );
43
44
        $pathToFolderInDisk = get_path_to( $destinationFolder );
45
        $pathToFile = get_path_to( $destinationFolder, $filename );
46
47
        if ( !$this->disk->exists( $pathToFolderInDisk ) ) $this->disk->makeDirectory( $pathToFolderInDisk, 0775);
48
49
        try
50
        {
51
            $fullPathToImage = get_path_to( $this->diskBasePath, $destinationFolder, $filename );
52
            $result = $image->save( $fullPathToImage, 100 );
53
        } catch( \Exception $e )
54
        {
55
            $result = FALSE;
56
        }
57
58
        return $result ? $pathToFile : FALSE;
59
    }
60
61
62
    /**
63
     * @param $sourceImage
64
     * @param $destinationFolder
65
     * @param array $sizes
66
     * @param array $settings
67
     * @return array
68
     */
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
    }
145
146
147
    /**
148
     * @param array $extraSettings
149
     * @return array
150
     */
151
    protected function parseSettings( array $extraSettings )
152
    {
153
        $parsedSettings = [];
154
        foreach( $this->availableSettings as $availableSetting => $defaultValue )
155
        {
156
            if ( array_key_exists( $availableSetting, $extraSettings ) ) $parsedSettings[ $availableSetting ] = $extraSettings[ $availableSetting ];
157
            else $parsedSettings[ $availableSetting ] = $defaultValue;
158
        }
159
160
        return $parsedSettings;
161
    }
162
}
163