<?php
/**
 * random_image.php
 * Contains a random image creator class
 */
 
/****** CONFIG ******/

$DIR = "/";  // The root directory for this site, make it blank if the site is in its web root
$THUMB_DIR = "/thumbs"; // Probably doesn't need changing
$FULL_DIR = "/full";    // Probably doesn't need changing

/**** END CONFIG ****/





define(WEB_ROOT, $DIR);
define(SRV_ROOT, $_SERVER['DOCUMENT_ROOT'].$DIR);


/**
 * Thumbnailer
 *
 * An object for generating pairs of thumbnails and links to full images from a predefined set of directories
 * example usage:
 * $thumbs = new Thumbnailer(17);
 * $thumbs->next();
 * echo $thumbs->getRandomWorkFull();
 * echo $thumbs->getRandomWorkThumb();
 * $thumbs->next();
 * echo $thumbs->GetRandomWorkFull();
 * echo $thumbs->getRandomWorkThumb();
 *
 * Would create a new Thumbnailer object with 17 images loaded and load up the first set of data.
 * The next two lines print out the URL of the full image and the thumbnail image respectively. 
 * The next line asks the Thumbnailer to move to the next set of thumbnails. Then it prints out 
 * the next pair of URLs.
 *
 * Methods of note are 
 * getRandomWorkThumb: returns the thumbnail URL for the currently loaded random item
 * getRandomWorkFull:  returns the full image URL for the currently loaded random item
 * getPixelArtThumb: returns the thumbnail URL for the currently loaded pixel art image
 * getPixelArtFull:  returns the full image URL for the currently loaded pixel art image
 * getDrawingThumb: returns the thumbnail URL for the currently loaded drawing
 * getDrawingFull:  returns the full image URL for the currently loaded drawing
 * get3dModelThumb: returns the thumbnail URL for the currently loaded 3d image
 * get3dModelFull:  returns the full image URL for the currently loaded 3d image
 * 
 *
 * @author Justen Robertson (Nphyx)
 * @version 1.0 12/14/2008
 * @license http://creativecommons.org/licenses/GPL/2.0/ CC GNU/GPL
 * @link http://www.justenrobertson.com Author's Webpage
 * @param int $num the number of images total the thumbnailer should generate
 * 
 */
 
class Thumbnailer {
	function Thumbnailer($num = 14) { // constructor
		global $THUMB_DIR;
		global $FULL_DIR;
		$this->DEBUG = false; // Show debugging output
			
		$this->THUMB_FOLDER = $THUMB_DIR;
		$this->FULL_FOLDER = $FULL_DIR;
		$this->DIR_3D_MODL = SRV_ROOT."/images/portfolio/3dmodels/thumbs";
		$this->DIR_DRAWING = SRV_ROOT."/images/portfolio/drawings/thumbs";
		$this->DIR_PIX_ART = SRV_ROOT."/images/portfolio/pixelart/thumbs";
		//$this->DIR_IMGONLY = SRV_ROOT."/images/portfolio/3dmodels/thumbs";
		
		$num_3d_modl = rand(1, $num);
		$num_drawing = rand(1, $num - $num_3d_modl);
		$num_pix_art  = $num - $num_3d_modl - $num_drawing;

		$this->files_3d_modl = $this->GetRandomFiles($this->DIR_3D_MODL, $num_3d_modl);
		$this->files_drawing = $this->GetRandomFiles($this->DIR_DRAWING, $num_drawing);
		$this->files_pix_art = $this->GetRandomFiles($this->DIR_PIX_ART, $num_pix_art);
		$this->random_work = array_merge($this->files_3d_modl, $this->files_drawing, $this->files_pix_art);
		shuffle($this->random_work);
	}

	function GetRandomFiles($dir, $num) {
		ob_start();
		echo $dir.": OPENING<br />";
		$handler = opendir($dir);
		while (false !== ($file = readdir($handler))) {
			echo "GOT FILE: $file<br />";
			$file = $dir . "/" . $file;

			//Don't list subdirectories
			if (!is_dir($file)) {
				$files []= $file;
			}
			else echo "FOLDER<br />";
		}
		//var_dump($files);
		closedir($handler);

		shuffle($files);

		$results = array();
		for ($i = 0; $i < $num; $i++) {
			$results[$i] = $files[$i];
		}
		//var_dump($results);
		$ob = ob_get_contents();
		ob_end_clean();
		if($this->DEBUG) echo $ob;
		return $results;
	}

	function Get3dModelThumb() {
		return str_replace(SRV_ROOT, WEB_ROOT, $this->big_3d_modl);
	}

	function GetDrawingThumb() {
		return str_replace(SRV_ROOT, WEB_ROOT, $this->big_drawing);
	}

	function GetPixelArtThumb() {
		return str_replace(SRV_ROOT, WEB_ROOT, $this->big_pix_art);
	}

	function GetRandomWorkThumb() {
		return str_replace(SRV_ROOT, WEB_ROOT, $this->random_item);
	}
	

	function Get3dModelFull() {
		return str_replace(SRV_ROOT, WEB_ROOT, str_replace($this->THUMB_FOLDER, $this->FULL_FOLDER, $this->big_3d_modl));
	}

	function GetDrawingFull() {
		return str_replace(SRV_ROOT, WEB_ROOT, str_replace($this->THUMB_FOLDER, $this->FULL_FOLDER, $this->big_drawing));
	}

	function GetPixelArtFull() {
		return str_replace(SRV_ROOT, WEB_ROOT, str_replace($this->THUMB_FOLDER, $this->FULL_FOLDER, $this->big_pix_art));
	}

	function GetRandomWorkFull() {
		return str_replace(SRV_ROOT, WEB_ROOT, str_replace($this->THUMB_FOLDER, $this->FULL_FOLDER, $this->random_item));
	}

	function GetRelTypes($thumbs) {
		$types = array();

		foreach ($thumbs as $index => $thumb) {
			$path = explode('/', $thumb);
			if ($path[0] == $DIR_3D_MODL) {
				$types []= "3D_MODL";
			} else if ($path[0] == $DIR_3D_ANIM) {
				$types []= "3D_ANIM";
			} else if ($path[0] == $DIR_PIX_ART) {
				$types []= "PIX_ART";
			} else {
				$types []= null;
			}
		}

		return $types;
	}
	
	function next() {
		$this->big_3d_modl = array_pop($this->files_3d_modl);
		$this->big_drawing = array_pop($this->files_drawing);
		$this->big_pix_art = array_pop($this->files_pix_art);
		if ($this->random_item = array_pop($this->random_work)) return true;
		else return false;
	}
	
	function getFileType($filename) {
		preg_match("/[^\.]*$/", $filename, $matches);
		return ($matches[0]);
	}
}