Skip to content

Commit da4c7ad

Browse files
authored
Merge pull request #42 from Progi1984/cm2pt
Added conversion from centimeters to points
2 parents b446746 + 7b4e636 commit da4c7ad

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Common/Drawing.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ public static function pointsToCentimeters(int $pValue = 0): float
7777
return (($pValue / 0.75) / self::DPI_96) * 2.54;
7878
}
7979

80+
/**
81+
* Convert centimeters width to points
82+
*
83+
* @param float $pValue Value in centimeters
84+
*
85+
* @return float
86+
*/
87+
public static function centimetersToPoints(float $pValue = 0): float
88+
{
89+
if ($pValue == 0) {
90+
return 0;
91+
}
92+
93+
return ($pValue / 2.54) * self::DPI_96 * 0.75;
94+
}
95+
8096
/**
8197
* Convert points width to pixels
8298
*

tests/Common/Tests/DrawingTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ public function testPointsCentimeters(): void
7474
$this->assertEquals($value / 0.75 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value));
7575
}
7676

77+
public function testCentimetersPoints(): void
78+
{
79+
$this->assertEquals(0, Drawing::centimetersToPoints());
80+
$this->assertEquals(28.346456692913385, Drawing::centimetersToPoints(1));
81+
$this->assertEquals(31.181102362204726, Drawing::centimetersToPoints(1.1));
82+
}
83+
7784
public function testTwips(): void
7885
{
7986
$value = rand(1, 100);

0 commit comments

Comments
 (0)