Skip to content

Commit bec08c7

Browse files
authored
Merge pull request #388 from thephpleague/3.x-cleanup
Cleanup code
2 parents bcb3168 + 1eb1ce5 commit bec08c7

File tree

9 files changed

+75
-112
lines changed

9 files changed

+75
-112
lines changed

src/Manipulators/Background.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ public function run(ImageInterface $image): ImageInterface
2525

2626
$color = (new Color($bg))->formatted();
2727

28-
if ($color) {
29-
$new = $image->driver()->createImage($image->width(), $image->height())
30-
->fill($color)
31-
->place($image, 'top-left', 0, 0)
32-
->setOrigin(
33-
new Origin($image->origin()->mediaType())
34-
);
35-
36-
$image = $new;
37-
}
38-
39-
return $image;
28+
return $image->driver()->createImage($image->width(), $image->height())
29+
->fill($color)
30+
->place($image, 'top-left', 0, 0)
31+
->setOrigin(
32+
new Origin($image->origin()->mediaType())
33+
);
4034
}
4135
}

src/Manipulators/Border.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,12 @@ class Border extends BaseManipulator
1818
*/
1919
public function run(ImageInterface $image): ImageInterface
2020
{
21-
if ($border = $this->getBorder($image)) {
22-
list($width, $color, $method) = $border;
21+
$border = $this->getBorder($image);
2322

24-
if ('overlay' === $method) {
25-
return $this->runOverlay($image, $width, $color);
26-
}
27-
28-
if ('shrink' === $method) {
29-
return $this->runShrink($image, $width, $color);
30-
}
23+
if ($border) {
24+
[$width, $color, $method] = $border;
3125

32-
if ('expand' === $method) {
33-
return $this->runExpand($image, $width, $color);
34-
}
26+
return $this->{'run'.$method}($image, $width, $color);
3527
}
3628

3729
return $image;
@@ -101,11 +93,11 @@ public function getColor(string $color): string
10193
*/
10294
public function getMethod(string $method): string
10395
{
104-
if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) {
105-
return 'overlay';
106-
}
107-
108-
return $method;
96+
return match ($method) {
97+
'expand' => 'expand',
98+
'shrink' => 'shrink',
99+
default => 'overlay',
100+
};
109101
}
110102

111103
/**

src/Manipulators/Filter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public function runGreyscaleFilter(ImageInterface $image): ImageInterface
4343
*/
4444
public function runSepiaFilter(ImageInterface $image): ImageInterface
4545
{
46-
$image->greyscale();
47-
$image->brightness(-10);
48-
$image->contrast(10);
49-
$image->colorize(38, 27, 12);
50-
$image->brightness(-10);
51-
$image->contrast(10);
46+
$image->greyscale()
47+
->brightness(-10)
48+
->contrast(10)
49+
->colorize(38, 27, 12)
50+
->brightness(-10)
51+
->contrast(10);
5252

5353
return $image;
5454
}

src/Manipulators/Flip.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ class Flip extends BaseManipulator
1616
public function run(ImageInterface $image): ImageInterface
1717
{
1818
$flip = $this->getFlip();
19-
if (null !== $flip) {
20-
if ('both' === $flip) {
21-
return $image->flip()->flop();
22-
}
23-
24-
if ('v' === $flip) {
25-
return $image->flip();
26-
}
2719

28-
if ('h' === $flip) {
29-
return $image->flop();
30-
}
20+
if (null !== $flip) {
21+
return match ($flip) {
22+
'both' => $image->flip()->flop(),
23+
'v' => $image->flip(),
24+
'h' => $image->flop(),
25+
default => $image,
26+
};
3127
}
3228

3329
return $image;

src/Manipulators/Orientation.php

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,18 @@ class Orientation extends BaseManipulator
1616
public function run(ImageInterface $image): ImageInterface
1717
{
1818
$orientation = $this->getOrientation();
19-
$originalOrientation = $image->exif('Orientation');
2019

21-
if ('auto' === $orientation && is_numeric($originalOrientation)) {
22-
switch ($originalOrientation) {
23-
case 2:
24-
$image->flip();
25-
break;
26-
case 3:
27-
$image->rotate(180);
28-
break;
29-
case 4:
30-
$image->rotate(180)->flip();
31-
break;
32-
case 5:
33-
$image->rotate(270)->flip();
34-
break;
35-
case 6:
36-
$image->rotate(270);
37-
break;
38-
case 7:
39-
$image->rotate(90)->flip();
40-
break;
41-
case 8:
42-
$image->rotate(90);
43-
break;
44-
}
45-
46-
return $image;
20+
if ('auto' === $orientation) {
21+
return match ($image->exif('Orientation')) {
22+
2 => $image->flip(),
23+
3 => $image->rotate(180),
24+
4 => $image->rotate(180)->flip(),
25+
5 => $image->rotate(270)->flip(),
26+
6 => $image->rotate(270),
27+
7 => $image->rotate(90)->flip(),
28+
8 => $image->rotate(90),
29+
default => $image,
30+
};
4731
}
4832

4933
return $image->rotate((float) $orientation);
@@ -56,9 +40,9 @@ public function run(ImageInterface $image): ImageInterface
5640
*/
5741
public function getOrientation(): string
5842
{
59-
$or = $this->getParam('or');
43+
$or = (string) $this->getParam('or');
6044

61-
if (in_array($or, ['auto', '0', '90', '180', '270'], true)) {
45+
if (in_array($or, ['0', '90', '180', '270'], true)) {
6246
return $or;
6347
}
6448

src/Manipulators/Size.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function run(ImageInterface $image): ImageInterface
5656
$fit = $this->getFit();
5757
$dpr = $this->getDpr();
5858

59-
list($width, $height) = $this->resolveMissingDimensions($image, $width, $height);
60-
list($width, $height) = $this->applyDpr($width, $height, $dpr);
61-
list($width, $height) = $this->limitImageSize($width, $height);
59+
[$width, $height] = $this->resolveMissingDimensions($image, $width, $height);
60+
[$width, $height] = $this->applyDpr($width, $height, $dpr);
61+
[$width, $height] = $this->limitImageSize($width, $height);
6262

6363
if ($width !== $image->width() || $height !== $image->height() || 1.0 !== $this->getCrop()[2]) {
6464
$image = $this->runResize($image, $fit, $width, $height);
@@ -326,13 +326,13 @@ public function runStretchResize(ImageInterface $image, int $width, int $height)
326326
*/
327327
public function runCropResize(ImageInterface $image, int $width, int $height): ImageInterface
328328
{
329-
list($resize_width, $resize_height) = $this->resolveCropResizeDimensions($image, $width, $height);
329+
[$resize_width, $resize_height] = $this->resolveCropResizeDimensions($image, $width, $height);
330330

331331
$zoom = $this->getCrop()[2];
332332

333333
$image->scale((int) round($resize_width * $zoom), (int) round($resize_height * $zoom));
334334

335-
list($offset_x, $offset_y) = $this->resolveCropOffset($image, $width, $height);
335+
[$offset_x, $offset_y] = $this->resolveCropOffset($image, $width, $height);
336336

337337
return $image->crop($width, $height, $offset_x, $offset_y);
338338
}
@@ -366,7 +366,7 @@ public function resolveCropResizeDimensions(ImageInterface $image, int $width, i
366366
*/
367367
public function resolveCropOffset(ImageInterface $image, int $width, int $height): array
368368
{
369-
list($offset_percentage_x, $offset_percentage_y) = $this->getCrop();
369+
[$offset_percentage_x, $offset_percentage_y] = $this->getCrop();
370370

371371
$offset_x = (int) (($image->width() * $offset_percentage_x / 100) - ($width / 2));
372372
$offset_y = (int) (($image->height() * $offset_percentage_y / 100) - ($height / 2));

src/Manipulators/Watermark.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,34 @@ public function getWatermarksPathPrefix(): string
8080
*/
8181
public function run(ImageInterface $image): ImageInterface
8282
{
83-
if ($watermark = $this->getImage($image)) {
84-
$markw = $this->getDimension($image, 'markw');
85-
$markh = $this->getDimension($image, 'markh');
86-
$markx = $this->getDimension($image, 'markx');
87-
$marky = $this->getDimension($image, 'marky');
88-
$markpad = $this->getDimension($image, 'markpad');
89-
$markfit = $this->getFit();
90-
$markpos = $this->getPosition();
91-
$markalpha = $this->getAlpha();
92-
93-
if (null !== $markpad) {
94-
$markx = $marky = $markpad;
95-
}
83+
$watermark = $this->getImage($image);
9684

97-
$size = new Size();
98-
$size->setParams([
99-
'w' => $markw,
100-
'h' => $markh,
101-
'fit' => $markfit,
102-
]);
103-
$watermark = $size->run($watermark);
85+
if (null === $watermark) {
86+
return $image;
87+
}
10488

105-
$image->place($watermark, $markpos, intval($markx), intval($marky), $markalpha);
89+
$markw = $this->getDimension($image, 'markw');
90+
$markh = $this->getDimension($image, 'markh');
91+
$markx = $this->getDimension($image, 'markx');
92+
$marky = $this->getDimension($image, 'marky');
93+
$markpad = $this->getDimension($image, 'markpad');
94+
$markfit = $this->getFit();
95+
$markpos = $this->getPosition();
96+
$markalpha = $this->getAlpha();
97+
98+
if (null !== $markpad) {
99+
$markx = $marky = $markpad;
106100
}
107101

108-
return $image;
102+
$size = new Size();
103+
$size->setParams([
104+
'w' => $markw,
105+
'h' => $markh,
106+
'fit' => $markfit,
107+
]);
108+
$watermark = $size->run($watermark);
109+
110+
return $image->place($watermark, $markpos, intval($markx), intval($marky), $markalpha);
109111
}
110112

111113
/**
@@ -117,7 +119,7 @@ public function run(ImageInterface $image): ImageInterface
117119
*/
118120
public function getImage(ImageInterface $image): ?ImageInterface
119121
{
120-
if (is_null($this->watermarks)) {
122+
if (null === $this->watermarks) {
121123
return null;
122124
}
123125

src/Responses/PsrResponseFactory.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ class PsrResponseFactory implements ResponseFactoryInterface
99
{
1010
/**
1111
* Base response object.
12-
*
13-
* @var ResponseInterface
1412
*/
15-
protected $response;
13+
protected ResponseInterface $response;
1614

1715
/**
1816
* Callback to create stream.
19-
*
20-
* @var \Closure
2117
*/
22-
protected $streamCallback;
18+
protected \Closure $streamCallback;
2319

2420
/**
2521
* Create PsrResponseFactory instance.

tests/Manipulators/OrientationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ public function testCreateInstance()
2727
public function testRun()
2828
{
2929
$image = \Mockery::mock(ImageInterface::class, function ($mock) {
30-
$mock->shouldReceive('exif')->withArgs(['Orientation'])->andReturn(null)->twice();
30+
$mock->shouldReceive('exif')->withArgs(['Orientation'])->andReturn(null)->once();
3131

32-
$mock->shouldReceive('rotate')->andReturn($mock)->with('0')->once();
3332
$mock->shouldReceive('rotate')->andReturn($mock)->with('90')->once();
3433
});
3534

0 commit comments

Comments
 (0)