<?php


abstract class phImaginator_Collage extends phImaginator_Universal implements phImaginator_Module {


	protected static $runCollected = true;


	public static function ordered($params) {
		require_once 'phImaginator.Resize.class.php';

		self::universalCollage('ordered', $params);
	}


	public static function fitted($params) {
		require_once 'phImaginator.Box.class.php';

		self::universalCollage('fitted', $params);
	}


	private static function universalCollage($type, $params) {
		$width = $params[0];
		$height = $params[1];

		if (is_array(parent::$imageSourceCollection)) {

			$images_count = count(parent::$imageSourceCollection);

			$proportion = $width / $height;

			$imr = sqrt($images_count);
			$imr_x = round($imr * $proportion);
			$imr_y = round($images_count / $imr_x);

			$imageBox_w = $width / $imr_x;
			$imageBox_h = $height / $imr_y;

			$collageImage = imagecreatetruecolor($width, $height);

			$bgcolor = parent::transparency($collageImage, $params[2], $params[3]);

			imagefill($collageImage, 0, 0, $bgcolor);

			for ($y = 0; $y < $imr_y; ++$y) {
				for ($x = 0; $x < $imr_x; ++$x) {
					$image_data = array_shift(parent::$imageSourceCollection);
					if (!$image_data) {
						break;
					}

					if ($type == 'fitted') {
						phImaginator_Box::fit($image_data, array($imageBox_w, $imageBox_h));
					} else if ($type == 'ordered') {
						phImaginator_Resize::fitBoth($image_data, array($imageBox_w, $imageBox_h));
					}

					imagecopyresampled($collageImage, $image_data['resource'], $x * $imageBox_w, $y * $imageBox_h, 0, 0, $image_data['width'], $image_data['height'], $image_data['width'], $image_data['height']);
				}
			}

			parent::replaceSourceCollection($collageImage);
		}

	}

}







?>