How can PHP be utilized to handle the functionality of navigating through images and displaying thumbnails in a gallery layout?
To handle the functionality of navigating through images and displaying thumbnails in a gallery layout using PHP, you can create an array of image file paths, iterate through the array to display thumbnails, and use JavaScript for image navigation. You can also use PHP to dynamically generate HTML for the gallery layout.
<?php
// Array of image file paths
$images = [
'image1.jpg',
'image2.jpg',
'image3.jpg',
// Add more image paths as needed
];
// Display thumbnails
foreach ($images as $image) {
echo '<img src="' . $image . '" alt="Thumbnail" style="width: 100px; height: 100px; margin: 5px;">';
}
// JavaScript for image navigation
echo '<script>
let currentImage = 0;
let images = ' . json_encode($images) . ';
function showImage(index) {
let img = document.getElementById("mainImage");
img.src = images[index];
currentImage = index;
}
function nextImage() {
currentImage = (currentImage + 1) % images.length;
showImage(currentImage);
}
function prevImage() {
currentImage = (currentImage - 1 + images.length) % images.length;
showImage(currentImage);
}
</script>';
// Display main image
echo '<img id="mainImage" src="' . $images[0] . '" alt="Main Image" style="width: 500px; height: 500px;">';
?>