Skip to content

Commit

Permalink
Fix incorrect resolution conversion (#1411)
Browse files Browse the repository at this point in the history
* Assert resolution x/y result

* Fix Resolution conversion
  • Loading branch information
olivervogel authored Jan 4, 2025
1 parent 116bd03 commit 436460e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/Drivers/Imagick/Analyzers/ResolutionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}
8 changes: 4 additions & 4 deletions src/Resolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ 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)),
->setX($this->x * 2.54)
->setY($this->y * 2.54),
default => $this
};
}
Expand All @@ -120,8 +120,8 @@ 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)),
->setX($this->x / 2.54)
->setY($this->y / 2.54),
default => $this,
};
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Drivers/Gd/Analyzers/ResolutionAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
30 changes: 16 additions & 14 deletions tests/Unit/ResolutionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ public function testXY(): void
$this->assertEquals(3.4, $resolution->y());
}

public function testPerInch(): void
public function testUnit(): void
{
$resolution = new Resolution(300, 150); // per inch
$this->assertEquals(300, $resolution->perInch()->x());
$this->assertEquals(150, $resolution->perInch()->y());
$resolution = new Resolution(1, 1);
$this->assertEquals('dpi', $resolution->unit());

$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(1, 1, Resolution::PER_CM);
$this->assertEquals('dpcm', $resolution->unit());
}

public function testPerCm(): void
public function testConversion(): 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); // per inch
$this->assertEquals(300, $resolution->perInch()->x());
$this->assertEquals(150, $resolution->perInch()->y());

$resolution = new Resolution(300, 150, Resolution::PER_CM);
$this->assertEquals(300, $resolution->perCm()->x());
$this->assertEquals(150, $resolution->perCm()->y());
$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));

$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
Expand Down
Binary file added tests/resources/300dpi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 436460e

Please sign in to comment.