Skip to content

Commit

Permalink
Merge pull request #388 from thephpleague/3.x-cleanup
Browse files Browse the repository at this point in the history
Cleanup code
  • Loading branch information
ADmad authored Jan 30, 2024
2 parents bcb3168 + 1eb1ce5 commit bec08c7
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 112 deletions.
18 changes: 6 additions & 12 deletions src/Manipulators/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);
}
}
26 changes: 9 additions & 17 deletions src/Manipulators/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
};
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Manipulators/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 7 additions & 11 deletions src/Manipulators/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 13 additions & 29 deletions src/Manipulators/Orientation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Manipulators/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
Expand Down
48 changes: 25 additions & 23 deletions src/Manipulators/Watermark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}

Expand Down
8 changes: 2 additions & 6 deletions src/Responses/PsrResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions tests/Manipulators/OrientationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit bec08c7

Please sign in to comment.