Skip to content

Commit

Permalink
Fix Resolution conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jan 1, 2025
1 parent 1673ebf commit d0d8d86
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 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: 2 additions & 2 deletions tests/Unit/Drivers/Gd/Analyzers/ResolutionAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testAnalyze(): void
$analyzer->setDriver(new Driver());
$result = $analyzer->analyze($image);
$this->assertInstanceOf(Resolution::class, $result);
$this->assertEquals(300, $result->x());
$this->assertEquals(300, $result->y());
$this->assertEquals(300, $result->perInch()->x());
$this->assertEquals(300, $result->perInch()->y());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testAnalyze(): void
$analyzer->setDriver(new Driver());
$result = $analyzer->analyze($image);
$this->assertInstanceOf(Resolution::class, $result);
$this->assertEquals(300, $result->x());
$this->assertEquals(300, $result->y());
$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

0 comments on commit d0d8d86

Please sign in to comment.