<?php 
class gallery_image{
	function fn_gallery_insert($conn){
      $sm_img=$_FILES['sm_image']['name'];
      $sm_file_tmp = $_FILES['sm_image']['tmp_name'];
	  $bg_img=$_FILES['bg_image']['name'];
      $bg_file_tmp = $_FILES['bg_image']['tmp_name'];
	  $sm_temp_dest='temp/small_img';
	   $bg_temp_dest='temp/big_img';
	  $sm_file_name='sm_img_'.date('dmYHims').'.jpg';
	  $bg_file_name='bg_img_'.date('dmYHims').'.jpg';
	  $sm_img_target= $sm_temp_dest.'/'. $sm_file_name;
	  $bg_img_target= $bg_temp_dest.'/'. $bg_file_name;
	  $final_dest='userdata';
	  if(is_dir($sm_temp_dest)==FALSE){
		mkdir($sm_temp_dest,0777,TRUE);
		
	}
	if(is_dir($bg_temp_dest)==FALSE  ){mkdir($bg_temp_dest,0777,TRUE);}
	 if(is_dir($final_dest.'/sm_img')==FALSE){
		mkdir($final_dest.'/sm_img',0777,TRUE);
	}
	if(is_dir($final_dest.'/bg_img')==FALSE){
		mkdir($final_dest.'/bg_img',0777,TRUE);
	}
	
	if(move_uploaded_file($sm_file_tmp,$sm_img_target)){
		
		//$file = "temp/small_img" . $ins_img;
			   list($width, $height) = getimagesize($sm_img_target);
			    $modwidth = 640;
	   $modheight = 468;
	    $tn = imagecreatetruecolor($modwidth, $modheight) ;
		$image = imagecreatefromjpeg($sm_img_target) ;
		
		 imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
		 if(imagejpeg($tn,$final_dest.'/sm_img/'.$sm_file_name, 80)){
			 unlink($sm_img_target);
				if(move_uploaded_file($bg_file_tmp,$bg_img_target)){
		
		//$file = "temp/small_img" . $ins_img;
			   list($width, $height) = getimagesize($bg_img_target);
			    $modwidth = 640;
	   $modheight = 468;
	    $tn = imagecreatetruecolor($modwidth, $modheight) ;
		$image = imagecreatefromjpeg($bg_img_target) ;
		
		 imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
		 
		 if(imagejpeg($tn,$final_dest.'/bg_img/'.$bg_file_name, 80)){
			
			$gallery_img_insert_query="insert into tbl_gallery_img (gt_id, small_img, big_img, title, title_desc, created_date) values('".$_POST['gtitle']."','".$sm_file_name."',
			'".$bg_file_name."','".$_POST['p_title']."','".$_POST['p_title_desc']."','".date('Y-m-d H:i:s')."')"; 
			$exec_gallery_img_insert_query=mysqli_query($conn,$gallery_img_insert_query) or die("error");
		if($exec_gallery_img_insert_query){
			 unlink($bg_img_target);
				return 1;
			}
			else{
				return 0;
			}
			return $gallery_img_insert_query;
		 }
		 else{
			return 0;  
		 }
	}
	else{
		return 0;
	}
		 }
		 else{
			return 0;  
		 }
	}
	else{
		return 0;
	}
	}
}
include_once('connection.php');
$obj_gi=new gallery_image();
$result=$obj_gi->fn_gallery_insert($conn);



$json = array( "op" =>$result);
		/* Send as JSON */
     header("Content-Type: application/json", true);
		echo json_encode($json);
		?>