View Single Post
Old 04-23-2024, 01:00 PM  
blackmonsters
Making PHP work
 
blackmonsters's Avatar
 
Industry Role:
Join Date: Nov 2002
Location: 🌎🌅🌈🌇
Posts: 20,136
User
What is the easiest one or two line script/html that will serve an infinite slide show out of a folder of images?

ChatGPT
You can achieve an infinite slideshow with a simple HTML script using JavaScript. Here's a basic example:

html
Copy code
<!DOCTYPE html>
<html>
<head>
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
Ignore this line; was inserted by gfy because I didn't use the code tags
<title>Infinite Slideshow</title>
<style>
#slideshow {
position: relative;
max-width: 100%;
max-height: 100%;
margin: auto;
}
img {
width: 100%;
height: auto;
}
</style>
</head>
<body>

<div id="slideshow"></div>

<script>
var images = ["image1.jpg", "image2.jpg", "image3.jpg"]; // List your image filenames here
var index = 0;

function nextImage() {
index = (index + 1) % images.length;
document.getElementById('slideshow').innerHTML = '<img src="' + images[index] + '">';
setTimeout(nextImage, 3000); // Change slide every 3 seconds (adjust as needed)
}

nextImage(); // Start the slideshow
</script>



</body>
</html>


Followed up with :

How can I do this from a folder of images though?


ChatGPT
To create a slideshow from a folder of images dynamically, you'll need to use server-side scripting languages like PHP to fetch the images from the folder and then serve them to the client-side script. Here's how you can modify the previous example to achieve this using PHP:

Create an HTML file (e.g., slideshow.html) with the following content:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Infinite Slideshow</title>
<style>
#slideshow {
position: relative;
max-width: 100%;
max-height: 100%;
margin: auto;
}
img {
width: 100%;
height: auto;
}
</style>
</head>
<body>

<div id="slideshow"></div>

<script>
var images = <?php echo json_encode(glob('images/*.jpg')); ?>;
var index = 0;

function nextImage() {
index = (index + 1) % images.length;
document.getElementById('slideshow').innerHTML = '<img src="' + images[index] + '">';
setTimeout(nextImage, 3000); // Change slide every 3 seconds (adjust as needed)
}

nextImage(); // Start the slideshow
</script>

</body>
</html>

That worked; but the file has to have PHP extension


__________________
You mad as fuck because you suck.
You have no life.

blackmonsters is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote