<!DOCTYPE html>
<html>
<head>
    <title>Lekker Site</title>
    <style>
        .image-container {
            text-align: center;
        }
    </style>
</head>
<body>
    <?php
    // Debug flag (set to false to disable debugging output)
    $debug = false;

    // Enable error reporting if debug is on
    if ($debug) {
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
    }

    // Directory containing images
    $imageDir = "images/";
    if ($debug) {
        echo "<p>Looking for images in: " . realpath($imageDir) . "</p>";
    }

    // Check if directory exists
    if (!is_dir($imageDir)) {
        echo "<p>Error: Directory '$imageDir' does not exist</p>";
        exit;
    }

    // Check if directory is readable
    if (!is_readable($imageDir)) {
        echo "<p>Error: Directory '$imageDir' is not readable - check permissions</p>";
        exit;
    }

    // Get all image files from directory
    $patterns = [
        $imageDir . "*.jpg",
        $imageDir . "*.jpeg",
        $imageDir . "*.png",
        $imageDir . "*.gif"
    ];
    $images = [];
    foreach ($patterns as $pattern) {
        $files = glob($pattern);
        if ($files !== false) {
            $images = array_merge($images, $files);
        }
    }
    
    if ($debug) {
        echo "<p>Found " . count($images) . " images</p>";
        echo "<pre>Files found:\n";
        var_dump($images);
        echo "</pre>";
    }

    // Check if there are any images
    if (!empty($images)) {
        // Select random image
        $randomImage = $images[array_rand($images)];
        if ($debug) {
            echo "<p>Selected image: $randomImage</p>";
        }
        
        // Check if file exists and is readable
        if (file_exists($randomImage) && is_readable($randomImage)) {
            echo '<div class="image-container">';
            echo '<img src="' . $randomImage . '" alt="Random Image from Lekker Site">';
            echo '</div>';
        } else {
            echo "<p>Error: Cannot read selected image file - check file permissions</p>";
        }
    } else {
        echo "<p>No images found in the directory. Make sure files have .jpg, .jpeg, .png, or .gif extensions</p>";
    }
    ?>
</body>
</html>