Skip to content

Commit 12a3e2b

Browse files
committed
Combine image flip methods
1 parent 6a72305 commit 12a3e2b

File tree

16 files changed

+173
-186
lines changed

16 files changed

+173
-186
lines changed

src/Colors/Rgb/NamedColor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use Intervention\Image\Colors\Rgb\Channels\Alpha;
88
use Intervention\Image\Colors\Rgb\Color as RgbColor;
99
use Intervention\Image\Colors\Rgb\Colorspace as Rgb;
10-
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
1110
use Intervention\Image\Exceptions\ColorDecoderException;
12-
use Intervention\Image\InputHandler;
1311
use Intervention\Image\Interfaces\ColorChannelInterface;
1412
use Intervention\Image\Interfaces\ColorInterface;
1513
use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -421,6 +419,10 @@ public function withTransparency(float $transparency): ColorInterface
421419
*/
422420
private function toRgbColor(): RgbColor
423421
{
424-
return $this->colorspace()->importColor($this);
422+
$color = $this->colorspace()->importColor($this);
423+
424+
return $color instanceof RgbColor
425+
? $color
426+
: throw new ColorDecoderException('Failed to convert named color to rgb object');
425427
}
426428
}

src/Direction.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Intervention\Image;
6+
7+
enum Direction
8+
{
9+
case HORIZONTAL;
10+
case VERTICAL;
11+
}

