<?php
/**
 * Klasa do przetwarzania i zapisywania różnych rozmiarów obrazka
 *
 * @author arson
 */
class Eppe_Image_Thumbs {

	const CROP = 'crop';
	const RESIZE = 'resize';
	
	/**
	 * Fabryka 
	 * @param string $filePath
	 * @return Eppe_Image_Thumbs
	 */
	static public function factory($filePath) {
		return new static($filePath);
	}

	protected $sourceFilePath;
	protected $thumbDefs;

	public function __construct($filePath) {
		$this->sourceFilePath = $filePath;
	}

	/**
	 * Ustawia definicje miniaturek. Definicji może być dowolna ilość.
	 * 
	 * @param array $thumbDefs array('NAZWA'=>array('width'=>SZEROKOŚĆ,'height'=>WYSOKOŚĆ))
	 * @return \Eppe_Image_Thumbs 
	 */
	public function setThumbDefs(array $thumbDefs) {
		$this->thumbDefs = $thumbDefs;
		return $this;
	}

	/**
	 * Robi resize i zapisuje 
	 * 
	 * @param string $path Ścieżka, gdzie mają być zapisane przetworzone obrazki
	 */
	public function saveThumbs($path) {
		foreach ($this->thumbDefs as $name => $options) {
			$phpThumb = Vendors_PhpThumb_PhpThumbFactory::create($this->sourceFilePath);

			if(isset($options['method']) AND $options['method']==self::CROP){
				$phpThumb->adaptiveResize($options['width'], $options['height']);
			}
			else{
				$phpThumb->resize($options['width'], $options['height']);
			}

			$filename = Eppe_Tools::getFilenameFromPath($phpThumb->getFileName());
			$filename = $name . '_' . $filename;

			$phpThumb->save($path . '/' . $filename);

			unset($phpThumb);
		}
	}

}
?>
