Results 1 to 6 of 6

Thread: How to show pictures randomly from directory

  1. #1
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up How to show pictures randomly from directory

    So, how can we show pictures randomly from directory?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

  3. #3
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    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:

    HTML Code:
    <div id="image_wrapper">
        <img id="random_image" src="/assets/images/gallery/default.gif" alt="Default Image" title="Default Image"  />
    </div>
    Javascript:

    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:
    HTML Code:
    <input type="button" name="btnChangeImage" id="btnChangeImage" value="Change Image" onclick="loadRandomImage()" />
    As the page loads:
    HTML Code:
    <body onload="loadRandomImage()"> ...
    When the image itself is clicked:
    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

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by ApacheTech View Post

    Javascript:

    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);
    }
    That will get you a random image, and a random alt and a random title. I think you meant:

    Code:
        var r = getRandomInt(0, 4);
        el.setAttribute('src', RandomImages[r].src);
        el.setAttribute('alt', RandomImages[r].alt);
        el.setAttribute('title', RandomImages[r].alt);
    which will select a random image and its alt property for the alt and title attributes.

    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

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

  6. #6
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    That will get you a random image, and a random alt and a random title. I think you meant:

    ...code...

    which will select a random image and its alt property for the alt and title attributes.

    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
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •