<?php

abstract class phImaginator_Universal {


	protected static $imageSourceCollection = array();
	protected static $fontFile, $fontSize, $fontColor, $fontOpacity;

	private static $cachedData = array();


	private static $imageResources = array(
		'rounded_corner' => '[encoded data, 10544 characters]'
	);


	private static $currentResource;


	public static function assignImageSourceCollection(&$imageSourceCollection) {
		self::$imageSourceCollection =& $imageSourceCollection;
	}


	public static function ifRunCollected() {
		return static::$runCollected;
	}


	public static function transparency(&$image_resource, $color = '#000000', $opacity = 100, $tMax = 127) {
		if (!is_array($color)) {
			$color = sscanf($color, '#%2x%2x%2x');
		}

		/*
		$tMax = ($tMax == 127 || $tMax == 100) ? $tMax : 127;
		$transparency = $tMax - (($tMax / 100) * $opacity);
		*/
		$transparency = self::calculateTransparency($opacity, $tMax);

		$bgcolor = imagecolorallocatealpha($image_resource, $color[0], $color[1], $color[2], $transparency);
		imagecolortransparent($image_resource, $bgcolor);

		return $bgcolor;
	}


	public static function calculateTransparency($opacity = 100, $tMax = 127) {
		$tMax = ($tMax == 127 || $tMax == 100) ? $tMax : 127;
		$transparency = $tMax - (($tMax / 100) * $opacity);

		return $transparency;
	}


	public static function replaceSourceCollection($image_resource) {
		self::$imageSourceCollection = array();

		self::addToSourceCollection($image_resource);
	}


	public static function addToSourceCollection($image_resource) {
		if (!is_array(self::$imageSourceCollection)) {
			self::$imageSourceCollection = array();
		}

		self::$imageSourceCollection[] = array(
			'resource' => $image_resource,
			'width' => imagesx($image_resource),
			'height' => imagesy($image_resource),
			'name' => uniqid(mt_rand(), true)
		);
	}


	public static function setFont($fontFile, $size, $color, $opacity) {
		self::$fontFile = $fontFile;
		self::$fontSize = $size;
		self::$fontColor = $color;
		self::$fontOpacity = $opacity;
	}


	public static function getFontFile() {
		return self::$fontFile;
	}


	public static function getFontSize() {
		return self::$fontSize;
	}


	public static function getFontColor() {
		return self::$fontColor;
	}


	public static function getFontOpacity() {
		return self::$fontOpacity;
	}


	public static function getElementPositions($xpos, $ypos, $image_data, $elementWidth, $elementHight) {
		switch ($xpos) {
			case phImaginator::POSITION_LEFT:
				$pos_x = 0;

				break;

			case phImaginator::POSITION_CENTER:
				$pos_x = ($image_data['width'] - $elementWidth) / 2;

				break;

			case phImaginator::POSITION_RIGHT:
				$pos_x = $image_data['width'] - $elementWidth;

				break;

			case $xpos >= 0:
				$pos_x = $xpos;

				break;

			case $xpos < 0:
				$pos_x = ($image_data['width'] - $elementWidth) + $xpos;

				break;
		}

		switch ($ypos) {
			case phImaginator::POSITION_TOP:
				$pos_y = 0;

				break;

			case phImaginator::POSITION_MIDDLE:
				$pos_y = ($image_data['height'] - $elementHight) / 2;

				break;

			case phImaginator::POSITION_BOTTOM:
				$pos_y = $image_data['height'] - $elementHight;

				break;

			case $ypos >= 0:
				$pos_y = $ypos;

				break;

			case $ypos < 0:
				$pos_y = ($image_data['height'] - $elementHight) + $ypos;

				break;
		}

		return array('x' => $pos_x, 'y' => $pos_y);
	}


	public static function computeElementSizes($image_data, $width, $height, $element_width = null, $element_height = null) {
		switch ($width) {
			case !$width:
				break;

			case phImaginator::FITTING_WIDTH:
				$computed_width = $image_data['width'];

				$computed_height = $element_height * ($image_data['width'] / $element_width);

				break;

			case phImaginator::FITTING_HEIGHT:
				$computed_height = $image_data['height'];

				$computed_width = $element_width * ($image_data['height'] / $element_height);

				break;

			case phImaginator::FITTING_BOTH:
				$computed_width = $image_data['width'];
				$computed_height = $image_data['height'];

				break;

			default:
				if (substr($width, -1, 1) == '%') {
					settype($width, "integer");

					$computed_width = $image_data['width'] / 100 * $width;
				} else if (is_numeric($width)) {
					$computed_width = $width;
				}

				break;
		}

		if ($height) {
			if (substr($height, -1, 1) == '%') {
				settype($height, "integer");

				$computed_height = $image_data['height'] / 100 * $height;
			} else if (is_numeric($height)) {
				$computed_height = $height;
			}
		}

		return array('w' => $computed_width, 'h' => $computed_height);
	}


	public static function getCachedData($name) {
		return self::$cachedData[$name];
	}


	public static function cacheData($name, $data) {
		self::$cachedData[$name] = $data;
	}


	public static function getImageResource($resource_name) {
		return self::$imageResources[$resource_name];
	}


	public static function setCurrentResource(&$image_data) {
		self::$currentResource =& $image_data;
	}


	public static function &getCurrentResource() {
		return self::$currentResource;
	}
}

?>