<?php
$photos_per_page = 30;
$thumbnail_width = 600; // Set a higher width for the thumbnails (adjust as needed)
$thumbnail_folder = 'thumbnails';

// Create thumbnails directory if it doesn't exist
if (!is_dir($thumbnail_folder)) {
    mkdir($thumbnail_folder, 0777, true);
}

// Function to create a thumbnail
function create_thumbnail($source, $destination, $thumb_width) {
    $img_info = getimagesize($source);
    $width = $img_info[0];
    $height = $img_info[1];
    $img_type = $img_info[2];

    // Calculate thumbnail size
    $thumb_height = intval($thumb_width * $height / $width);

    // Create the thumbnail
    $thumb = imagecreatetruecolor($thumb_width, $thumb_height);

    switch ($img_type) {
        case IMAGETYPE_JPEG:
            $source_img = imagecreatefromjpeg($source);
            break;
        case IMAGETYPE_PNG:
            $source_img = imagecreatefrompng($source);
            break;
        case IMAGETYPE_GIF:
            $source_img = imagecreatefromgif($source);
            break;
        default:
            return false; // Unsupported image type
    }

    imagecopyresampled($thumb, $source_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);

    switch ($img_type) {
        case IMAGETYPE_JPEG:
            imagejpeg($thumb, $destination);
            break;
        case IMAGETYPE_PNG:
            imagepng($thumb, $destination);
            break;
        case IMAGETYPE_GIF:
            imagegif($thumb, $destination);
            break;
    }

    imagedestroy($source_img);
    imagedestroy($thumb);

    return true;
}

// Get all photos from the 'images' directory
$photos = glob('images/*.{jpg,jpeg,png,gif}', GLOB_BRACE);

// Sort photos by name or any other criteria
sort($photos);

// Pagination logic
$total_photos = count($photos);
$total_pages = ceil($total_photos / $photos_per_page);
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$current_page = max(1, min($total_pages, $current_page)); // Ensure current page is within range
$offset = ($current_page - 1) * $photos_per_page;

// Get the photos for the current page
$photos_to_display = array_slice($photos, $offset, $photos_per_page);

foreach ($photos_to_display as $photo) {
    $thumbnail_path = $thumbnail_folder . '/' . basename($photo);

    // Create thumbnail if it doesn't exist
    if (!file_exists($thumbnail_path)) {
        create_thumbnail($photo, $thumbnail_path, $thumbnail_width);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Galleri</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="gallery">
        <?php foreach ($photos_to_display as $photo): ?>
            <div class="photo">
                <a href="<?php echo $photo; ?>" target="_blank">
                    <img src="<?php echo $thumbnail_folder . '/' . basename($photo); ?>" alt="">
                </a>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="pagination">
        <?php if ($current_page > 1): ?>
            <a href="?page=<?php echo $current_page - 1; ?>"><<</a>
        <?php endif; ?>
        <?php if ($current_page < $total_pages): ?>
            <a href="?page=<?php echo $current_page + 1; ?>">>></a>
        <?php endif; ?>
    </div>
</body>
</html>
