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 00000000..58f54bf9 Binary files /dev/null and b/tests/resources/300dpi.png differ