added convertors with test

This commit is contained in:
troosan 2018-12-28 18:04:36 +01:00
parent a4b532083b
commit d8d697c848
2 changed files with 50 additions and 0 deletions

View File

@ -217,6 +217,17 @@ class Converter
return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU); return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
} }
/**
* Convert point to cm
*
* @param int $point
* @return float
*/
public static function pointToCm($point = 1)
{
return $point / self::INCH_TO_POINT * self::INCH_TO_CM;
}
/** /**
* Convert EMU to pixel * Convert EMU to pixel
* *
@ -299,6 +310,7 @@ class Converter
if ($value == '0') { if ($value == '0') {
return 0; return 0;
} }
$matches = array();
if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) { if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) {
$size = $matches[1]; $size = $matches[1];
$unit = $matches[2]; $unit = $matches[2];
@ -332,4 +344,37 @@ class Converter
{ {
return self::pointToTwip(self::cssToPoint($value)); return self::pointToTwip(self::cssToPoint($value));
} }
/**
* Transforms a size in CSS format (eg. 10px, 10px, ...) to pixel
*
* @param string $value
* @return float
*/
public static function cssToPixel($value)
{
return self::pointToPixel(self::cssToPoint($value));
}
/**
* Transforms a size in CSS format (eg. 10px, 10px, ...) to cm
*
* @param string $value
* @return float
*/
public static function cssToCm($value)
{
return self::pointToCm(self::cssToPoint($value));
}
/**
* Transforms a size in CSS format (eg. 10px, 10px, ...) to emu
*
* @param string $value
* @return float
*/
public static function cssToEmu($value)
{
return self::pointToEmu(self::cssToPoint($value));
}
} }

View File

@ -29,6 +29,7 @@ class ConverterTest extends \PHPUnit\Framework\TestCase
*/ */
public function testUnitConversions() public function testUnitConversions()
{ {
$values = array();
$values[] = 0; // zero value $values[] = 0; // zero value
$values[] = rand(1, 100) / 100; // fraction number $values[] = rand(1, 100) / 100; // fraction number
$values[] = rand(1, 100); // integer $values[] = rand(1, 100); // integer
@ -79,6 +80,9 @@ class ConverterTest extends \PHPUnit\Framework\TestCase
$result = Converter::pointToTwip($value); $result = Converter::pointToTwip($value);
$this->assertEquals($value * 20, $result); $this->assertEquals($value * 20, $result);
$result = Converter::pointToCm($value);
$this->assertEquals($value * 0.035277778, $result, '', 0.00001);
$result = Converter::pointToPixel($value); $result = Converter::pointToPixel($value);
$this->assertEquals($value / 72 * 96, $result); $this->assertEquals($value / 72 * 96, $result);
@ -105,6 +109,7 @@ class ConverterTest extends \PHPUnit\Framework\TestCase
public function testHtmlToRGB() public function testHtmlToRGB()
{ {
// Prepare test values [ original, expected ] // Prepare test values [ original, expected ]
$values = array();
$values[] = array('#FF99DD', array(255, 153, 221)); // With # $values[] = array('#FF99DD', array(255, 153, 221)); // With #
$values[] = array('FF99DD', array(255, 153, 221)); // 6 characters $values[] = array('FF99DD', array(255, 153, 221)); // 6 characters
$values[] = array('F9D', array(255, 153, 221)); // 3 characters $values[] = array('F9D', array(255, 153, 221)); // 3 characters