mirror of
https://github.com/Intervention/image.git
synced 2025-08-21 05:01:20 +02:00
Fix incorrect resolution conversion (#1411)
* Assert resolution x/y result * Fix Resolution conversion
This commit is contained in:
@@ -13,6 +13,13 @@ class ResolutionAnalyzer extends GenericResolutionAnalyzer implements Specialize
|
|||||||
{
|
{
|
||||||
public function analyze(ImageInterface $image): mixed
|
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(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -104,8 +104,8 @@ class Resolution implements ResolutionInterface
|
|||||||
return match ($this->per_unit) {
|
return match ($this->per_unit) {
|
||||||
self::PER_CM => $this
|
self::PER_CM => $this
|
||||||
->setPerUnit(self::PER_INCH)
|
->setPerUnit(self::PER_INCH)
|
||||||
->setX($this->x * (1 / 2.54))
|
->setX($this->x * 2.54)
|
||||||
->setY($this->y * (1 / 2.54)),
|
->setY($this->y * 2.54),
|
||||||
default => $this
|
default => $this
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -120,8 +120,8 @@ class Resolution implements ResolutionInterface
|
|||||||
return match ($this->per_unit) {
|
return match ($this->per_unit) {
|
||||||
self::PER_INCH => $this
|
self::PER_INCH => $this
|
||||||
->setPerUnit(self::PER_CM)
|
->setPerUnit(self::PER_CM)
|
||||||
->setX($this->x / (1 / 2.54))
|
->setX($this->x / 2.54)
|
||||||
->setY($this->y / (1 / 2.54)),
|
->setY($this->y / 2.54),
|
||||||
default => $this,
|
default => $this,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -17,10 +17,12 @@ final class ResolutionAnalyzerTest extends GdTestCase
|
|||||||
{
|
{
|
||||||
public function testAnalyze(): void
|
public function testAnalyze(): void
|
||||||
{
|
{
|
||||||
$image = $this->readTestImage('tile.png');
|
$image = $this->readTestImage('300dpi.png');
|
||||||
$analyzer = new ResolutionAnalyzer();
|
$analyzer = new ResolutionAnalyzer();
|
||||||
$analyzer->setDriver(new Driver());
|
$analyzer->setDriver(new Driver());
|
||||||
$result = $analyzer->analyze($image);
|
$result = $analyzer->analyze($image);
|
||||||
$this->assertInstanceOf(Resolution::class, $result);
|
$this->assertInstanceOf(Resolution::class, $result);
|
||||||
|
$this->assertEquals(300, $result->perInch()->x());
|
||||||
|
$this->assertEquals(300, $result->perInch()->y());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,10 +17,12 @@ final class ResolutionAnalyzerTest extends ImagickTestCase
|
|||||||
{
|
{
|
||||||
public function testAnalyze(): void
|
public function testAnalyze(): void
|
||||||
{
|
{
|
||||||
$image = $this->readTestImage('tile.png');
|
$image = $this->readTestImage('300dpi.png');
|
||||||
$analyzer = new ResolutionAnalyzer();
|
$analyzer = new ResolutionAnalyzer();
|
||||||
$analyzer->setDriver(new Driver());
|
$analyzer->setDriver(new Driver());
|
||||||
$result = $analyzer->analyze($image);
|
$result = $analyzer->analyze($image);
|
||||||
$this->assertInstanceOf(Resolution::class, $result);
|
$this->assertInstanceOf(Resolution::class, $result);
|
||||||
|
$this->assertEquals(300, round($result->perInch()->x()));
|
||||||
|
$this->assertEquals(300, round($result->perInch()->y()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -24,26 +24,28 @@ final class ResolutionTest extends BaseTestCase
|
|||||||
$this->assertEquals(3.4, $resolution->y());
|
$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
|
$resolution = new Resolution(300, 150); // per inch
|
||||||
$this->assertEquals(300, $resolution->perInch()->x());
|
$this->assertEquals(300, $resolution->perInch()->x());
|
||||||
$this->assertEquals(150, $resolution->perInch()->y());
|
$this->assertEquals(150, $resolution->perInch()->y());
|
||||||
|
|
||||||
$resolution = new Resolution(300, 150, Resolution::PER_CM);
|
$resolution = new Resolution(300, 150); // per inch
|
||||||
$this->assertEquals(118.11024, round($resolution->perInch()->x(), 5));
|
$this->assertEquals(118.11, round($resolution->perCm()->x(), 2));
|
||||||
$this->assertEquals(59.05512, round($resolution->perInch()->y(), 5));
|
$this->assertEquals(59.06, round($resolution->perCm()->y(), 2));
|
||||||
}
|
|
||||||
|
|
||||||
public function testPerCm(): void
|
$resolution = new Resolution(118.11024, 59.06, Resolution::PER_CM); // per cm
|
||||||
{
|
$this->assertEquals(300, round($resolution->perInch()->x()));
|
||||||
$resolution = new Resolution(118.11024, 59.05512); // per inch
|
$this->assertEquals(150, round($resolution->perInch()->y()));
|
||||||
$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
|
public function testToString(): void
|
||||||
|
BIN
tests/resources/300dpi.png
Normal file
BIN
tests/resources/300dpi.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.9 KiB |
Reference in New Issue
Block a user