<?php
include('../../../dbconnection/dbconnection.php');
include("../../../steam_functions/steam_functions.php");

if(isset($_POST["btn_action"])){
	if($_POST["btn_action"]==="add-gallery"){
			// $title=$_POST["etitle"];
			$desc=$_POST["desc"];
			// $set_date=$_POST["edate"];
			// $set_time=$_POST["etime"];
			// $notes=$_POST["desc"];
			/*
			$id=(int)  trim($_POST["service"]);
			if($id==""){
				$service_name="_default";
			}else{
			$service=single_service($connect,$id);
			$service_name=strtolower($service[0]["service"]);
			}
			
			$folder=$service_name;
			*/
				$folder="gallery";
							$files_name=$_FILES["file"]["name"];
							$type=$_FILES["file"]["type"];
							$tmp=$_FILES["file"]["tmp_name"];
							$tmpp=$_FILES["file"]["tmp_name"];
							$original=$_FILES['file']["name"];
							$profile=$_FILES['file']["name"];
							$directory=$folder."/";
							$ext=strtolower(end(explode(".",$files_name)));
							$allowed_all=array("jpg","jpeg","png","gif");
							$allowed_resize=array("jpg","jpeg","png");
							create_directory($folder);
							if(!empty(@getimagesize($tmp))){
									$ofile="large-".date(time()).time().".".$ext;
									$file="cover-".date(time()).time().".".$ext;
									$large=strtolower($ofile);
									$profile=strtolower($directory.$file);
									if(in_array($ext,$allowed_all)){
										list($width,$height)=getimagesize($tmp);
										// if($width>200 || $height>190){
												if(in_array($ext,$allowed_resize)){
													//if($ext=="png"){$tmp=imagecreatefrompng($tmp);}
													//if($ext=="jpg"){$tmp=imagecreatefromjpeg($tmp);}
												//if($ext=="jpeg"){$tmp=imagecreatefromjpeg($tmp);}
												//$profile=resize($width,$height,$tmp,$profile);	
												 move_uploaded_file($tmpp,$profile);
												}
										
										}
										else{
											if(!empty($tmp)){
												move_uploaded_file($tmp,$profile);
												// move_uploaded_file($tmpp,$large);;
											}
											
										}
									/*
									}
									else {
										move_uploaded_file($tmp,$profile);
										// move_uploaded_file($tmpp,$large);
									}
									*/
									$user=$_SESSION['id'];
									
									$query = "
										INSERT INTO `events_gallery` (user,pic,	details,date,status) 
													VALUES (:user,:pic,:det,:date,:status)
										";
										
										$statement=$connect->prepare($query);
										$statement->execute(
											array(
												":user" 		=>	$user,
												":pic" 			=>	$profile,
												":det"			=>	$desc, 
												":date"			=>	time(),
												":status"		=>	1
											)
										);
										
										$result = $statement->fetchAll();
											if(isset($result))
											{
												
												
												
												echo 'Gallery Created';
												
												
											}
									
									
				}else{
					
					echo "Image file Error";
				}
		
			
	}
	
	
}

		function resize($width,$height,$img,$store){
			///*
		if($width>$height){
		$new_height=($height/$width )*350;
		$new_width=350;}
		else{
			$new_height=350;
			$new_width=($width/$height)*350;
		}
		//*/
		//$new_width=200;
		//$new_height=190;
		$tmp_img=imagecreatetruecolor($new_width,$new_height);
		imagecopyresampled($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
		if(imagepng($tmp_img,$store,1)){
				chmod($store, 0777);
			return $file_name=$store;
			}
		if(imagejpeg($tmp_img,$store,100)){
				chmod($store, 0777);
		return 	$file_name=$store;
			}
	}
	
function create_directory($folda){
	
		if(!is_dir($folda)){mkdir(strtolower($folda));}
		
}

?>