px_per_em = $value; } /** * Query the pixel per em unit. * * @return int The current value. */ public function getPixelPerEm () { return $this->px_per_em; } /** * Sets the twips per pixel (X axis) used for px to pt conversion. * * @param int $value The value to be set. */ public function setTwipsPerPixelX ($value) { $this->twips_per_pixel_x = $value; } /** * Sets the twips per pixel (Y axis) unit used for px to pt conversion. * * @param int $value The value to be set. */ public function setTwipsPerPixelY ($value) { $this->twips_per_pixel_y = $value; } /** * Query the twips per pixel (X axis) setting. * * @return int The current value. */ public function getTwipsPerPixelX () { return $this->twips_per_pixel_x; } /** * Query the twips per pixel (Y axis) setting. * * @return int The current value. */ public function getTwipsPerPixelY () { return $this->twips_per_pixel_y; } /** * Convert pixel (X axis) to points according to the current settings. * * @param string|int $pixel String with pixel length value, e.g. '20px' * @return string The current value. */ public function pixelToPointsX ($pixel) { $pixel = self::getDigits ((string)$pixel); $value = $pixel * $this->twips_per_pixel_x / self::$twips_per_point; return round ($value, 2).'pt'; } /** * Convert pixel (Y axis) to points according to the current settings. * * @param string|int $pixel String with pixel length value, e.g. '20px' * @return string The current value. */ public function pixelToPointsY ($pixel) { $pixel = self::getDigits ((string)$pixel); $value = $pixel * $this->twips_per_pixel_y / self::$twips_per_point; return round ($value, 2).'pt'; } /** * Convert length value with valid XSL unit to points. * * @param string $value String with length value, e.g. '20px', '20cm'... * @param string $axis Is the value to be converted a value on the X or Y axis? Default is 'y'. * Only relevant for conversion from 'px' or 'em'. * @return string The current value. */ public function toPoints ($value, $axis = 'y') { $unit = self::stripDigits ($value); if ( $unit == 'pt' ) { return $value; } if ( self::isValidXSLUnit ($unit) === false ) { // Not a vlaid/supported unit. Return original value. return $value; } $value = self::getDigits ($value); switch ($unit) { case 'cm': $value = round (($value/self::$point_in_cm), 2).'pt'; break; case 'mm': $value = round (($value/(10 * self::$point_in_cm)), 2).'pt'; break; case 'in': $value = round (($value * self::$inch_in_pt), 2).'pt'; break; case 'pc': $value = round (($value * self::$pc_in_pt), 2).'pt'; break; case 'px': if ( $axis == 'x' || $axis == 'X' ) { $value = $this->pixelToPointsX ($value); } else { $value = $this->pixelToPointsY ($value); } break; case 'em': $value = $this->pixelToPointsY ($value * $this->getPixelPerEm()); break; } return $value; } /** * Convert length value with valid XSL unit to points. * * @param string $value String with length value, e.g. '20px', '20pt'... * @param string $axis Is the value to be converted a value on the X or Y axis? Default is 'y'. * Only relevant for conversion from 'px' or 'em'. * @return string The current value. */ public function toCentimeters ($value, $axis = 'y') { $unit = self::stripDigits ($value); if ( $unit == 'cm' ) { return $value; } if ( self::isValidXSLUnit ($unit) === false ) { // Not a vlaid/supported unit. Return original value. return $value; } $value = self::toPoints ($value, $axis); $value = substr($value, 0, -2); $value = round (($value * self::$point_in_cm), 2).'cm'; return $value; } /** * Convert length value with valid XSL unit to pixel. * * @param string $value String with length value, e.g. '20pt'... * @param string $axis Is the value to be converted a value on the X or Y axis? Default is 'y'. * Only relevant for conversion from 'px' or 'em'. * @return string The current value. */ public function toPixel ($value, $axis = 'y') { $unit = self::stripDigits ($value); if ( $unit == 'px' ) { return $value; } if ( self::isValidXSLUnit ($unit) === false ) { // Not a vlaid/supported unit. Return original value. return $value; } $value = self::toPoints ($value, $axis); $value = substr($value, 0, -2); if ($axis == 'x') { $value = round ((($value*self::$twips_per_point)/$this->twips_per_pixel_x), 2).'px'; } else { $value = round ((($value*self::$twips_per_point)/$this->twips_per_pixel_y), 2).'px'; } return $value; } public function getAbsoluteValue ($value, $base) { $unit = self::stripDigits ($value); $value = self::getDigits ($value); switch ($unit) { case '%': $value = ($value * $base)/100; break; case 'em': $value = $value * $base; break; default: // Not an relative value. Just keep it. break; } return $value; } }