<?php
/**
 * $Id: PhotoDAL.class.php 1144 2008-08-19 08:10:12Z pawy $
 * Enter description here...
 *
 */
class PhotoDAL {

	/**
	 * Metoda tworzy katalogi
	 *
	 * @param string $dir sciezka katalog�w
	 * @param int $mode prawa zapisu
	 * @param bool $recursive czy ma tworzy� katalogi rekursywnie
	 * @return bool
	 */
	public static function mkdirs($dir, $mode = 0777, $recursive = true) {
	  if( is_null($dir) || $dir === "" ){
	   return FALSE;
	  }
	  if( is_dir($dir) || $dir === DIRECTORY_SEPARATOR ){
	   return TRUE;
	  }
	  if( PhotoDAL::mkdirs(dirname($dir), $mode, $recursive) ){
	   return mkdir($dir, $mode);
	  }
	  return FALSE;
	}
	
    public static function SimplePhotoUpload($sourceFileArray, $path, $dimnsionX = null, $dimensionY = null, $quality = 90) {
			
			$directory = PATH_STATIC_CONTENT . $path;
			$url = $path . '/';

			PhotoDAL::mkdirs($directory);

			if(file_exists($directory . time() . $sourceFileArray['name'])) {
				unlink($directory . time() . $sourceFileArray['name']);
			}
			//$type = explode("/", $sourceFileArray['type'][$key]);
			//if($type[1] == "jpeg") $type[1] = "jpg";

			$destinationFilePath = $directory . $sourceFileArray['name'];
			$destinationFileUrl = $url . $sourceFileArray['name'];
			
			move_uploaded_file($sourceFileArray['tmp_name'], $destinationFilePath);

            if($dimensionY!=null && $dimnsionX!=null) {
                $size = getimagesize($destinationFilePath);
			
				$imgOryginal = new mImage($destinationFilePath);
				$imgOryginal->scaleProp($dimnsionX, $dimensionY, 'ffffff', false, 'c', 0);
				$imgOryginal->saveToFile($destinationFilePath, 'jpg', $quality, true);
            }

			if (file_exists($destinationFilePath)) {
				return $destinationFileUrl;
			} else {
				return false;
			}
    }

	/**
	 * Przenosi tymczasowo zapisane zdjęcie do zdjęć zapisanych na stałe,
	 * przy okazji obcinając je wedle wskazań croppera, i resizując do
	 * docelowego rozmiaru.
	 *
	 * @param string $fileName
	 * @param int $x
	 * @param int $y
	 * @param int $width
	 * @param int $height
	 * @param int $destWidth
	 * @param int $destHeight
	 */
	public static function SaveTempFile($fileName, $destFile, $destDir, $x, $y, $width, $height, $destWidth, $destHeight) {
		$file = PATH_STATIC_CONTENT . DIRECTORY_SEPARATOR . $fileName;

		$destDir = PATH_STATIC_CONTENT . DIRECTORY_SEPARATOR . $destDir;

		//Utils::ArrayDisplay($destDir);
		PhotoDAL::mkdirs($destDir);

		$destFileN = $destFile;
		$destFile = $destDir . DIRECTORY_SEPARATOR . $destFile;

		$image = new mImage($file);
		$image->crop1($x, $y, $width, $height);
		$image->scaleProp($destWidth, $destHeight, 'ffffff', false, 'c', 0);
		$image->saveToFile($destFile . ".jpg", 'jpg', 90, true);

		return $destFileN . ".jpg";
	}
	
	
	
	/**
	 * Tworzy widok
	 */
	public static function MakeimgTitle($file, $string, $layout, $sizeImg) {
		
		//$string = self::PLttf($string);
		//Utils::ArrayDisplay($string);

		//header('Content-Type: text/html; charset=utf-8');
		$arrayLayout = array(
		1 => array('x' => '', 'y' => ''),
		2 => array('x' => 100, 'y' => 100),
		3 => array('x' => 120, 'y' => 100),
		4 => array('x' => 100, 'y' => 180),
		5 => array('x' => 100, 'y' => 20) );
		$im = imagecreate(400, 100);
		// White background and blue text
		$bg = imagecolorallocate($im, 255, 255, 255);
		$textcolor = imagecolorallocate($im, 0, 0, 0);
		if (is_file(PATH_STATIC_CONTENT.$file)) {
			$src = imagecreatefromjpeg(PATH_STATIC_CONTENT.$file);
		}
		//$string = iconv("utf-8", "utf-8", $string);
		$string = html_entity_decode($string);
		//Utils::ArrayDisplay($string);
		

		// Copy and merge
		//1
		if ($layout == 1) {
			imagecopymerge($im, $src, 25, 25, 0, 0, $sizeImg[0], $sizeImg[1], 90);
		}
		elseif ($layout == 2) {
			//imagecopymerge($im, $src, 25, 25, 0, 0, $sizeImg[0], $sizeImg[1], 90);
			//imagestring($im,8, 25, 45, $string ,$textcolor);
			imagettftext ( $im, 16, 0, 25, 55, $textcolor, PATH_CORE.'/plugins/arial.ttf', $string );
		}
		elseif ($layout == 3) {
			//3
			imagecopymerge($im, $src, 25, 25, 0, 0, $sizeImg[0], $sizeImg[1], 90);
			imagettftext ( $im, 16, 0, 25+$sizeImg[0]+20, 55, $textcolor, PATH_CORE.'/plugins/arial.ttf', $string );
			//imagestring($im,8, 25+$sizeImg[0]+20, 45, $string ,$textcolor);
		}
		elseif ($layout == 4) {
			imagecopymerge($im, $src, 25, 10, 0, 0, $sizeImg[0], $sizeImg[1], 90);
			imagettftext ( $im, 16, 0, 25, 85, $textcolor, PATH_CORE.'/plugins/arial.ttf', $string );
			//imagestring($im,8, 25, 65, $string ,$textcolor);
		}
		elseif ($layout == 5) {
			imagecopymerge($im, $src, 25, 40, 0, 0, $sizeImg[0], $sizeImg[1], 90);
			imagettftext ( $im, 16, 0, 25, 25, $textcolor, PATH_CORE.'/plugins/arial.ttf', $string );
			//imagestring($im,8, 25, 20, $string ,$textcolor);
		}

		//imagestring($im,8, 25+$sizeImg[0]+20, 45, $string ,$textcolor);
		//2



		//4

		//5
		//imagettftext ( $im, 16, 0, 25+$sizeImg[0]+20, 45, $textcolor, PATH_CORE.'/plugins/arial.ttf', $string );
		$destName = md5('photo' . time()).".jpg";
		// Output and free from memory
		//header('Content-Type: image/jpeg');
		imagejpeg($im, PATH_STATIC_CONTENT.'creator/'.$destName );

		imagedestroy($im);
		if (isset($src)) {
			imagedestroy($src);
		}
		return $destName;
	}

	/**
	 * Pusty konstruktor
	 *
	 */
	public function __construct() {
		
	}
}
?>