<?php
$target_dir = "ScanedImagesTemp/";
function compress_image($source_url, $destination_url, $quality)
        {
            $info = getimagesize($source_url);
            if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
            elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
            elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);
            imagejpeg($image, $destination_url, $quality);

            echo "Image uploaded successfully.";
            return true;
        }
        $filesa=glob('ScanedImagesTemp/*');
        foreach($filesa as $fils)
        {
            if(is_file($fils)){
                unlink($fils);
            }else{
                echo 'photos is not deleted from ScanedImagesTemp';
            }
        }
if(isset($_FILES["com_asprise_scannerjs_images"]["name"]) && count($_FILES["com_asprise_scannerjs_images"]["name"])>0) {
    for ($i = 0; $i < count($_FILES["com_asprise_scannerjs_images"]["name"]); $i++) {
        $target_file = $target_dir . date("Ymdhis").basename($_FILES["com_asprise_scannerjs_images"]["name"][$i]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

        // Check if file already exists
        if (file_exists($target_file)) {
        // echo "<br>Sorry, file already exists.";
        $uploadOk = 0;
        }

        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
        // echo "<br>Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
        }

        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
        // echo "<br>Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
        } else {
        // if (move_uploaded_file($_FILES["file"]["tmp_name"][$i], $target_file)) {
        if (compress_image($_FILES["com_asprise_scannerjs_images"]["tmp_name"][$i], $target_file, 70)) {
           

            $filePath = $target_file;
            list($width, $height) = getimagesize($filePath);
    
            $offset_x = 0;
            $offset_y = 0;
            
            $new_height = $height - 150;
            $new_width = $width;
    
            $img = imagecreatefromjpeg($filePath);
            $new_image = imagecreatetruecolor($new_width, $new_height);
    
            $area = ["x"=>0, "y"=>0, "width"=>$new_width, "height"=> $new_height];
            $crop = imagecrop($img, $area);
           $targetFile1 = $target_dir.'c'.date("Ymdhis").basename($_FILES["com_asprise_scannerjs_images"]["name"][$i]);
          // echo $targetFile;exit;
            imagejpeg($crop,$targetFile1 , 50);
            unlink($target_file);
            // echo "The file ". htmlspecialchars( basename( $_FILES["com_asprise_scannerjs_images"]["name"][$i])). " has been uploaded.";
            } else {
            // echo "<br>Sorry, there was an error uploading your file.";
        }
        }
    }
}
?>