What are the best practices for creating a gallery with thumbnails in PHP?

When creating a gallery with thumbnails in PHP, it is important to optimize the loading time of the page by using thumbnails instead of full-size images. One way to achieve this is by creating a grid layout with thumbnails that link to the full-size images. Additionally, you can use PHP to dynamically generate the thumbnails based on the images in a specified directory.

<?php
// Path to the directory containing images
$directory = "path/to/images/";

// Get all files in the directory
$files = glob($directory . "*");

// Loop through each file and display as a thumbnail
foreach ($files as $file) {
    echo "<a href='$file' target='_blank'><img src='$file' alt='Thumbnail'></a>";
}
?>