From 436460e33b55fc5da82d552b8811f0d0094304df Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 4 Jan 2025 08:31:13 +0100 Subject: [PATCH] Fix incorrect resolution conversion (#1411) * Assert resolution x/y result * Fix Resolution conversion --- .../Imagick/Analyzers/ResolutionAnalyzer.php | 9 +++++- src/Resolution.php | 8 ++--- .../Gd/Analyzers/ResolutionAnalyzerTest.php | 4 ++- .../Analyzers/ResolutionAnalyzerTest.php | 4 ++- tests/Unit/ResolutionTest.php | 30 ++++++++++-------- tests/resources/300dpi.png | Bin 0 -> 1911 bytes 6 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 tests/resources/300dpi.png diff --git a/src/Drivers/Imagick/Analyzers/ResolutionAnalyzer.php b/src/Drivers/Imagick/Analyzers/ResolutionAnalyzer.php index 6bd4731e..1a1b6321 100644 --- a/src/Drivers/Imagick/Analyzers/ResolutionAnalyzer.php +++ b/src/Drivers/Imagick/Analyzers/ResolutionAnalyzer.php @@ -13,6 +13,13 @@ class ResolutionAnalyzer extends GenericResolutionAnalyzer implements Specialize { public function analyze(ImageInterface $image): mixed { - return new Resolution(...$image->core()->native()->getImageResolution()); + $imagick = $image->core()->native(); + $imageResolution = $imagick->getImageResolution(); + + return new Resolution( + $imageResolution['x'], + $imageResolution['y'], + $imagick->getImageUnits(), + ); } } diff --git a/src/Resolution.php b/src/Resolution.php index ef4a6921..dea71b13 100644 --- a/src/Resolution.php +++ b/src/Resolution.php @@ -104,8 +104,8 @@ class Resolution implements ResolutionInterface return match ($this->per_unit) { self::PER_CM => $this ->setPerUnit(self::PER_INCH) - ->setX($this->x * (1 / 2.54)) - ->setY($this->y * (1 / 2.54)), + ->setX($this->x * 2.54) + ->setY($this->y * 2.54), default => $this }; } @@ -120,8 +120,8 @@ class Resolution implements ResolutionInterface return match ($this->per_unit) { self::PER_INCH => $this ->setPerUnit(self::PER_CM) - ->setX($this->x / (1 / 2.54)) - ->setY($this->y / (1 / 2.54)), + ->setX($this->x / 2.54) + ->setY($this->y / 2.54), default => $this, }; } diff --git a/tests/Unit/Drivers/Gd/Analyzers/ResolutionAnalyzerTest.php b/tests/Unit/Drivers/Gd/Analyzers/ResolutionAnalyzerTest.php index 7b63c31a..fb1dde47 100644 --- a/tests/Unit/Drivers/Gd/Analyzers/ResolutionAnalyzerTest.php +++ b/tests/Unit/Drivers/Gd/Analyzers/ResolutionAnalyzerTest.php @@ -17,10 +17,12 @@ final class ResolutionAnalyzerTest extends GdTestCase { public function testAnalyze(): void { - $image = $this->readTestImage('tile.png'); + $image = $this->readTestImage('300dpi.png'); $analyzer = new ResolutionAnalyzer(); $analyzer->setDriver(new Driver()); $result = $analyzer->analyze($image); $this->assertInstanceOf(Resolution::class, $result); + $this->assertEquals(300, $result->perInch()->x()); + $this->assertEquals(300, $result->perInch()->y()); } } diff --git a/tests/Unit/Drivers/Imagick/Analyzers/ResolutionAnalyzerTest.php b/tests/Unit/Drivers/Imagick/Analyzers/ResolutionAnalyzerTest.php index c811062f..1f7e9ed3 100644 --- a/tests/Unit/Drivers/Imagick/Analyzers/ResolutionAnalyzerTest.php +++ b/tests/Unit/Drivers/Imagick/Analyzers/ResolutionAnalyzerTest.php @@ -17,10 +17,12 @@ final class ResolutionAnalyzerTest extends ImagickTestCase { public function testAnalyze(): void { - $image = $this->readTestImage('tile.png'); + $image = $this->readTestImage('300dpi.png'); $analyzer = new ResolutionAnalyzer(); $analyzer->setDriver(new Driver()); $result = $analyzer->analyze($image); $this->assertInstanceOf(Resolution::class, $result); + $this->assertEquals(300, round($result->perInch()->x())); + $this->assertEquals(300, round($result->perInch()->y())); } } diff --git a/tests/Unit/ResolutionTest.php b/tests/Unit/ResolutionTest.php index 1e09748a..6986b89d 100644 --- a/tests/Unit/ResolutionTest.php +++ b/tests/Unit/ResolutionTest.php @@ -24,26 +24,28 @@ final class ResolutionTest extends BaseTestCase $this->assertEquals(3.4, $resolution->y()); } - public function testPerInch(): void + public function testUnit(): void + { + $resolution = new Resolution(1, 1); + $this->assertEquals('dpi', $resolution->unit()); + + $resolution = new Resolution(1, 1, Resolution::PER_CM); + $this->assertEquals('dpcm', $resolution->unit()); + } + + public function testConversion(): void { $resolution = new Resolution(300, 150); // per inch $this->assertEquals(300, $resolution->perInch()->x()); $this->assertEquals(150, $resolution->perInch()->y()); - $resolution = new Resolution(300, 150, Resolution::PER_CM); - $this->assertEquals(118.11024, round($resolution->perInch()->x(), 5)); - $this->assertEquals(59.05512, round($resolution->perInch()->y(), 5)); - } + $resolution = new Resolution(300, 150); // per inch + $this->assertEquals(118.11, round($resolution->perCm()->x(), 2)); + $this->assertEquals(59.06, round($resolution->perCm()->y(), 2)); - public function testPerCm(): void - { - $resolution = new Resolution(118.11024, 59.05512); // per inch - $this->assertEquals(300, round($resolution->perCm()->x())); - $this->assertEquals(150, round($resolution->perCm()->y())); - - $resolution = new Resolution(300, 150, Resolution::PER_CM); - $this->assertEquals(300, $resolution->perCm()->x()); - $this->assertEquals(150, $resolution->perCm()->y()); + $resolution = new Resolution(118.11024, 59.06, Resolution::PER_CM); // per cm + $this->assertEquals(300, round($resolution->perInch()->x())); + $this->assertEquals(150, round($resolution->perInch()->y())); } public function testToString(): void diff --git a/tests/resources/300dpi.png b/tests/resources/300dpi.png new file mode 100644 index 0000000000000000000000000000000000000000..58f54bf9ef5a5c0f707a950182c30e1e3c8cbd27 GIT binary patch literal 1911 zcmah~`#;l*8zl~#KgoPPUlcw zBBqHdObR5zej-6uMB=x+9PPzt-WU-?jr7fPez(NLe%1b2;%fyMopla5CnO6?qli*xB8aKFf!Q&z5N_d$ywD-? z0D*KQ^F;dr!j#m5tNUET-zghRORw=LIC>$^J<7$v)}M75kK?d--m71D@oCU*97izB zE3u;5{>WP6RhaQN&RmCvW}6JE{!h5%)k(H32S?6Z=%*n7G1zcJhSl18n!wkRb`oDu zXSSDU4!#$xBK6LE(q_^vc4Y1bwTy6an5e~&h&3>lyRK2Rt=h>2W`Q4JWv{O~4D%;i z7bML=(DB@bo8Pkj@E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