<?php
/**
 * R&S Imagination Suite
 * Mask Class
 *
 * blah fasel description
 *
 * @author Darius A. Zylka <rs@regen-sonne.de>
 * @copyright (c) 2009 Darius A. Zylka
 * @version 1.0
 * @package phImaginator
 * @subpackage phImaginator_Mask
 */


/**
 * Mask class
 *
 * @package phImaginator
 * @subpackage phImaginator_Mask
 *
 */
abstract class phImaginator_Mask extends phImaginator_Universal implements phImaginator_Module {


	/**
	 * Defines if the desired method of this class should be used for each source element or for all elements at once
	 *
	 * @access protected
	 * @var bool
	 */
	protected static $runCollected = false;

	/**
	 * Stamps an image with another image with a defined opacity
	 *
	 * @param array $image_data Image data array from the image resource collection
	 * @param array $params Submitted parameters
	 * @uses phImaginator::$imageSourceCollection
	 */
	public static function stamp($stamp_file, $position_horizontal, $position_vertical, $opacity, $fitting = false) {
		$image_data =& parent::getCurrentResource();

		$cache_name = serialize(array(__METHOD__, $stamp_file, $position_horizontal, $position_vertical, $opacity, $fitting));

		$cached_data = parent::getCachedData($cache_name);

		if ($cached_data) {
			$stamp_w = $cached_data[1];
			$stamp_h = $cached_data[2];

			$stamp_mod = $cached_data[0];
		} else {
			$stamp = imagecreatefrompng($stamp_file);

			$stamp_w = imagesx($stamp);
			$stamp_h = imagesy($stamp);

			$stamp_mod = imagecreatetruecolor($stamp_w, $stamp_h);

			$colortrans = parent::transparency($stamp_mod, array(127, 127, 127), $opacity);
			imagefill($stamp_mod, 0, 0, $colortrans);

			imagelayereffect($stamp_mod, IMG_EFFECT_OVERLAY);

			imagecopyresampled($stamp_mod, $stamp, 0, 0, 0, 0, $stamp_w, $stamp_h, $stamp_w, $stamp_h);

			parent::cacheData($cache_name, array($stamp_mod, $stamp_w, $stamp_h));
		}

		$stampSizes['w'] = $stamp_w;
		$stampSizes['h'] = $stamp_h;

		if ($fitting) {
			$stampSizes = parent::computeElementSizes($image_data, $fitting, null, $stamp_w, $stamp_h);
		}
		$stampPos = parent::getElementPositions($position_horizontal, $position_vertical, $image_data, $stampSizes['w'], $stampSizes['h']);

		imagecopyresampled($image_data['resource'], $stamp_mod, $stampPos['x'], $stampPos['y'], 0, 0, $stampSizes['w'], $stampSizes['h'], $stamp_w, $stamp_h);
	}


	public static function corners($corner_radius) {
		$image_data =& parent::getCurrentResource();

		$w = $image_data['width'];
		$h = $image_data['height'];


		$mask = imagecreatetruecolor($w, $h);
		imageantialias($mask, true);
		imageantialias($image_data['resource'], true);

		$transColor = imagecolorallocatealpha($mask, 255, 0, 255, 127);
		imagefill($mask, 0, 0, $transColor);
		imagecolortransparent($mask, $transColor);

		$overlayColor = imagecolorallocate($mask, 127, 127, 127);

		imagefilledellipse($mask, $corner_radius, $corner_radius, $corner_radius * 2, $corner_radius * 2, $overlayColor);
		imagefilledellipse($mask, $corner_radius, $h - $corner_radius, $corner_radius * 2, $corner_radius * 2, $overlayColor);
		imagefilledellipse($mask, $w - $corner_radius, $corner_radius, $corner_radius * 2, $corner_radius * 2, $overlayColor);
		imagefilledellipse($mask, $w - $corner_radius, $h - $corner_radius, $corner_radius * 2, $corner_radius * 2, $overlayColor);
		imagefilledrectangle($mask, $corner_radius, 0, $w - $corner_radius, $h, $overlayColor);
		imagefilledrectangle($mask, 0, $corner_radius, $w, $h - $corner_radius, $overlayColor);

		imagelayereffect($mask, IMG_EFFECT_OVERLAY);

		imagecopyresampled($mask, $image_data['resource'], 0, 0, 0, 0, $w, $h, $w, $h);

		$image_data['resource'] = $mask;
	}


	public static function betterCorners($corner_radius, $positions = POSITION_ALL) {
		$image_data = parent::getCurrentResource();

		$cache_name = serialize(array(__METHOD__, $corner_radius));

		$cached_data = parent::getCachedData($cache_name);

		if ($cached_data) {
			$corner2 = $cached_data[0];
		} else {
			$rounded_corner = parent::getImageResource('rounded_corner');

			$corner = imagecreatefromstring(base64_decode($rounded_corner));


			$corner2 = imagecreatetruecolor($corner_radius * 2, $corner_radius * 2);
			imagealphablending($corner2, false);
			imagesavealpha($corner2, true);

			imagecopyresampled($corner2, $corner, 0, 0, 0, 0, $corner_radius * 2, $corner_radius * 2, imagesx($corner), imagesy($corner));

			parent::cacheData($cache_name, array($corner2));
		}

		$w = $image_data['width'];
		$h = $image_data['height'];

		imagelayereffect($image_data['resource'], IMG_EFFECT_OVERLAY);

		if (phImaginator::POSITION_LEFT_TOP & $positions || phImaginator::POSITION_ALL & $positions || phImaginator::POSITION_TOP & $positions || phImaginator::POSITION_LEFT & $positions) {
			imagecopyresampled($image_data['resource'], $corner2, 0, 0, 0, 0, $corner_radius, $corner_radius, $corner_radius, $corner_radius);
		}
		if (phImaginator::POSITION_LEFT_BOTTOM & $positions || phImaginator::POSITION_ALL & $positions || phImaginator::POSITION_BOTTOM & $positions || phImaginator::POSITION_LEFT & $positions) {
			imagecopyresampled($image_data['resource'], $corner2, 0, $h - $corner_radius, 0, $corner_radius, $corner_radius, $corner_radius, $corner_radius, $corner_radius);
		}
		if (phImaginator::POSITION_RIGHT_TOP & $positions || phImaginator::POSITION_ALL & $positions || phImaginator::POSITION_TOP & $positions || phImaginator::POSITION_RIGHT & $positions) {
			imagecopyresampled($image_data['resource'], $corner2, $w - $corner_radius, 0, $corner_radius, 0, $corner_radius, $corner_radius, $corner_radius, $corner_radius);
		}
		if (phImaginator::POSITION_RIGHT_BOTTOM & $positions || phImaginator::POSITION_ALL & $positions || phImaginator::POSITION_BOTTOM & $positions || phImaginator::POSITION_RIGHT & $positions) {
			imagecopyresampled($image_data['resource'], $corner2, $w - $corner_radius, $h - $corner_radius, $corner_radius, $corner_radius, $corner_radius, $corner_radius, $corner_radius, $corner_radius);
		}
	}
}







?>