From 2a9a0f1cc189d5898aeb6b07188c238e9affdc89 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 29 Oct 2023 09:16:17 +0100 Subject: [PATCH] Add conversion methods to ResolutionInterface --- src/Interfaces/ResolutionInterface.php | 62 ++++++++++++ src/Resolution.php | 128 ++++++++++++++++++++++++- tests/ResolutionTest.php | 32 +++++++ 3 files changed, 220 insertions(+), 2 deletions(-) diff --git a/src/Interfaces/ResolutionInterface.php b/src/Interfaces/ResolutionInterface.php index 35f1cc7d..2ee64a2b 100644 --- a/src/Interfaces/ResolutionInterface.php +++ b/src/Interfaces/ResolutionInterface.php @@ -4,6 +4,68 @@ namespace Intervention\Image\Interfaces; interface ResolutionInterface { + /** + * Return resolution of x-axis + * + * @return float + */ public function x(): float; + + /** + * Set resolution on x-axix + * + * @param float $x + * @return ResolutionInterface + */ + public function setX(float $x): ResolutionInterface; + + /** + * Return resolution on y-axis + * + * @return float + */ public function y(): float; + + /** + * Set resolution on y-axis + * + * @param float $y + * @return ResolutionInterface + */ + public function setY(float $y): ResolutionInterface; + + /** + * Convert the resolution to DPI + * + * @return ResolutionInterface + */ + public function perInch(): ResolutionInterface; + + /** + * Convert the resolution to DPCM + * + * @return ResolutionInterface + */ + public function perCm(): ResolutionInterface; + + /** + * Return string representation of unit + * + * @return string + */ + public function unit(): string; + + /** + * Transform object to string + * + * @return string + */ + public function toString(): string; + + /** + * Cast object to string + * + * @return string + */ + public function __toString(): string; } diff --git a/src/Resolution.php b/src/Resolution.php index 9e625098..625370ce 100644 --- a/src/Resolution.php +++ b/src/Resolution.php @@ -6,18 +6,142 @@ use Intervention\Image\Interfaces\ResolutionInterface; class Resolution implements ResolutionInterface { - public function __construct(protected float $x, protected float $y) - { + public const PER_INCH = 1; + public const PER_CM = 2; + + /** + * Create new instance + * + * @param float $x + * @param float $y + * @param int $per_unit + */ + public function __construct( + protected float $x, + protected float $y, + protected int $per_unit = self::PER_INCH + ) { // } + /** + * {@inheritdoc} + * + * @see ResolutionInterface::x() + */ public function x(): float { return $this->x; } + /** + * {@inheritdoc} + * + * @see ResolutionInterface::setX() + */ + public function setX(float $x): self + { + $this->x = $x; + + return $this; + } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::y() + */ public function y(): float { return $this->y; } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::setY() + */ + public function setY(float $y): self + { + $this->y = $y; + + return $this; + } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::setPerUnit() + */ + protected function setPerUnit(int $per_unit): self + { + $this->per_unit = $per_unit; + + return $this; + } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::unit() + */ + public function unit(): string + { + return match ($this->per_unit) { + self::PER_CM => 'dpcm', + default => 'dpi', + }; + } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::perInch() + */ + public function perInch(): self + { + 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)), + default => $this + }; + } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::perCm() + */ + public function perCm(): self + { + 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)), + default => $this, + }; + } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::toString() + */ + public function toString(): string + { + return sprintf("%1\$.2f x %2\$.2f %3\$s", $this->x, $this->y, $this->unit()); + } + + /** + * {@inheritdoc} + * + * @see ResolutionInterface::__toString() + */ + public function __toString(): string + { + return $this->toString(); + } } diff --git a/tests/ResolutionTest.php b/tests/ResolutionTest.php index 3e152d83..08c8967c 100644 --- a/tests/ResolutionTest.php +++ b/tests/ResolutionTest.php @@ -21,4 +21,36 @@ class ResolutionTest extends TestCase $this->assertEquals(1.2, $resolution->x()); $this->assertEquals(3.4, $resolution->y()); } + + public function testPerInch(): 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)); + } + + 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()); + } + + public function testToString(): void + { + $resolution = new Resolution(300, 150, Resolution::PER_CM); + $this->assertEquals('300.00 x 150.00 dpcm', $resolution->toString()); + + $resolution = new Resolution(300, 150, Resolution::PER_INCH); + $this->assertEquals('300.00 x 150.00 dpi', $resolution->toString()); + $this->assertEquals('300.00 x 150.00 dpi', (string) $resolution); + } }