1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-28 08:09:54 +02:00

Add conversion methods to ResolutionInterface

This commit is contained in:
Oliver Vogel
2023-10-29 09:16:17 +01:00
parent e3748065bb
commit 2a9a0f1cc1
3 changed files with 220 additions and 2 deletions

View File

@@ -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;
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}