<?php
include 'session.php';
if(isset($_FILES['file']['name']))
{
	$id = $_POST['id'];
      /* Getting file name */
      $name = $_POST['name'];
      $filename = "$name." . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
      $source = $filename;
      $tempPath = $_FILES['file']['tmp_name'];

      /* Location */
      $path = $_POST['path'];
      $location = $path . '/' . $filename;

      /* Extension */
      $extension = pathinfo($location,PATHINFO_EXTENSION);
      $extension = strtolower($extension);

      /* Allowed file extensions */
      $allowed_extensions = array("jpg","pdf");

      $outputQuality = 60;

      $response = array();
      $status = 0;

      /* Check file extension */
      if(in_array(strtolower($extension), $allowed_extensions)) 
      {
            if(strtolower($extension)=="jpg")
            {
                  //compressimage($tempPath,$location,$outputQuality);
                  $imageInfo = getimagesize($tempPath);
                  if($imageInfo['mime'] == 'image/jpeg')
                  {
                        $status = 1;
                        $imageLayer = imagecreatefromjpeg($tempPath);
                  }
                  else if($imageInfo['mime'] == 'image/jpg')
                  {
                        $status = 1;
                        $imageLayer = imagecreatefromjpeg($tempPath);
                  }
                  $resp = imagejpeg($imageLayer, $location, $outputQuality);
                  if($resp)
                  {
                        $bdd=new DB();
                        $q="INSERT INTO `customer_menu` (`customer_menu_id`, `customer_id`, `menu_id`, `menu_option_id`, `filename`, `added_by`) VALUES (NULL, {$id}, {$_POST['menuId']}, '', '{$filename}', '{$_SESSION['admin_id']}')";
                        
                        $row=$bdd->getOne($q);
                        
                        $bdd->disconnect();
                        $status = 1;
                        $response['path'] = $location;
                        $response['extension'] = $extension;
                        
                  }
            }
            else if(strtolower($extension)=="pdf")
            {
                  if(move_uploaded_file($_FILES['file']['tmp_name'],$location))
                  {
                        $status = 1; 
                        $response['path'] = $location;
                        $response['extension'] = $extension;
                  }
            }
      }

      $response['status'] = $status;

      echo json_encode($response);
      exit;
}

echo 0;

?>