src/Drivers/Gd/Modifiers/AlignRotationModifier.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,30 @@ class AlignRotationModifier extends GenericAlignRotationModifier implements Spec
1717
*/
1818
public function apply(ImageInterface $image): ImageInterface
1919
{
20-
$image = match ($image->exif('IFD0.Orientation')) {
21-
2 => $image->flop(),
20+
$image = match ($this->orientation($image)) {
21+
2 => $image->flip(),
2222
3 => $image->rotate(180),
23-
4 => $image->rotate(180)->flop(),
24-
5 => $image->rotate(270)->flop(),
25-
6 => $image->rotate(270),
26-
7 => $image->rotate(90)->flop(),
27-
8 => $image->rotate(90),
23+
4 => $image->rotate(180)->flip(),
24+
5 => $image->rotate(90)->flip(),
25+
6 => $image->rotate(90),
26+
7 => $image->rotate(270)->flip(),
27+
8 => $image->rotate(270),
2828
default => $image
2929
};
3030

3131
return $this->markAligned($image);
3232
}
3333

34+
/**
35+
* Return exif information about image orientation.
36+
*/
37+
private function orientation(ImageInterface $image): int
38+
{
39+
$orientation = $image->exif('IFD0.Orientation');
40+
41+
return is_numeric($orientation) ? (int) $orientation : 0;
42+
}
43+
3444
/**
3545
* Set exif data of image to top-left orientation, marking the image as
3646
* aligned and making sure the rotation correction process is not

src/Drivers/Gd/Modifiers/FlipModifier.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Intervention\Image\Drivers\Gd\Modifiers;
66

7+
use Intervention\Image\Direction;
78
use Intervention\Image\Exceptions\ModifierException;
89
use Intervention\Image\Interfaces\ImageInterface;
910
use Intervention\Image\Interfaces\SpecializedInterface;
@@ -20,8 +21,10 @@ class FlipModifier extends GenericFlipModifier implements SpecializedInterface
2021
*/
2122
public function apply(ImageInterface $image): ImageInterface
2223
{
24+
$direction = $this->direction === Direction::HORIZONTAL ? IMG_FLIP_HORIZONTAL : IMG_FLIP_VERTICAL;
25+
2326
foreach ($image as $frame) {
24-
imageflip($frame->native(), IMG_FLIP_VERTICAL);
27+
imageflip($frame->native(), $direction);
2528
}
2629

2730
return $image;

src/Drivers/Gd/Modifiers/FlopModifier.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Drivers/Imagick/Modifiers/AlignRotationModifier.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ public function apply(ImageInterface $image): ImageInterface
3030
=> $image->core()->native()->rotateImage('#000', 180) && $image->core()->native()->flopImage(), // 4
3131

3232
Imagick::ORIENTATION_LEFTTOP
33-
=> $image->core()->native()->rotateImage('#000', -270) && $image->core()->native()->flopImage(), // 5
33+
=> $image->core()->native()->rotateImage('#000', 90) && $image->core()->native()->flopImage(), // 5
3434

3535
Imagick::ORIENTATION_RIGHTTOP
36-
=> $image->core()->native()->rotateImage('#000', -270), // 6
36+
=> $image->core()->native()->rotateImage('#000', 90), // 6
3737

3838
Imagick::ORIENTATION_RIGHTBOTTOM
39-
=> $image->core()->native()->rotateImage('#000', -90) && $image->core()->native()->flopImage(), // 7
39+
=> $image->core()->native()->rotateImage('#000', 270) && $image->core()->native()->flopImage(), // 7
4040

4141
Imagick::ORIENTATION_LEFTBOTTOM
42-
=> $image->core()->native()->rotateImage('#000', -90), // 8
42+
=> $image->core()->native()->rotateImage('#000', 270), // 8
4343

4444
default => 'value',
4545
};

src/Drivers/Imagick/Modifiers/FlipModifier.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Intervention\Image\Drivers\Imagick\Modifiers;
66

77
use ImagickException;
8+
use Intervention\Image\Direction;
89
use Intervention\Image\Exceptions\ModifierException;
910
use Intervention\Image\Interfaces\ImageInterface;
1011
use Intervention\Image\Interfaces\SpecializedInterface;
@@ -19,7 +20,9 @@ public function apply(ImageInterface $image): ImageInterface
1920
{
2021
foreach ($image as $frame) {
2122
try {
22-
$result = $frame->native()->flipImage();
23+
$result = $this->direction === Direction::HORIZONTAL
24+
? $frame->native()->flopImage()
25+
: $frame->native()->flipImage();
2326
if ($result === false) {
2427
throw new ModifierException(
2528
'Failed to apply ' . self::class . ', unable to mirror image',

src/Drivers/Imagick/Modifiers/FlopModifier.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Image.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
use Intervention\Image\Modifiers\DrawRectangleModifier;
6868
use Intervention\Image\Modifiers\FillModifier;
6969
use Intervention\Image\Modifiers\FlipModifier;
70-
use Intervention\Image\Modifiers\FlopModifier;
7170
use Intervention\Image\Modifiers\GammaModifier;
7271
use Intervention\Image\Modifiers\GrayscaleModifier;
7372
use Intervention\Image\Modifiers\InsertModifier;
@@ -546,19 +545,9 @@ public function colorize(int $red = 0, int $green = 0, int $blue = 0): ImageInte
546545
*
547546
* @see ImageInterface::flip()
548547
*/
549-
public function flip(): ImageInterface
548+
public function flip(Direction $direction = Direction::HORIZONTAL): ImageInterface
550549
{
551-
return $this->modify(new FlipModifier());
552-
}
553-
554-
/**
555-
* {@inheritdoc}
556-
*
557-
* @see ImageInterface::flop()
558-
*/
559-
public function flop(): ImageInterface
560-
{
561-
return $this->modify(new FlopModifier());
550+
return $this->modify(new FlipModifier($direction));
562551
}
563552

564553
/**

src/Interfaces/ImageInterface.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Intervention\Image\MediaType;
1616
use Intervention\Image\Origin;
1717
use Intervention\Image\Alignment;
18+
use Intervention\Image\Direction;
1819
use Intervention\Image\Fraction;
1920
use Intervention\Image\Format;
2021
use IteratorAggregate;
@@ -273,16 +274,9 @@ public function colorize(int $red = 0, int $green = 0, int $blue = 0): self;
273274
/**
274275
* Mirror the current image horizontally.
275276
*
276-
* @link https://image.intervention.io/v3/modifying-images/effects#mirror-image-vertically
277+
* @link https://image.intervention.io/v3/modifying-images/effects
277278
*/
278-
public function flip(): self;
279-
280-
/**
281-
* Mirror the current image vertically.
282-
*
283-
* @link https://image.intervention.io/v3/modifying-images/effects#mirror-image-horizontally
284-
*/
285-
public function flop(): self; // TODO: maybe rename
279+
public function flip(Direction $direction = Direction::HORIZONTAL): self;
286280

287281
/**
288282
* Blur current image by given strength.

0 commit comments

Comments
 (0)