1616
1717namespace PhpCollective \Infrastructure \Storage \Processor \Image ;
1818
19- use Intervention \Image \Image ;
19+ use Intervention \Image \Interfaces \ ImageInterface ;
2020use InvalidArgumentException ;
2121use PhpCollective \Infrastructure \Storage \Processor \Image \Exception \UnsupportedOperationException ;
2222
2323/**
2424 * Operations
25+ *
26+ * @link https://image.intervention.io/v3
2527 */
2628class Operations
2729{
30+ public const POSITION_CENTER = 'center ' ;
31+ public const POSITION_TOP_CENTER = 'top-center ' ;
32+ public const POSITION_BOTTOM_CENTER = 'bottom-center ' ;
33+ public const POSITION_LEFT_TOP = 'left-top ' ;
34+ public const POSITION_RIGHT_TOP = 'right-top ' ;
35+ public const POSITION_LEFT_CENTER = 'left-center ' ;
36+ public const POSITION_RIGHT_CENTER = 'right-center ' ;
37+ public const POSITION_LEFT_BOTTOM = 'left-bottom ' ;
38+ public const POSITION_RIGHT_BOTTOM = 'right-bottom ' ;
39+
2840 /**
29- * @var \Intervention\Image\Image
41+ * @var \Intervention\Image\Interfaces\ImageInterface
3042 */
31- protected Image $ image ;
43+ protected ImageInterface $ image ;
3244
3345 /**
34- * @param \Intervention\Image\Image $image Image
46+ * @param \Intervention\Image\Interfaces\ImageInterface $image Image
3547 */
36- public function __construct (Image $ image )
48+ public function __construct (ImageInterface $ image )
3749 {
3850 $ this ->image = $ image ;
3951 }
@@ -51,34 +63,37 @@ public function __call(string $name, array $arguments): mixed
5163 /**
5264 * Crops the image
5365 *
54- * @link http://image.intervention.io/api/fit
5566 * @param array<string, mixed> $arguments Arguments
5667 * @return void
5768 */
58- public function fit (array $ arguments ): void
69+ public function cover (array $ arguments ): void
5970 {
60- if (!isset ($ arguments ['width ' ])) {
61- throw new InvalidArgumentException ('Missing width ' );
71+ if (!isset ($ arguments ['height ' ], $ arguments [ ' width ' ])) {
72+ throw new InvalidArgumentException ('Missing width or height ' );
6273 }
6374
75+ $ arguments += ['position ' => static ::POSITION_CENTER ];
6476 $ preventUpscale = $ arguments ['preventUpscale ' ] ?? false ;
65- $ height = $ arguments ['height ' ] ?? null ;
77+ if ($ preventUpscale ) {
78+ $ this ->image ->coverDown (
79+ (int )$ arguments ['width ' ],
80+ (int )$ arguments ['height ' ],
81+ $ arguments ['position ' ]
82+ );
6683
67- $ this ->image ->fit (
84+ return ;
85+ }
86+
87+ $ this ->image ->cover (
6888 (int )$ arguments ['width ' ],
69- (int )$ height ,
70- static function ($ constraint ) use ($ preventUpscale ) {
71- if ($ preventUpscale ) {
72- $ constraint ->upsize ();
73- }
74- }
89+ (int )$ arguments ['height ' ],
90+ $ arguments ['position ' ]
7591 );
7692 }
7793
7894 /**
7995 * Crops the image
8096 *
81- * @link http://image.intervention.io/api/crop
8297 * @param array<string, mixed> $arguments Arguments
8398 * @return void
8499 */
@@ -88,19 +103,18 @@ public function crop(array $arguments): void
88103 throw new InvalidArgumentException ('Missing width or height ' );
89104 }
90105
91- $ arguments = array_merge ( ['x ' => null , 'y ' => null ], $ arguments ) ;
106+ $ arguments += ['x ' => null , 'y ' => null , ' position ' => static :: POSITION_CENTER ] ;
92107 $ height = $ arguments ['height ' ] ? (int )$ arguments ['height ' ] : null ;
93108 $ width = $ arguments ['width ' ] ? (int )$ arguments ['width ' ] : null ;
94- $ x = $ arguments ['x ' ] ? (int )$ arguments ['x ' ] : null ;
95- $ y = $ arguments ['y ' ] ? (int )$ arguments ['y ' ] : null ;
109+ $ x = $ arguments ['x ' ] ? (int )$ arguments ['x ' ] : 0 ;
110+ $ y = $ arguments ['y ' ] ? (int )$ arguments ['y ' ] : 0 ;
96111
97- $ this ->image ->crop ($ width , $ height , $ x , $ y );
112+ $ this ->image ->crop ($ width , $ height , $ x , $ y, $ arguments [ ' position ' ] );
98113 }
99114
100115 /**
101116 * Flips the image horizontal
102117 *
103- * @link http://image.intervention.io/api/flip
104118 * @return void
105119 */
106120 public function flipHorizontal (): void
@@ -111,7 +125,6 @@ public function flipHorizontal(): void
111125 /**
112126 * Flips the image vertical
113127 *
114- * @link http://image.intervention.io/api/flip
115128 * @return void
116129 */
117130 public function flipVertical (): void
@@ -122,7 +135,6 @@ public function flipVertical(): void
122135 /**
123136 * Flips the image
124137 *
125- * @link http://image.intervention.io/api/flip
126138 * @param array<string, mixed> $arguments Arguments
127139 * @return void
128140 */
@@ -138,87 +150,85 @@ public function flip(array $arguments): void
138150 );
139151 }
140152
141- $ this ->image ->flip ($ arguments ['direction ' ]);
153+ if ($ arguments ['direction ' ] === 'h ' ) {
154+ $ this ->image ->flip ();
155+
156+ return ;
157+ }
158+
159+ $ this ->image ->flop ();
142160 }
143161
144162 /**
145- * Resizes the image
163+ * @param array<string, mixed> $arguments
146164 *
147- * @link http://image.intervention.io/api/resize
148- * @param array<string, mixed> $arguments Arguments
149165 * @return void
150166 */
151- public function resize (array $ arguments ): void
167+ public function scale (array $ arguments ): void
152168 {
153169 if (!isset ($ arguments ['height ' ], $ arguments ['width ' ])) {
154170 throw new InvalidArgumentException (
155171 'Missing height or width '
156172 );
157173 }
158174
159- $ aspectRatio = $ arguments ['aspectRatio ' ] ?? true ;
160175 $ preventUpscale = $ arguments ['preventUpscale ' ] ?? false ;
161176
162- $ this ->image ->resize (
177+ if ($ preventUpscale ) {
178+ $ this ->image ->scaleDown (
179+ $ arguments ['width ' ],
180+ $ arguments ['height ' ]
181+ );
182+
183+ return ;
184+ }
185+
186+ $ this ->image ->scale (
163187 $ arguments ['width ' ],
164188 $ arguments ['height ' ],
165- static function ($ constraint ) use ($ aspectRatio , $ preventUpscale ) {
166- if ($ aspectRatio ) {
167- $ constraint ->aspectRatio ();
168- }
169- if ($ preventUpscale ) {
170- $ constraint ->upsize ();
171- }
172- }
173189 );
174190 }
175191
176192 /**
177- * @link http://image.intervention.io/api/widen
193+ * Resizes the image
194+ *
178195 * @param array<string, mixed> $arguments Arguments
179196 * @return void
180197 */
181- public function widen (array $ arguments ): void
198+ public function resize (array $ arguments ): void
182199 {
183- if (!isset ($ arguments ['width ' ])) {
200+ if (!isset ($ arguments ['height ' ], $ arguments [ ' width ' ])) {
184201 throw new InvalidArgumentException (
185- 'Missing width '
202+ 'Missing height or width '
186203 );
187204 }
188205
189- $ preventUpscale = $ arguments ['preventUpscale ' ] ?? false ;
190-
191- $ this ->image ->widen ((int )$ arguments ['width ' ], function ($ constraint ) use ($ preventUpscale ) {
192- if ($ preventUpscale ) {
193- $ constraint ->upsize ();
194- }
195- });
196- }
206+ // Deprecated: Coming from old API
207+ $ aspectRatio = $ arguments ['aspectRatio ' ] ?? null ;
208+ if ($ aspectRatio !== null ) {
209+ $ this ->scale ($ arguments );
197210
198- /**
199- * @link http://image.intervention.io/api/heighten
200- * @param array<string, mixed> $arguments Arguments
201- * @return void
202- */
203- public function heighten (array $ arguments ): void
204- {
205- if (!isset ($ arguments ['height ' ])) {
206- throw new InvalidArgumentException (
207- 'Missing height '
208- );
211+ return ;
209212 }
210213
211214 $ preventUpscale = $ arguments ['preventUpscale ' ] ?? false ;
212215
213- $ this ->image ->heighten ((int )$ arguments ['height ' ], function ($ constraint ) use ($ preventUpscale ) {
214- if ($ preventUpscale ) {
215- $ constraint ->upsize ();
216- }
217- });
216+ if ($ preventUpscale ) {
217+ $ this ->image ->resizeDown (
218+ $ arguments ['width ' ],
219+ $ arguments ['height ' ]
220+ );
221+
222+ return ;
223+ }
224+
225+ $ this ->image ->resize (
226+ $ arguments ['width ' ],
227+ $ arguments ['height ' ],
228+ );
218229 }
219230
220231 /**
221- * @link http://image.intervention.io/api/rotate
222232 * @param array<string, mixed> $arguments Arguments
223233 * @return void
224234 */
@@ -234,8 +244,6 @@ public function rotate(array $arguments): void
234244 }
235245
236246 /**
237- * @link http://image.intervention.io/api/rotate
238- * @param array<string, mixed> $arguments Arguments
239247 * @return void
240248 */
241249 public function sharpen (array $ arguments ): void
0 commit comments