What are the best practices for implementing pagination in PHP when displaying a large number of images from directories?

When displaying a large number of images from directories in PHP, it is important to implement pagination to improve performance and user experience. This can be achieved by limiting the number of images displayed per page and providing navigation links to allow users to navigate through the pages of images.

<?php
$imagesPerPage = 10;
$currentpage = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($currentpage - 1) * $imagesPerPage;

$directory = 'path/to/images';
$images = glob($directory . '/*.jpg');

$totalImages = count($images);
$totalPages = ceil($totalImages / $imagesPerPage);

$paginatedImages = array_slice($images, $start, $imagesPerPage);

foreach ($paginatedImages as $image) {
    echo '<img src="' . $image . '" alt="Image">';
}

for ($i = 1; $i <= $totalPages; $i++) {
    echo '<a href="?page=' . $i . '">' . $i . '</a>';
}
?>