<?php
    $page = "image-metadata-viewer";
    include_once('includes/header.php');
    $normal_string = "";
    
    if(isset($_FILES['image_file'])){
        $target_dir = "./uploads/";
        $target_file = $target_dir . basename($_FILES["image_file"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        $error = [];
        // Check if image file is a actual image or fake image
        
        $check = getimagesize($_FILES["image_file"]["tmp_name"]);
        if($check !== false) {
            
            $uploadOk = 1;
        } else {
            $error[] = "File is not an image."; 
            $uploadOk = 0;
        }
        


        // Check file size
        if ($_FILES["image_file"]["size"] > 50000000) {
            $error[] = "Sorry, your file is too large.";
            $uploadOk = 0;
        }

        // Allow certain file formats
        $allowed_file = ["jpg", "png", "jpeg", "gif", "webp", "tiff"];
        if(!in_array($imageFileType, $allowed_file)) {
            $error[] = "Sorry, only JPG, JPEG, PNG, Webp & GIF files are allowed.";
            $uploadOk = 0;
        }

        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0 && sizeof($error)> 0) {
        
        } else {
        if (move_uploaded_file($_FILES["image_file"]["tmp_name"], $target_file)) {
            if($imageFileType != "png"){
                $data = exif_read_data($target_file, 0, true);
                $image_data = [];
                $keys = array_keys($data);
                
                foreach($data as $info){
                    foreach(array_keys($info) as $line){
                        if(gettype($info[$line]) == "array"){
                            $innder_info = $info[$line];
                            $image_data[$line] = implode(",",$innder_info);
                        }else{
                            $image_data[$line] = $info[$line];
                        }
                    }
                }
            }
            
            $image_data['Name'] = $_FILES['image_file']['name'];
            $image_data['Type'] = $_FILES['image_file']['type'];
            $image_data['Size'] = $_FILES['image_file']['size'];
            unlink($target_file);
        } else {
            $error[] = "Sorry, there was an error uploading your file.";
        }
        }
    }
?>

<style>
    .files input {
    outline: 4px dashed #0d6efd;
    outline-offset: -10px;
    -webkit-transition: outline-offset .15s ease-in-out, background-color .15s linear;
    transition: outline-offset .15s ease-in-out, background-color .15s linear;
    padding: 120px 0px 85px 35%;
    text-align: center !important;
    margin: 0;
    width: 100% !important;
}
.files input:focus{     outline: 2px dashed #92b0b3;  outline-offset: -10px;
    -webkit-transition: outline-offset .15s ease-in-out, background-color .15s linear;
    transition: outline-offset .15s ease-in-out, background-color .15s linear; border:2px solid #0d6efd;
 }
.files{ position:relative}
.files:after {  pointer-events: none;
    position: absolute;
    top: 60px;
    left: 0;
    width: 50px;
    right: 0;
    height: 56px;
    content: "";
    background-image: url(https://image.flaticon.com/icons/png/128/109/109612.png);
    display: block;
    margin: 0 auto;
    background-size: 100%;
    background-repeat: no-repeat;
}
.color input{ background-color:#f1f1f1;}
.files:before {
    position: absolute;
    bottom: 10px;
    left: 0;  pointer-events: none;
    width: 100%;
    right: 0;
    height: 57px;
    content: " or drag it here. ";
    display: block;
    margin: 0 auto;
    color: #2ea591;
    font-weight: 600;
    text-transform: capitalize;
    text-align: center;
}

</style>
<div class="row">
<div class="mt-2 mb-2 col-md-2">

</div>
<div class="col-md-7 mt-2 mb-2">
    <form action="" id="upload" name="upload" method="POST" enctype="multipart/form-data">
        <h1><?php echo $title; ?></h1>
        <div class="card">
            <div class="card-body">
                <?php if(isset($error) && sizeof($error)>0){ foreach($error as $alert){?>
                    <div class="mb-3"> 
                        <div class="alert alert-danger" role="alert">
                            <?php echo $alert; ?>
                        </div>
                    </div>
                <?php }}
                ?>
                <div class="mb-3">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group files">
                                <label>Upload Your Image File </label>
                                <input type="file" class="form-control" name="image_file">
                            </div>    
                        </div>
                    </div>
                </div>
                <div class="text-end">
                    <button type="submit" class="btn btn-outline-success">View Data</button>
                </div>
            </div>
        </div>
    </form>
    
    <?php if(isset($image_data) && sizeof($error) == 0){ ?>
    <div class="card">
        <div class="card-body">
            <div class="mb-3">
                <table class="table table-bordered">
                <?php foreach(array_keys($image_data) as $key){ ?>
                    <tr>
                        <td class="bg-light"><?php echo $key; ?></td>
                        <td><?php echo $image_data[$key]; ?></td>
                    </tr>
                <?php } ?>

                </table>
            </div>
        </div>
    </div>
    <?php } ?>
    
    <div class="card mt-3">
        <div class="card-header border">
            What is Image Metadata Viewer?
        </div>
            <div class="card-body">
                <p class="card-text">Our Image Metadata Viewer lets you to get the details of images or photos taken from a camera. Image Metada Viewer tool can show you data like GPS location, time, resolution, exposure, aperture, etc of a photo. You can use our Image Metadata viewer to get all the data of an image instantly.</p>
        </div>
        <div class="card-header border">
            Do we store the uploaded images on our server?
        </div>
            <div class="card-body">
                <p class="card-text">No we dont. Once you upload an image, we fetch all the data from the uploaded image and delete that image instantly from our servers.</p>
        </div>
    </div>
            
    
<div class="mt-2 mb-2 col-md-3">

</div>
</div>
</div>

<?php
    include_once('includes/footer.php');
?>