How to Display Random Images Using ASP

ID: Q247745


The information in this article applies to:
  • Microsoft Internet Information Services version 5.0


SUMMARY

This article describes the process for returning a random image from a folder in your Web site using Microsoft Active Server Pages (ASP) and the Scripting.FileSystemObject.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

Random Images from ASP

Use the following steps to create an include file for use with ASP containing the RandomImage() function. The RandomImage() function randomly returns the name of an image based on the contents in a folder, which is passed by name to the function, or based on the name of a default image if no images are found in the folder.
  1. Create a folder named "Images" in your Web site's root folder.
  2. Copy a few .gif or .jpg images into the Images folder.
  3. Save the following ASP page as "RandomTest.asp" in your Web site's root folder:
    
    <% @Language="VBScript" %>
    <% Option Explicit %>
    <html>
    <head>
    <!--#include virtual="/includes/RandomImage.inc"-->
    </head>
    <body>
    <div align="center"><center>
    <table border="1">
      <tr>
        <td bgcolor="#ffffff">
          <img src="<%=RandomImage("/images/","/images/default.gif")%>">
        </td>
      </tr>
    </table>
    </center></div>
    </body>
    </html> 
    Note: The call to the RandomImage() function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.

  4. Create a folder named "Includes" in your Web site's root directory.
  5. Save the following ASP code in the Includes folder as RandomImage.inc:
    
    <%
      Function RandomImage(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strImages, strPhysical, strFile
    
        ' this constant has the names of valid image file name
        ' extensions and can be modified for more image types
        Const strValid = ".gif.jpg.png"
    
        ' make sure we have a trailing slash in the path
        If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
        ' get the physical path of the folder
        strPhysical = Server.MapPath(strPath)
        ' get a File System Object
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        ' create a folder object
        Set objFolder = objFSO.GetFolder(strPhysical)
        ' get the files collection
        Set objFiles = objFolder.Files
    
        ' enumerate the files collection looking for images
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add vaild images to a string of image names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the image names into an array
        strImages = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strImages) > 1 Then
          ' get a random name
          RandomImage = strPath & strImages(Int(Rnd(1)*UBound(strImages)))
        Else
          ' otherwise return the default
          RandomImage = strDefault
        End If
    
      End Function
    %> 
  6. Browse the RandomTest.asp page; it should display a single image in a table.

Additional query words:

Keywords : kbWinOS2000
Version : winnt:5.0
Platform : winnt
Issue type : kbhowto


Last Reviewed: January 25, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.