<?php

if (isset($_POST['img_submit'])) {



    $note_ref=$_POST['note_ref'];

    $img_pos=$_POST['img_pos'];



    $j = $img_pos; //Variable for indexing uploaded image 

    $images_data = array();



    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

        

        $target_path = "../upload/notes/"; //Declaring Path for uploaded images

        $validextensions = array("jpeg", "jpg", "png", "gif");  //Extensions which are allowed

        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 

        $file_extension = end($ext); //store extensions in the variable



        $new_name = md5(uniqid()) . "." . $ext[count($ext) - 1];//set a new name of image



        $target_path = $target_path .$new_name ;//set the target path with a new name of image



        $j = $j + 1;//increment the number of uploaded images according to the files in array       

        

      if (($_FILES["file"]["size"][$i] < 1000000) && in_array($file_extension, $validextensions))  //Approx. 1000kb files can be uploaded.

          {



            $temp_name = $_FILES['file']['tmp_name'][$i];//set a Temp name of image

            $imageQuality= 20;



            if (compress_image($temp_name, $target_path, $imageQuality)) {//if Compressed file moved to uploads folder

                //array_push($images_data, $new_name);



                insertImageDB($con,$j,$new_name,$note_ref);



                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';

            } else {//if file was not moved.

                echo $j. ').<span id="error">please try again!.</span><br/><br/>';

            }





        } else {//if file size and file type was incorrect.

            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';

        }



    }



}



function compress_image($tempPath, $originalPath, $imageQuality){

  

    // Get image info 

    $imgInfo = getimagesize($tempPath); 

    $mime = $imgInfo['mime']; 

     

    // Create a new image from file 

    switch($mime){ 

        case 'image/jpeg': 

            $image = imagecreatefromjpeg($tempPath); 

            break; 

        case 'image/png': 

            $image = imagecreatefrompng($tempPath); 

            break; 

        case 'image/gif': 

            $image = imagecreatefromgif($tempPath); 

            break; 

        default: 

            $image = imagecreatefromjpeg($tempPath); 

    } 

     

    // Save image 

    imagejpeg($image, $originalPath, $imageQuality);    

    // Return compressed image 

    return $originalPath; 



}



function insertImageDB($con,$pos,$insImg,$noteRef){  



        $img_sql = "INSERT INTO notes_img (ni_name,ni_pos, ni_ref) VALUES ('$insImg','$pos','$noteRef')";

        if(mysqli_query($con, $img_sql)){

            ?>

            <script>

                alert('Submit Successfully');

                window.location = "edit-notes.php?edit_ref=<?php echo $noteRef; ?>";

            </script>

            <?php

        }

}



?>