ImageProcessingService   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
c 2
b 0
f 0
dl 0
loc 150
rs 10
wmc 22

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createImageFromImageDataOrURL() 0 21 4
C resizeOrCropImageToSizes() 0 76 14
A __construct() 0 6 1
A parseSettings() 0 10 3
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, array $settings = [] )
40
    {
41
        $settings = $this->parseSettings( $settings );
42
        $filename = md5( $imageDataOrURL  ) . '.' . $settings['extension'];
43
        $image = $this->intervention->make( $imageDataOrURL );
44
45
        $pathToFolderInDisk = get_path_to( $destinationFolder );
46
        $pathToFile = get_path_to( $destinationFolder, $filename );
47
48
        if ( !$this->disk->exists( $pathToFolderInDisk ) ) $this->disk->makeDirectory( $pathToFolderInDisk, 0775);
49
50
        try
51
        {
52
            $fullPathToImage = get_path_to( $this->diskBasePath, $destinationFolder, $filename );
53
            $result = $image->save( $fullPathToImage, 100 );
54
        } catch( \Exception $e )
55
        {
56
            $result = FALSE;
57
        }
58
59
        return $result ? $pathToFile : FALSE;
60
    }
61
62
63
    /**
64
     * @param $sourceImage
65
     * @param $destinationFolder
66
     * @param array $sizes
67
     * @param array $settings
68
     * @return array
69
     */
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 )
0 ignored issues
show
introduced by
$image is of type Intervention\Image\Image, thus it always evaluated to true.
Loading history...
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 )
0 ignored issues
show
Bug introduced by
It seems like $height can also be of type string; however, parameter $height of Intervention\Image\Image::resize() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

106
                            $workWithThisImage->resize( $width, /** @scrutinizer ignore-type */ $height, function ($constraint) use ( $maintainAspectRatio, $preventUpsizing )
Loading history...
Bug introduced by
It seems like $width can also be of type string; however, parameter $width of Intervention\Image\Image::resize() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

106
                            $workWithThisImage->resize( /** @scrutinizer ignore-type */ $width, $height, function ($constraint) use ( $maintainAspectRatio, $preventUpsizing )
Loading history...
107
                            {
108
                                if ( $maintainAspectRatio ) $constraint->aspectRatio();
109
                                if ( $preventUpsizing ) $constraint->upsize();
110
                            });
111
                        } else
112
                        {
113
                            $workWithThisImage->fit( $width, $height, function ($constraint) use ( $preventUpsizing ) {
0 ignored issues
show
Bug introduced by
It seems like $width can also be of type string; however, parameter $width of Intervention\Image\Image::fit() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

113
                            $workWithThisImage->fit( /** @scrutinizer ignore-type */ $width, $height, function ($constraint) use ( $preventUpsizing ) {
Loading history...
Bug introduced by
It seems like $height can also be of type string; however, parameter $height of Intervention\Image\Image::fit() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

113
                            $workWithThisImage->fit( $width, /** @scrutinizer ignore-type */ $height, function ($constraint) use ( $preventUpsizing ) {
Loading history...
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
    }
147
148
149
    /**
150
     * @param array $extraSettings
151
     * @return array
152
     */
153
    protected function parseSettings( array $extraSettings )
154
    {
155
        $parsedSettings = [];
156
        foreach( $this->availableSettings as $availableSetting => $defaultValue )
157
        {
158
            if ( array_key_exists( $availableSetting, $extraSettings ) ) $parsedSettings[ $availableSetting ] = $extraSettings[ $availableSetting ];
159
            else $parsedSettings[ $availableSetting ] = $defaultValue;
160
        }
161
162
        return $parsedSettings;
163
    }
164
}
165