11
11
use Illuminate \Support \Str ;
12
12
use Intervention \Image \Constraint ;
13
13
use Intervention \Image \ImageManager ;
14
+ use le0daniel \Laravel \ImageEngine \Contract \ImageManipulator ;
15
+ use le0daniel \Laravel \ImageEngine \Image \Manipulator \Fit ;
16
+ use le0daniel \Laravel \ImageEngine \Image \Manipulator \Resize ;
14
17
use le0daniel \Laravel \ImageEngine \Utility \Directories ;
15
18
use le0daniel \Laravel \ImageEngine \Utility \Images ;
16
19
use le0daniel \Laravel \ImageEngine \Utility \Signatures ;
@@ -20,16 +23,13 @@ final class ImageEngine
20
23
{
21
24
private const IMAGE_RESIZE_DIMENSIONS_TO_FIT = 3000 ;
22
25
23
- private array $ sizes ;
24
- private array $ filters = [];
25
26
private string $ secret ;
26
27
private string $ tmpPath ;
27
28
private ImageManager $ imageManager ;
28
29
29
30
public function __construct (ImageManager $ imageManager )
30
31
{
31
32
$ this ->imageManager = $ imageManager ;
32
- $ this ->sizes = config ('image-engine.sizes ' );
33
33
$ this ->secret = config ('image-engine.key ' );
34
34
$ this ->tmpPath = rtrim (config ('image-engine.tmp_directory ' ), '/ ' );
35
35
}
@@ -43,51 +43,21 @@ private function getBasePath(ImageRepresentation $imageRepresentation, bool $for
43
43
return rtrim ($ basePath , '/ ' );
44
44
}
45
45
46
- /**
47
- * @param string $size
48
- * @return array
49
- * @throws ImageException
50
- */
51
- private function getSizeArray (string $ size ): array
46
+ private function getConfiguration (string $ size ): array
52
47
{
53
- if (!array_key_exists ($ size , $ this ->sizes )) {
54
- throw ImageException::withHint (
55
- 'Invalid size. ' ,
56
- "Given {$ size }, available: " . implode (', ' , array_keys ($ this ->sizes ))
57
- );
48
+ $ configuration = config ('image-engine.sizes ' , [])[$ size ] ?? null ;
49
+ if (!$ configuration ) {
50
+ throw ImageException::withHint ("Invalid size given: {$ size }" , 'Please provide a configured size. ' );
58
51
}
59
52
60
53
return [
61
- $ this ->sizes [$ size ][0 ],
62
- $ this ->sizes [$ size ][1 ],
63
- $ this ->sizes [$ size ][2 ] ?? false ,
54
+ $ configuration [$ size ][0 ],
55
+ $ configuration [$ size ][1 ],
56
+ $ configuration [$ size ][2 ] ?? false ,
57
+ $ configuration [$ size ]['manipulation ' ] ?? null ,
64
58
];
65
59
}
66
60
67
- private function applyFilters (\Intervention \Image \Image $ image , string $ size )
68
- {
69
- if (!array_key_exists ($ size , $ this ->filters )) {
70
- return ;
71
- }
72
-
73
- /* Loop through filters */
74
- foreach ($ this ->filters [$ size ] as $ filter => $ params ) {
75
- /* No params given */
76
- if (is_numeric ($ filter )) {
77
- $ filter = $ params ;
78
- $ params = [];
79
- }
80
-
81
- /* Cast single params as array */
82
- if (!is_array ($ params )) {
83
- $ params = [$ params ];
84
- }
85
-
86
- /* Apply filter */
87
- $ image ->{$ filter }(...$ params );
88
- }
89
- }
90
-
91
61
private function getAbsoluteRenderPath (
92
62
ImageRepresentation $ imageRepresentation ,
93
63
string $ extension ,
@@ -132,45 +102,28 @@ public function render(
132
102
if (file_exists ($ absoluteRenderPath )) {
133
103
return $ absoluteRenderPath ;
134
104
}
135
-
136
- if (!file_exists (dirname ($ absoluteRenderPath ))) {
137
- Directories::makeRecursive (dirname ($ absoluteRenderPath ));
138
- }
105
+ Directories::makeRecursive (dirname ($ absoluteRenderPath ));
139
106
140
107
// Original requested
141
108
if (Images::isOriginalSize ($ imageRepresentation ->size )) {
142
109
copy (Images::realPath ($ imageRepresentation ), $ absoluteRenderPath );
143
110
return $ absoluteRenderPath ;
144
111
}
145
112
146
- [$ x , $ y , $ fit ] = $ this ->getSizeArray ($ imageRepresentation ->size );
113
+ [$ x , $ y , $ fit, $ userProvidedManipulators ] = $ this ->getConfiguration ($ imageRepresentation ->size );
147
114
148
- $ image = $ this
149
- ->imageManager
150
- ->make (Images::realPath ($ imageRepresentation ));
115
+ $ image = $ this ->imageManager ->make (Images::realPath ($ imageRepresentation ));
151
116
152
- /* Resize the image */
153
- if ($ fit ) {
154
- $ image ->fit (
155
- $ x ,
156
- $ y ,
157
- static function (Constraint $ constraint ) {
158
- $ constraint ->upsize ();
159
- }
160
- );
161
- } else {
162
- $ image ->resize (
163
- $ x ,
164
- $ y ,
165
- static function (Constraint $ constraint ) {
166
- $ constraint ->aspectRatio ();
167
- $ constraint ->upsize ();
168
- }
169
- );
117
+ $ manipulators = [
118
+ $ fit ? new Fit ($ x , $ y ) : new Resize ($ x , $ y ),
119
+ ];
120
+ if ($ userProvidedManipulators ) {
121
+ foreach ($ userProvidedManipulators as $ manipulator ) {
122
+ $ manipulators [] = new $ manipulator ;
123
+ }
170
124
}
171
125
172
- /* Add Filters */
173
- $ this ->applyFilters ($ image , $ imageRepresentation ->size );
126
+ array_walk ($ manipulators , fn (ImageManipulator $ manipulator ) => $ manipulator ->handle ($ image ));
174
127
175
128
/* Save */
176
129
$ image ->save ($ absoluteRenderPath );
0 commit comments