From a566769f21595c8b1f44357b084b02eddc5664e3 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 6 Jun 2024 17:38:32 +0200 Subject: [PATCH] Fix bug in cover modifiers See: https://github.com/Intervention/image/issues/1359 --- .../Gd/Modifiers/CoverDownModifier.php | 2 +- .../Imagick/Modifiers/CoverDownModifier.php | 2 +- src/Modifiers/CoverModifier.php | 2 +- .../Gd/Modifiers/CoverDownModifierTest.php | 38 +++++++++++++++++++ .../Gd/Modifiers/CoverModifierTest.php | 8 ++++ .../Modifiers/CoverDownModifierTest.php | 38 +++++++++++++++++++ .../Imagick/Modifiers/CoverModifierTest.php | 8 ++++ 7 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/Drivers/Gd/Modifiers/CoverDownModifierTest.php create mode 100644 tests/Unit/Drivers/Imagick/Modifiers/CoverDownModifierTest.php diff --git a/src/Drivers/Gd/Modifiers/CoverDownModifier.php b/src/Drivers/Gd/Modifiers/CoverDownModifier.php index dc6fbd2d..ac1cec11 100644 --- a/src/Drivers/Gd/Modifiers/CoverDownModifier.php +++ b/src/Drivers/Gd/Modifiers/CoverDownModifier.php @@ -14,6 +14,6 @@ class CoverDownModifier extends CoverModifier */ public function getResizeSize(SizeInterface $size): SizeInterface { - return $size->scaleDown($this->width, $this->height); + return $size->resizeDown($this->width, $this->height); } } diff --git a/src/Drivers/Imagick/Modifiers/CoverDownModifier.php b/src/Drivers/Imagick/Modifiers/CoverDownModifier.php index efc82297..44ab79e4 100644 --- a/src/Drivers/Imagick/Modifiers/CoverDownModifier.php +++ b/src/Drivers/Imagick/Modifiers/CoverDownModifier.php @@ -14,6 +14,6 @@ class CoverDownModifier extends CoverModifier */ public function getResizeSize(SizeInterface $size): SizeInterface { - return $size->scaleDown($this->width, $this->height); + return $size->resizeDown($this->width, $this->height); } } diff --git a/src/Modifiers/CoverModifier.php b/src/Modifiers/CoverModifier.php index d71a5e42..61e5acc5 100644 --- a/src/Modifiers/CoverModifier.php +++ b/src/Modifiers/CoverModifier.php @@ -38,6 +38,6 @@ public function getCropSize(ImageInterface $image): SizeInterface */ public function getResizeSize(SizeInterface $size): SizeInterface { - return $size->scale($this->width, $this->height); + return $size->resize($this->width, $this->height); } } diff --git a/tests/Unit/Drivers/Gd/Modifiers/CoverDownModifierTest.php b/tests/Unit/Drivers/Gd/Modifiers/CoverDownModifierTest.php new file mode 100644 index 00000000..4b08306a --- /dev/null +++ b/tests/Unit/Drivers/Gd/Modifiers/CoverDownModifierTest.php @@ -0,0 +1,38 @@ +readTestImage('blocks.png'); + $this->assertEquals(640, $image->width()); + $this->assertEquals(480, $image->height()); + $image->modify(new CoverDownModifier(100, 100, 'center')); + $this->assertEquals(100, $image->width()); + $this->assertEquals(100, $image->height()); + $this->assertColor(255, 0, 0, 255, $image->pickColor(90, 90)); + $this->assertColor(0, 255, 0, 255, $image->pickColor(65, 70)); + $this->assertColor(0, 0, 255, 255, $image->pickColor(70, 52)); + $this->assertTransparency($image->pickColor(90, 30)); + } + + public function testModifyOddSize(): void + { + $image = $this->createTestImage(375, 250); + $image->modify(new CoverDownModifier(240, 90, 'center')); + $this->assertEquals(240, $image->width()); + $this->assertEquals(90, $image->height()); + } +} diff --git a/tests/Unit/Drivers/Gd/Modifiers/CoverModifierTest.php b/tests/Unit/Drivers/Gd/Modifiers/CoverModifierTest.php index 20ccd37d..83c9487b 100644 --- a/tests/Unit/Drivers/Gd/Modifiers/CoverModifierTest.php +++ b/tests/Unit/Drivers/Gd/Modifiers/CoverModifierTest.php @@ -27,4 +27,12 @@ public function testModify(): void $this->assertColor(0, 0, 255, 255, $image->pickColor(70, 52)); $this->assertTransparency($image->pickColor(90, 30)); } + + public function testModifyOddSize(): void + { + $image = $this->createTestImage(375, 250); + $image->modify(new CoverModifier(240, 90, 'center')); + $this->assertEquals(240, $image->width()); + $this->assertEquals(90, $image->height()); + } } diff --git a/tests/Unit/Drivers/Imagick/Modifiers/CoverDownModifierTest.php b/tests/Unit/Drivers/Imagick/Modifiers/CoverDownModifierTest.php new file mode 100644 index 00000000..2ffdfc96 --- /dev/null +++ b/tests/Unit/Drivers/Imagick/Modifiers/CoverDownModifierTest.php @@ -0,0 +1,38 @@ +readTestImage('blocks.png'); + $this->assertEquals(640, $image->width()); + $this->assertEquals(480, $image->height()); + $image->modify(new CoverDownModifier(100, 100, 'center')); + $this->assertEquals(100, $image->width()); + $this->assertEquals(100, $image->height()); + $this->assertColor(255, 0, 0, 255, $image->pickColor(90, 90)); + $this->assertColor(0, 255, 0, 255, $image->pickColor(65, 70)); + $this->assertColor(0, 0, 255, 255, $image->pickColor(70, 52)); + $this->assertTransparency($image->pickColor(90, 30)); + } + + public function testModifyOddSize(): void + { + $image = $this->createTestImage(375, 250); + $image->modify(new CoverDownModifier(240, 90, 'center')); + $this->assertEquals(240, $image->width()); + $this->assertEquals(90, $image->height()); + } +} diff --git a/tests/Unit/Drivers/Imagick/Modifiers/CoverModifierTest.php b/tests/Unit/Drivers/Imagick/Modifiers/CoverModifierTest.php index 07c607a1..87523dbd 100644 --- a/tests/Unit/Drivers/Imagick/Modifiers/CoverModifierTest.php +++ b/tests/Unit/Drivers/Imagick/Modifiers/CoverModifierTest.php @@ -27,4 +27,12 @@ public function testModify(): void $this->assertColor(0, 0, 255, 255, $image->pickColor(70, 52)); $this->assertTransparency($image->pickColor(90, 30)); } + + public function testModifyOddSize(): void + { + $image = $this->createTestImage(375, 250); + $image->modify(new CoverModifier(240, 90, 'center')); + $this->assertEquals(240, $image->width()); + $this->assertEquals(90, $image->height()); + } }