<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	$page_id	=	base64_decode(base64_decode(base64_decode(base64_decode($_GET['page_id'])))); //THIS IS PAGE ID 
if($page_id==1) //this is for basic details add page	
{ 		
		
		$cnt1 = $_POST['cur_count1'];
		for($x=1;$x<=$cnt1;$x++)
		{
				$PATH_photo	= "../../slider_images/";
				$SLCT = "SELECT  * FROM slider_image ORDER BY slider_image_id ASC";
				$QURY = mysqli_query($connect,$SLCT);
				if(mysqli_num_rows($QURY)> 0)
					{
						$SLCT_sk = "SELECT  MAX(slider_image_id) as max_ids FROM slider_image ORDER BY slider_image_id ASC";
						$QURY_sk = mysqli_query($connect,$SLCT_sk);  
						$FTCH_sk = mysqli_fetch_object($QURY_sk);
						$max_id = $FTCH_sk->max_ids + 1;
						
						$photo = substr($_FILES["sli_img"]["name"][$x], strrpos($_FILES["sli_img"]["name"][$x], '.') + 1);
						$photo = strtolower($photo);
						$pho_ext=$max_id.".".$photo;
					}
					else
					{
						$photo = substr($_FILES["sli_img"]["name"][$x], strrpos($_FILES["sli_img"]["name"][$x], '.') + 1);
						$photo = strtolower($photo);
						$pho_ext=$max_id.".".$photo;
						$max_id = 1;	
					}
					$ext = strtolower($_FILES['sli_img']['type'][$x]);
					switch(strtolower($_FILES['sli_img']['type'][$x]))
						{
							case 'image/jpeg':
								$image = imagecreatefromjpeg($_FILES['sli_img']['tmp_name'][$x]);
								break;
							case 'image/png':
								$image = imagecreatefrompng($_FILES['sli_img']['tmp_name'][$x]);
								break;
							case 'image/gif':
								$image = imagecreatefromgif($_FILES['sli_img']['tmp_name'][$x]);
								break;
							case 'image/bmp':
								$image = imagecreatefromgif($_FILES['sli_img']['tmp_name'][$x]);
							default:
								$image=imagecreatefromjpeg($_FILES['sli_img']['tmp_name'][$x]);
						}
						$old_width  = imagesx($image);
						$old_height = imagesy($image);
						$size_width=array(2000,600,60);
						$size_hieght=array(2000,600,60);
						$prod_size=array("L","M","S");
						for($l=0;$l<=2;$l++)
							{
								$max_width = $size_width[$l];
								$max_height = $size_hieght[$l];				
								// Get current dimensions
								// Calculate the scaling we need to do to fit the image inside our frame
								$scale = min($max_width/$old_width, $max_height/$old_height);						
								// Get the new dimensions
								$new_width  = ceil($scale*$old_width);
								$new_height = ceil($scale*$old_height);
								// Create new empty image
								$new = imagecreatetruecolor($new_width, $new_height);
								// Resize old image into new
								imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
								imagealphablending($new, false);
								imagesavealpha($new, true);
								imagecopyresampled($new, $image,0, 0, 0, 0,$new_width, $new_height, $old_width, $old_height);
								// Catch the imagedata
								ob_start();
								//echo $ext;			
								switch ($ext)
								{
									case 'image/gif':
										//imagegif($im, $fileName);
										imagegif($new,"$PATH_photo/$prod_size[$l].$max_id.gif", 90);
										break;
									case 'image/jpg':								
									case 'image/jpeg':
										//imagejpeg($im, $fileName, $quality);
										imagejpeg($new,"$PATH_photo/$prod_size[$l].$max_id.jpg", 90);
										imagejpeg($new,"$PATH_photo/$prod_size[$l].$max_id.jpeg", 90);
										break;
									case 'image/png':
										//imagepng($im, $fileName);
										imagepng($new,"$PATH_photo/$prod_size[$l].$max_id.png", 8);
										break;
									case 'image/bmp':
										//imagewbmp($im, $fileName);
										imagewbmp($new,"$PATH_photo/$prod_size[$l].$max_id.bmp", 90);
										break;
									default:
										break;
								}	
							}
						$INSRT		= "INSERT INTO slider_image(`description`,`imag_ext`,`post_dt`) VALUES ('".$_POST['content'][$x]."','".$photo."','".$cur_time."')";
						//echo $INSRT;exit;
						$QURY		= mysqli_query($connect,$INSRT);
						header ("location:../img_slider_list.php");
			}
		
}
else if($page_id==2) //this is for basic details list page	
{
	$sli_id	= base64_decode(base64_decode(base64_decode(base64_decode($_GET['sli_id']))));
	$statu	= base64_decode(base64_decode(base64_decode(base64_decode($_GET['statu_s']))));
	
	if($statu=="active")
	{
		$UPDATE = "UPDATE slider_image SET statu_s = 'inactive',update_dt = '$cur_time' WHERE slider_image_id = '$sli_id'";
		$QRY    = mysqli_query($connect,$UPDATE); 
	}
	else if($statu=="inactive")
	{
		$UPDATE = "UPDATE slider_image SET statu_s = 'active',update_dt = '$cur_time' WHERE slider_image_id = '$sli_id'";
		$QRY    = mysqli_query($connect,$UPDATE); 
	}
	?>
  			<script type="text/javascript">
    		//alert("Data inserted successfully");
    		//history.back();
   			window.location='../img_slider_list.php';
  			</script>
   <?php
}
?>
	
