So, how can we show pictures randomly from directory?
So, how can we show pictures randomly from directory?
Moderator's note: this has been moved to a new discussion. Please do not reply to an old discussion with a new question.
-----
What resources do you have available? What language(s) do you want to use?
My suggestion would be to use PHP and then get a list of all images from a directory. Then pick one randomly from the list. The function glob() should get the list, then you can shuffle() the array and pick one of them. Note that you should also verify that each is an image.
A better way to do this would be to use a database containing filenames, then select one entry at random and use the filename to locate the file and display that. It takes a little more setup, but it's the best option.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
This can be harder if you don't know the images that are in the folder. If you do, you can do it like this:
Javascript:HTML Code:<div id="image_wrapper"> <img id="random_image" src="/assets/images/gallery/default.gif" alt="Default Image" title="Default Image" /> </div>
Code:var Image = function(src, alt) { this.src= src; this.alt= alt; } function getRandomInt (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function loadRandomImage() { var dir = "/assets/images/gallery/"; var RandomImages = [new Image(dir + "001.png", "My amazing picture"), new Image(dir + "002.png", "Another picture"), new Image(dir + "003.png", "Some dude on a bike"), new Image(dir + "004.png", "Balloons"), new Image(dir + "005.png", "Random pic 5")]; var el = document.getElementById('random_image'); el.setAttribute('src', RandomImages[getRandomInt(0, 4)].src); el.setAttribute('alt', RandomImages[getRandomInt(0, 4)].alt); el.setAttribute('title', RandomImages[getRandomInt(0, 4)].alt); }
Then you can use this in a few ways.
As a button:
As the page loads:HTML Code:<input type="button" name="btnChangeImage" id="btnChangeImage" value="Change Image" onclick="loadRandomImage()" />
When the image itself is clicked:HTML Code:<body onload="loadRandomImage()"> ...
HTML Code:<img id="random_image" src="/assets/images/gallery/default.gif" alt="Default Image" title="Default Image" onclick="loadRandomImage()" />
Last edited by jscheuer1; 05-29-2012 at 04:49 PM. Reason: Format
That will get you a random image, and a random alt and a random title. I think you meant:
which will select a random image and its alt property for the alt and title attributes.Code:var r = getRandomInt(0, 4);el.setAttribute('src', RandomImages[r].src); el.setAttribute('alt', RandomImages[r].alt); el.setAttribute('title', RandomImages[r].alt);
Even then, I'm not sure you can co-opt javascript's native Image() function like that, probably not in at least some browsers, probably not in any, so:
Code:function im(src, alt) { var im = new Image(); im.src= src; im.alt= alt; return im; } function getRandomInt (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function loadRandomImage() { var dir = "/assets/images/gallery/"; var RandomImages = [im(dir + "001.png", "My amazing picture"), im(dir + "002.png", "Another picture"), im(dir + "003.png", "Some dude on a bike"), im(dir + "004.png", "Balloons"), im(dir + "005.png", "Random pic 5")]; var el = document.getElementById('random_image'); var r = getRandomInt(0, 4); el.setAttribute('src', RandomImages[r].src); el.setAttribute('alt', RandomImages[r].alt); el.setAttribute('title', RandomImages[r].alt); }
Last edited by jscheuer1; 05-29-2012 at 05:09 PM. Reason: code was attempting to glom onto native Image function
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Additionally, this is not very hard to do if you can use a serverside language to get a list of the images that are there. Your post sounds discouraging about that, ApacheTech, but it's very easy to do and will save time manually listing the images in Javascript (what if there are hundreds or thousands?). Also, using a serverside language eliminates the dependence on Javascript, which might not work for everyone, and will make the page faster and use the processor less. Assuming a serverside language is available, it really is the better (maybe slightly more difficult) solution.
We just need more info from the original poster, to find out what languages are available, etc. (Sometimes it's best not to answer a question until extra information is given, so you don't waste your time with an answer that might not answer the question at all-- so far, here, we just don't know.)
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Apologies. As soon as I read your comment I knew the code you'd write, that was a stupid oversight on my part. For the second, I wasn't aware of the native Image() class, it would have been caught straight away in debug if I'd had VS open. Two oversights in one post, I'm really slacking. Won't happen again, boss. *salutes*![]()
Note to self: Debug code in VS prior to posting rather than writing it raw in the quick reply box.![]()
Bookmarks