Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19

Thread: Random Images

  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Random Images

    Required Knowledge: HTML, setting up PHP pages.
    Keywords: Random Image, Rotation, Banner, Picture

    This tutorial will show you how to display a random image, as seen in my signature (at the time of posting this)
    JR

  2.  

  3. #2
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Part 1: The Simple Way

    This method assumes you have the images named 1.gif, 2.gif, 3.gif etc.

    All you have to do is create a PHP page with this script in:
    PHP Code:
    <?php
    //$dir is the directory where the images are stored,
    //if the images are in the same directory as the script leave it blank.
    //Enter it in the following format: directoryname/
    $dir "";
    //$ext is the file extension of all the files, such as .gif or .jpg.
    $ext ".gif";
    //$i is the image filename. They must be named in order (e.g. 1.gif, 2.gif).
    //Replace '10' with the total number of images.
    $totalimages 10;
    //Do not edit below here
    $i rand(1$totalimages);
    $img $dir.$i.$ext;
    header("Location: $img"); 
    ?>

    You can remove the orange comments.

    If the PHP file that the script is kept in is called ‘randomimage.php’ then you can use this in your image tags on your website:
    Code:
    <img src=”randomimage.php” />
    JR

  4. #3
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Part 2: The Other Way

    If you cannot name the files 1.gif, 2.gif for any reason, then this script may be for you…
    PHP Code:
    <?php
    //$dirname is the directory where the images are stored.
    //The images MUST be stored in a folder.
    //Enter it in the following format: directoryname/
    $dirname "";
    //Do not edit below here. Unless you know what you're doing of course
    $dir opendir($dirname);
    $i = -1;
    while (
    false !== ($file readdir($dir)))
    {
        if (
    is_readable("$dirname/$file"))
        {
            
    $img_array[$i] = $file;
            
    $i++;
        }
    }
    $r rand(1$i 1);
    $img $dirname.$img_array[$r];
    header("location: $img");
    ?>


    A draw-back of this method is that the images must be stored in a folder, as far as I know. Plus you cannot store any other file types than images in the folder, or it won’t work.

    Again, if the file that this script is kept in is called ‘randomimage’ then you can use it in your img tags on your website:
    Code:
    <img src=”randomimage.php” />
    If you need to customise this script for your own needs, then just post a reply and I’ll give you some help.
    JR

  5. #4
    Senior Member ULTiMATE's Avatar
    Join Date
    Nov 2002
    Location
    England
    Posts
    124
    Member #
    308
    Well theres always a more complicated way of doing it. This will get all the images from a directory AND and sub-directory inside that one

    PHP Code:
    <?php 
    # folder from the document_root you want to include and all sub dirs 
    $folder "images"
        function 
    createimagearray($newdir){ 
         global 
    $folder,$newarray,$DOCUMENT_ROOT
         
    $dir_subdirs = array(); 
         
    chdir($newdir); 

         
    $handle opendir($newdir); 

            while(
    $entry readdir($handle)){ 
             
    $dirtitle str_replace($DOCUMENT_ROOT,"",$newdir); 
             
    $last4 substr(strtolower($entry),-4); 
                if(
    is_dir($entry) && $entry != ".." && $entry != "."){ 
                 
    $dir_subdirs[] = $entry
                } elseif(
    $entry != ".." && $entry != "." &&
    (
    $last4 == ".gif" || 
    $last4 == ".jpg" || 
    $last4 == ".png" || 
    $last4 == ".bmp")){ 
                 
    $newarray .= "$dirtitle$entry||"
                } 
            } 
         
    sort($dir_subdirs); 
               for(
    $i=0$i<count($dir_subdirs); $i++){ 
             
    $array createimagearray("$newdir$dir_subdirs[$i]/"); 
            } 
         
    closedir($handle); 
         return 
    $newarray
        } 
    $array    createimagearray($DOCUMENT_ROOT."/".$folder."/"); 
    $array substr($array0,-2); 
    $array explode("||",$array); 
        function 
    make_seed() { 
         list(
    $usec$sec) = explode(' 'microtime()); 
         return (float) 
    $sec + ((float) $usec 100000); 
        } 
    srand(make_seed()); 
    $view $array[rand(0,(count($array))-1)]; 
    echo 
    "<img src=\"$view\">"
    ?>

  6. #5
    Senior Member [Li] Brad's Avatar
    Join Date
    Oct 2003
    Location
    Saskatoon, Canada
    Posts
    578
    Member #
    3689
    Nice work JR, much simpler than the traditional JavaScript method, though universal on all web servers, but mostly every web host these days offers PHP as an incentive.

    [Li] Brad
    "Art strives for form, and hopes for beauty." - George Bellows

  7. #6
    Senior Member justlivyalife's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham, UK
    Posts
    2,871
    Member #
    2374
    Very nice, thanks JR . Now to check it out :-P.
    justlivyalife - The future depends on what we do in the present. (Mahatma Gandhi)
    WDF Resources: WDF Rules
    Non-WDF: JavascriptSource | Dynamic Drive | phpBB | HTML-Kit | Winamp | Download Firefox | Morguefile

  8. #7
    Senior Member justlivyalife's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham, UK
    Posts
    2,871
    Member #
    2374
    Got the script to work using both the 'Simple Way', and the 'Other Way' Thanks very much!
    justlivyalife - The future depends on what we do in the present. (Mahatma Gandhi)
    WDF Resources: WDF Rules
    Non-WDF: JavascriptSource | Dynamic Drive | phpBB | HTML-Kit | Winamp | Download Firefox | Morguefile

  9. #8
    Banned
    Join Date
    Oct 2003
    Posts
    236
    Member #
    3555
    this is something I recently found myself doing using ASP, in which it's equally simple.Include this code:

    <%
    Dim strPic

    'array variable for pics
    Dim x(1000)

    'constant for image directory
    Const mypath="/images"

    'new filesystem object
    Set filesystem = CreateObject("Scripting.FileSystemObject")
    Set folder = filesystem.GetFolder(server.mappath(mypath))

    Set filecollection = folder.Files

    ' Step through the files list
    ' keeping track of the number of files
    idx=0
    For Each file in filecollection
    idx=idx+1
    x(idx)=file.name
    Next

    'Choose a random picture
    randomize timer
    whichNo=int(rnd()*idx)+1

    'Clean up objects
    set filesystem=nothing
    set folder=nothing
    set filecollection=nothing

    'Displaying the image
    strPic= "<img height=82 width=181 src=" & imagepath & "/" & x(whichNO)& " alt=" & x(whichNo) & " border=0>"

    %>

    Then write out the pic variable in the relevant page position:

    <% =strPic %>

  10. #9
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    Since no one can see the above code by the banned poster, I'm re-posting it in its entirety. The original poster stated that they did not personally code it, but simply found it somewhere. I have not personally researched this code to see if it is under the GPL or copyrighted or anything, but if asked the staff of WDF will respectfully remove it from this site immediately:

    <%
    Dim strPic

    'array variable for pics
    Dim x(1000)

    'constant for image directory
    Const mypath="/images"

    'new filesystem object
    Set filesystem = CreateObject("Scripting.FileSystemObject")
    Set folder = filesystem.GetFolder(server.mappath(mypath))

    Set filecollection = folder.Files

    ' Step through the files list
    ' keeping track of the number of files
    idx=0
    For Each file in filecollection
    idx=idx+1
    x(idx)=file.name
    Next

    'Choose a random picture
    randomize timer
    whichNo=int(rnd()*idx)+1

    'Clean up objects
    set filesystem=nothing
    set folder=nothing
    set filecollection=nothing

    'Displaying the image
    strPic= "<img height=82 width=181 src=" & imagepath & "/" & x(whichNO)& " alt=" & x(whichNo) & " border=0>"

    %>

    Then write out the pic variable in the relevant page position:

    <% =strPic %>
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  11. #10
    Junior Member
    Join Date
    Jul 2004
    Posts
    4
    Member #
    6797
    ok i named all my images 1.jpg, 2.jpg, etc. and edited the script accordingly.

    i uploaded my images on to my server in their own directory. do i need to upload the php file into the same directory?

    im using dreamweaver. do i actually put one of the images in the spot i want? what exactly do i need to do with the image tag part?

    thanks a lot! trying to get this to work is driving me nuts!


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:13 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com