class_mcUnityCalculator.inc.php 2.51 KB
<?php

class mcUnityCalculator  {

	public static $UNITS = array(
		'in' => array(
			'mm'	=>	25.4, 
			'cm'	=>	2.54, 
			'in'	=>	1, 
			'pt'	=>	72, 
			'pc'	=>	6, 
			'px'	=>	false, 			
		)
	);
	
	/**
	 * transform $param_in_number $param_in_unit to $param_out_unit
	 * 
	 * @param number $param_in_number
	 * @param string $param_in_unit
	 * @param string $param_out_unit
	 * @return number
	 */
	public static function to($param_in_number, $param_in_unit, $param_out_unit){
		$inchNumber = mcUnityCalculator::toInch($param_in_number, $param_in_unit);
		$outNumber = mcUnityCalculator::inchTo($inchNumber, $param_out_unit);
		return $outNumber;
	}
	
	public static function calculateDpi($param_px_number, $param_number, $param_unit) {
		$inch_outNumber = mcUnityCalculator::toInch($param_number, $param_unit);
		
		$dpi = $param_px_number / $inch_outNumber;
		
		return $dpi;
		
	}
	
	/**
	 * 
	 * @param type $param_number - number in inch / zahl in zoll
	 * @param string $param_unit - mm / cm / in / pt / pc / px
	 * @return type
	 */
	private static function inchTo($param_number, $param_unit, $param_dpi=300) {
		if(array_key_exists($param_unit, mcUnityCalculator::$UNITS['in'])) {
			if($param_unit != 'px') {
				return $param_number * mcUnityCalculator::$UNITS['in'][$param_unit];
				
			} else {
				return mcUnityCalculator::inchToPx($param_number, $param_dpi);
			}
			
		} else {
			return Error::newError('falsche Einheit', '');
		}
	}
	/**
	 * 
	 * @param type $param_number - number in inch / zahl in zoll
	 * @param string $param_unit - mm / cm / in / pt / pc / px
	 * @return type
	 */	
	private static function toInch($param_number, $param_unit, $param_dpi=300)  {
		if(array_key_exists($param_unit, mcUnityCalculator::$UNITS['in'])) {
			if($param_unit != 'px') {
				return $param_number / mcUnityCalculator::$UNITS['in'][$param_unit];
				
			} else {
				return mcUnityCalculator::pxToInch($param_number, $param_dpi);
			}
		} else {
			return Error::newError('falsche Einheit', '');
		}
	}

//	/**
//	 * 
//	 * @param type $param_number
//	 * @param type $param_unit
//	 * @param type $param_dpi
//	 * @return type
//	 */
//	public static function toPx($param_number, $param_unit, $param_dpi) {
//		$inch = mcUnityCalculator::toInch($param_number, $param_unit);
//		$px = mcUnityCalculator::inchToPx($inch, $param_dpi);
//		return $px;
//	}

	private static function pxToInch($param_px, $param_dpi) {
		return $param_px / $param_dpi;
	}
	
	private static function inchToPx($param_inch, $param_dpi)  {
		return $param_inch * $param_dpi;
	}	
	
}
?>