Skip to content

Commit 2d77e1a

Browse files
committed
Try to upgrade to intervention/image v3
1 parent bf7c85c commit 2d77e1a

File tree

9 files changed

+1054
-136
lines changed

9 files changed

+1054
-136
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
],
1717
"require": {
1818
"php": ">=8.1",
19-
"intervention/image": "^2.5",
20-
"spatie/image-optimizer": "^1.2",
19+
"intervention/image": "^3.2.0",
20+
"spatie/image-optimizer": "^1.2.0",
2121
"ext-json": "*"
2222
},
2323
"require-dev": {
@@ -43,7 +43,8 @@
4343
},
4444
"autoload-dev": {
4545
"psr-4": {
46-
"PhpCollective\\Test\\": "tests/"
46+
"PhpCollective\\Test\\": "tests/",
47+
"TestApp\\": "tests/test_app/src/"
4748
}
4849
},
4950
"scripts": {

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ parameters:
33
paths:
44
- src/
55
checkGenericClassInNonGenericObjectType: false
6+
checkMissingIterableValueType: false

src/ImageProcessor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace PhpCollective\Infrastructure\Storage\Processor\Image;
1818

1919
use GuzzleHttp\Psr7\StreamWrapper;
20+
use Intervention\Image\Interfaces\ImageInterface;
2021
use InvalidArgumentException;
2122
use Intervention\Image\Image;
2223
use Intervention\Image\ImageManager;
@@ -74,9 +75,9 @@ class ImageProcessor implements ProcessorInterface
7475
protected ImageManager $imageManager;
7576

7677
/**
77-
* @var \Intervention\Image\Image
78+
* @var \Intervention\Image\Interfaces\ImageInterface
7879
*/
79-
protected Image $image;
80+
protected ImageInterface $image;
8081

8182
/**
8283
* Quality setting for writing images
@@ -239,7 +240,7 @@ public function process(FileInterface $file): FileInterface
239240
continue;
240241
}
241242

242-
$this->image = $this->imageManager->make($tempFile);
243+
$this->image = $this->imageManager->read($tempFile);
243244
$operations = new Operations($this->image);
244245

245246
// Apply the operations
@@ -285,11 +286,12 @@ protected function optimizeAndStore(FileInterface $file, string $path): void
285286

286287
// We need more tmp files because the optimizer likes to write
287288
// and read the files from disk, not from a stream. :(
289+
//FIXME Use memory/stream instead?
288290
$optimizerTempFile = TemporaryFile::create();
289291
$optimizerOutput = TemporaryFile::create();
290292

291293
// Save the image to the tmp file
292-
$this->image->save($optimizerTempFile, 90, $file->extension());
294+
$this->image->save($optimizerTempFile, 90);
293295
// Optimize it and write it to another file
294296
$this->optimizer()->optimize($optimizerTempFile, $optimizerOutput);
295297
// Open a new stream for the storage system

src/ImageVariant.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,16 @@ public function callback(callable $callback): self
223223
}
224224

225225
/**
226-
* @link http://image.intervention.io/api/fit
227226
* @param int $width Width
228-
* @param int|null $height Height
227+
* @param null $height Height
229228
* @param callable|null $callback Callback
230229
* @param bool $preventUpscale Prevent Upscaling
231230
* @param string $position Position
232231
* @return $this
233232
*/
234-
public function fit(
233+
public function cover(
235234
int $width,
236-
?int $height = null,
235+
int $height,
237236
?callable $callback = null,
238237
bool $preventUpscale = false,
239238
string $position = 'center'

src/Operations.php

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,36 @@
1616

1717
namespace PhpCollective\Infrastructure\Storage\Processor\Image;
1818

19-
use Intervention\Image\Image;
19+
use Intervention\Image\Interfaces\ImageInterface;
2020
use InvalidArgumentException;
2121
use PhpCollective\Infrastructure\Storage\Processor\Image\Exception\UnsupportedOperationException;
2222

2323
/**
2424
* Operations
25+
*
26+
* @link https://image.intervention.io/v3
2527
*/
2628
class 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

tests/TestCase/Processor/Image/ImageProcessorTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace PhpCollective\Test\TestCase\Processor\Image;
1818

19-
use Intervention\Image\Image;
19+
use Intervention\Image\Drivers\Gd\Driver;
2020
use Intervention\Image\ImageManager;
2121
use PhpCollective\Infrastructure\Storage\File;
2222
use PhpCollective\Infrastructure\Storage\FileFactory;
@@ -25,6 +25,7 @@
2525
use PhpCollective\Infrastructure\Storage\Processor\Image\ImageVariantCollection;
2626
use PhpCollective\Infrastructure\Storage\Processor\Image\ImageProcessor;
2727
use PhpCollective\Test\TestCase\TestCase;
28+
use TestApp\Image;
2829

2930
/**
3031
* ImageProcessorTest
@@ -41,15 +42,7 @@ public function testProcessor(): void
4142

4243
$pathBuilder = new PathBuilder();
4344

44-
$imageManager = $this->getMockBuilder(ImageManager::class)
45-
->getMock();
46-
47-
$image = $this->getMockBuilder(Image::class)
48-
->getMock();
49-
50-
$imageManager->expects($this->any())
51-
->method('make')
52-
->willReturn($image);
45+
$imageManager = new ImageManager(new Driver());
5346

5447
$processor = new ImageProcessor(
5548
$fileStorage,

0 commit comments

Comments
 (0)