<?php
$message = '';

if (isset($_POST['upload'])) {

    if (!isset($_FILES['idx_file']) || $_FILES['idx_file']['error'] !== UPLOAD_ERR_OK) {
        $message = "Upload failed.";
    } else {

        $file = $_FILES['idx_file'];

        // Maximum size: 300 KB
        if ($file['size'] > 300 * 1024) {
            $message = "File is too large. Maximum size is 300 KB.";
        } else {

            // Verify that it is actually an image
            $imageInfo = @getimagesize($file['tmp_name']);

            if ($imageInfo === false) {
                $message = "Only image files are allowed.";
            } else {

                $width  = $imageInfo[0];
                $height = $imageInfo[1];

                // Exact dimensions
                if ($width != 900 || $height != 600) {
                    $message = "Image must be exactly 900 × 600 pixels.";
                } else {

                    $allowedTypes = [
                        IMAGETYPE_JPEG => 'jpg',
                        IMAGETYPE_PNG  => 'png',
                        IMAGETYPE_WEBP => 'webp'
                    ];

                    $type = $imageInfo[2];

                    if (!isset($allowedTypes[$type])) {
                        $message = "Only JPG, PNG, and WebP images are allowed.";
                    } else {

                        // Create upload directory
                        $uploadDir = __DIR__ . '/uploads/';

                        if (!is_dir($uploadDir)) {
                            mkdir($uploadDir, 0755, true);
                        }

                        // Generate a safe filename
                        $filename = bin2hex(random_bytes(16)) . '.' . $allowedTypes[$type];

                        $destination = $uploadDir . $filename;

                        if (move_uploaded_file($file['tmp_name'], $destination)) {

                            $url = 'uploads/' . $filename;

                            $message =
                                'Upload successful!<br>' .
                                '<a href="' . htmlspecialchars($url) . '" target="_blank">' .
                                htmlspecialchars($url) .
                                '</a>';

                        } else {
                            $message = "Could not save the uploaded file.";
                        }
                    }
                }
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Uploader</title>

<style>
html, body {
    height: 100%;
}

body {
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    background: #000;
    font-family: Arial, Helvetica, sans-serif;
}

.upload-box {
    background: #fff;
    padding: 30px;
    border-radius: 8px;
}

input {
    margin: 10px;
}

.message {
    margin-top: 15px;
}
</style>
</head>

<body>

<div class="upload-box">

<form method="post" enctype="multipart/form-data">

    <input
        type="file"
        name="idx_file"
        accept="image/jpeg,image/png,image/webp"
        required
    >

    <br>

    <input type="submit" name="upload" value="Upload">

</form>

<div class="message">
<?php echo $message; ?>
</div>

</div>

</body>
</html>