diff --git a/src/Manipulators/Background.php b/src/Manipulators/Background.php index 055f36d..75ad6d8 100644 --- a/src/Manipulators/Background.php +++ b/src/Manipulators/Background.php @@ -25,17 +25,11 @@ public function run(ImageInterface $image): ImageInterface $color = (new Color($bg))->formatted(); - if ($color) { - $new = $image->driver()->createImage($image->width(), $image->height()) - ->fill($color) - ->place($image, 'top-left', 0, 0) - ->setOrigin( - new Origin($image->origin()->mediaType()) - ); - - $image = $new; - } - - return $image; + return $image->driver()->createImage($image->width(), $image->height()) + ->fill($color) + ->place($image, 'top-left', 0, 0) + ->setOrigin( + new Origin($image->origin()->mediaType()) + ); } } diff --git a/src/Manipulators/Border.php b/src/Manipulators/Border.php index 3f8ffa4..7e911e3 100644 --- a/src/Manipulators/Border.php +++ b/src/Manipulators/Border.php @@ -18,20 +18,12 @@ class Border extends BaseManipulator */ public function run(ImageInterface $image): ImageInterface { - if ($border = $this->getBorder($image)) { - list($width, $color, $method) = $border; + $border = $this->getBorder($image); - if ('overlay' === $method) { - return $this->runOverlay($image, $width, $color); - } - - if ('shrink' === $method) { - return $this->runShrink($image, $width, $color); - } + if ($border) { + [$width, $color, $method] = $border; - if ('expand' === $method) { - return $this->runExpand($image, $width, $color); - } + return $this->{'run'.$method}($image, $width, $color); } return $image; @@ -101,11 +93,11 @@ public function getColor(string $color): string */ public function getMethod(string $method): string { - if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) { - return 'overlay'; - } - - return $method; + return match ($method) { + 'expand' => 'expand', + 'shrink' => 'shrink', + default => 'overlay', + }; } /** diff --git a/src/Manipulators/Filter.php b/src/Manipulators/Filter.php index 0f06398..775e590 100644 --- a/src/Manipulators/Filter.php +++ b/src/Manipulators/Filter.php @@ -43,12 +43,12 @@ public function runGreyscaleFilter(ImageInterface $image): ImageInterface */ public function runSepiaFilter(ImageInterface $image): ImageInterface { - $image->greyscale(); - $image->brightness(-10); - $image->contrast(10); - $image->colorize(38, 27, 12); - $image->brightness(-10); - $image->contrast(10); + $image->greyscale() + ->brightness(-10) + ->contrast(10) + ->colorize(38, 27, 12) + ->brightness(-10) + ->contrast(10); return $image; } diff --git a/src/Manipulators/Flip.php b/src/Manipulators/Flip.php index 9eedea2..9e5e6b3 100644 --- a/src/Manipulators/Flip.php +++ b/src/Manipulators/Flip.php @@ -16,18 +16,14 @@ class Flip extends BaseManipulator public function run(ImageInterface $image): ImageInterface { $flip = $this->getFlip(); - if (null !== $flip) { - if ('both' === $flip) { - return $image->flip()->flop(); - } - - if ('v' === $flip) { - return $image->flip(); - } - if ('h' === $flip) { - return $image->flop(); - } + if (null !== $flip) { + return match ($flip) { + 'both' => $image->flip()->flop(), + 'v' => $image->flip(), + 'h' => $image->flop(), + default => $image, + }; } return $image; diff --git a/src/Manipulators/Orientation.php b/src/Manipulators/Orientation.php index a2fecf3..d48ef0d 100644 --- a/src/Manipulators/Orientation.php +++ b/src/Manipulators/Orientation.php @@ -16,34 +16,18 @@ class Orientation extends BaseManipulator public function run(ImageInterface $image): ImageInterface { $orientation = $this->getOrientation(); - $originalOrientation = $image->exif('Orientation'); - if ('auto' === $orientation && is_numeric($originalOrientation)) { - switch ($originalOrientation) { - case 2: - $image->flip(); - break; - case 3: - $image->rotate(180); - break; - case 4: - $image->rotate(180)->flip(); - break; - case 5: - $image->rotate(270)->flip(); - break; - case 6: - $image->rotate(270); - break; - case 7: - $image->rotate(90)->flip(); - break; - case 8: - $image->rotate(90); - break; - } - - return $image; + if ('auto' === $orientation) { + return match ($image->exif('Orientation')) { + 2 => $image->flip(), + 3 => $image->rotate(180), + 4 => $image->rotate(180)->flip(), + 5 => $image->rotate(270)->flip(), + 6 => $image->rotate(270), + 7 => $image->rotate(90)->flip(), + 8 => $image->rotate(90), + default => $image, + }; } return $image->rotate((float) $orientation); @@ -56,9 +40,9 @@ public function run(ImageInterface $image): ImageInterface */ public function getOrientation(): string { - $or = $this->getParam('or'); + $or = (string) $this->getParam('or'); - if (in_array($or, ['auto', '0', '90', '180', '270'], true)) { + if (in_array($or, ['0', '90', '180', '270'], true)) { return $or; } diff --git a/src/Manipulators/Size.php b/src/Manipulators/Size.php index 79d0bda..a8ba90d 100644 --- a/src/Manipulators/Size.php +++ b/src/Manipulators/Size.php @@ -56,9 +56,9 @@ public function run(ImageInterface $image): ImageInterface $fit = $this->getFit(); $dpr = $this->getDpr(); - list($width, $height) = $this->resolveMissingDimensions($image, $width, $height); - list($width, $height) = $this->applyDpr($width, $height, $dpr); - list($width, $height) = $this->limitImageSize($width, $height); + [$width, $height] = $this->resolveMissingDimensions($image, $width, $height); + [$width, $height] = $this->applyDpr($width, $height, $dpr); + [$width, $height] = $this->limitImageSize($width, $height); if ($width !== $image->width() || $height !== $image->height() || 1.0 !== $this->getCrop()[2]) { $image = $this->runResize($image, $fit, $width, $height); @@ -326,13 +326,13 @@ public function runStretchResize(ImageInterface $image, int $width, int $height) */ public function runCropResize(ImageInterface $image, int $width, int $height): ImageInterface { - list($resize_width, $resize_height) = $this->resolveCropResizeDimensions($image, $width, $height); + [$resize_width, $resize_height] = $this->resolveCropResizeDimensions($image, $width, $height); $zoom = $this->getCrop()[2]; $image->scale((int) round($resize_width * $zoom), (int) round($resize_height * $zoom)); - list($offset_x, $offset_y) = $this->resolveCropOffset($image, $width, $height); + [$offset_x, $offset_y] = $this->resolveCropOffset($image, $width, $height); return $image->crop($width, $height, $offset_x, $offset_y); } @@ -366,7 +366,7 @@ public function resolveCropResizeDimensions(ImageInterface $image, int $width, i */ public function resolveCropOffset(ImageInterface $image, int $width, int $height): array { - list($offset_percentage_x, $offset_percentage_y) = $this->getCrop(); + [$offset_percentage_x, $offset_percentage_y] = $this->getCrop(); $offset_x = (int) (($image->width() * $offset_percentage_x / 100) - ($width / 2)); $offset_y = (int) (($image->height() * $offset_percentage_y / 100) - ($height / 2)); diff --git a/src/Manipulators/Watermark.php b/src/Manipulators/Watermark.php index 91f2d20..2abd276 100644 --- a/src/Manipulators/Watermark.php +++ b/src/Manipulators/Watermark.php @@ -80,32 +80,34 @@ public function getWatermarksPathPrefix(): string */ public function run(ImageInterface $image): ImageInterface { - if ($watermark = $this->getImage($image)) { - $markw = $this->getDimension($image, 'markw'); - $markh = $this->getDimension($image, 'markh'); - $markx = $this->getDimension($image, 'markx'); - $marky = $this->getDimension($image, 'marky'); - $markpad = $this->getDimension($image, 'markpad'); - $markfit = $this->getFit(); - $markpos = $this->getPosition(); - $markalpha = $this->getAlpha(); - - if (null !== $markpad) { - $markx = $marky = $markpad; - } + $watermark = $this->getImage($image); - $size = new Size(); - $size->setParams([ - 'w' => $markw, - 'h' => $markh, - 'fit' => $markfit, - ]); - $watermark = $size->run($watermark); + if (null === $watermark) { + return $image; + } - $image->place($watermark, $markpos, intval($markx), intval($marky), $markalpha); + $markw = $this->getDimension($image, 'markw'); + $markh = $this->getDimension($image, 'markh'); + $markx = $this->getDimension($image, 'markx'); + $marky = $this->getDimension($image, 'marky'); + $markpad = $this->getDimension($image, 'markpad'); + $markfit = $this->getFit(); + $markpos = $this->getPosition(); + $markalpha = $this->getAlpha(); + + if (null !== $markpad) { + $markx = $marky = $markpad; } - return $image; + $size = new Size(); + $size->setParams([ + 'w' => $markw, + 'h' => $markh, + 'fit' => $markfit, + ]); + $watermark = $size->run($watermark); + + return $image->place($watermark, $markpos, intval($markx), intval($marky), $markalpha); } /** @@ -117,7 +119,7 @@ public function run(ImageInterface $image): ImageInterface */ public function getImage(ImageInterface $image): ?ImageInterface { - if (is_null($this->watermarks)) { + if (null === $this->watermarks) { return null; } diff --git a/src/Responses/PsrResponseFactory.php b/src/Responses/PsrResponseFactory.php index cff5a1c..41a658f 100644 --- a/src/Responses/PsrResponseFactory.php +++ b/src/Responses/PsrResponseFactory.php @@ -9,17 +9,13 @@ class PsrResponseFactory implements ResponseFactoryInterface { /** * Base response object. - * - * @var ResponseInterface */ - protected $response; + protected ResponseInterface $response; /** * Callback to create stream. - * - * @var \Closure */ - protected $streamCallback; + protected \Closure $streamCallback; /** * Create PsrResponseFactory instance. diff --git a/tests/Manipulators/OrientationTest.php b/tests/Manipulators/OrientationTest.php index bcc01a5..25ec250 100644 --- a/tests/Manipulators/OrientationTest.php +++ b/tests/Manipulators/OrientationTest.php @@ -27,9 +27,8 @@ public function testCreateInstance() public function testRun() { $image = \Mockery::mock(ImageInterface::class, function ($mock) { - $mock->shouldReceive('exif')->withArgs(['Orientation'])->andReturn(null)->twice(); + $mock->shouldReceive('exif')->withArgs(['Orientation'])->andReturn(null)->once(); - $mock->shouldReceive('rotate')->andReturn($mock)->with('0')->once(); $mock->shouldReceive('rotate')->andReturn($mock)->with('90')->once(); });