home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / random_image.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  7.0 KB  |  230 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. ' Constant representing the number of images.
  15. ' This is used in Methods 1 and 2.
  16. Const NUMBER_OF_IMAGES = 4
  17.  
  18. ' Initialize the random number generator.  This is the
  19. ' command that tells the computer to give us psedo-random
  20. ' numbers when we use Rnd later.
  21. Randomize
  22.  
  23. ' Note:
  24. ' The generic formula for generating a random number in a
  25. ' range is:
  26. '
  27. ' Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
  28. '
  29. ' I've simplified this where possible, but all the random
  30. ' generation below is based upon this basic form.
  31.  
  32.  
  33. '==========================================================
  34. ' Method #1: This is probably the easiest and most
  35. ' straight-forward approach.  We name our image files so
  36. ' that they're all the same except for one part of the
  37. ' name.  This part is then assigned a set of sequential
  38. ' numbers.
  39. '
  40. ' IE: ad1.gif
  41. '     ad2.gif
  42. '     ad3.gif
  43. '     ad4.gif
  44. '
  45. ' This way by simply generating a random number in the
  46. ' appropriate range we can easily associate it with a
  47. ' particular image.
  48. '==========================================================
  49. Dim intImageNumber ' A var to store our randomn number
  50.  
  51. ' Pick a random number between 1 and NUMBER_OF_IMAGES (4)
  52. intImageNumber = Int((NUMBER_OF_IMAGES * Rnd) + 1)
  53.  
  54. ' Show the corresponding image:
  55. %>
  56. <h3>Method #1:</h3>
  57. <img
  58. src    = "./rndimgs/ad<%= intImageNumber %>.gif"
  59. width  = "150"
  60. height = "75"
  61. alt    = "Random Image"
  62. />
  63. <br />
  64.  
  65. <%
  66. '==========================================================
  67. ' Method #2: This method is similar to the first except
  68. ' that the options to choose from are hard coded into the
  69. ' page explicitly.  This allows a few things.  First, the
  70. ' image names can be whatever you'd like them to be.
  71. ' Second, it's easy to associate different parameters with
  72. ' the different images as illustrated with the width,
  73. ' height, and alt properties below.
  74. '==========================================================
  75. Dim intImageIdToShow  ' Random Id we generate
  76. Dim arrImages(3, 3)   ' Array to hold image parameters
  77.  
  78. ' Set up our 2D array with the values
  79. ' In my setup the first number is basically representing
  80. ' an image id and each image has an associated filename,
  81. ' width, height, and alt tag text.
  82. arrImages(0, 0) = "ad1.gif"
  83. arrImages(0, 1) = 150
  84. arrImages(0, 2) = 75
  85. arrImages(0, 3) = "Random Image #1"
  86.  
  87. arrImages(1, 0) = "ad2.gif"
  88. arrImages(1, 1) = 150
  89. arrImages(1, 2) = 75
  90. arrImages(1, 3) = "Random Image #2"
  91.  
  92. arrImages(2, 0) = "ad3.gif"
  93. arrImages(2, 1) = 150
  94. arrImages(2, 2) = 75
  95. arrImages(2, 3) = "Random Image #3"
  96.  
  97. arrImages(3, 0) = "ad4.gif"
  98. arrImages(3, 1) = 150
  99. arrImages(3, 2) = 75
  100. arrImages(3, 3) = "Random Image #4"
  101.  
  102. ' Generate a random number based on the bounds of the
  103. ' array we just set up.  Notice the ", 1" in the LBound
  104. ' and UBound calls.  This indicates I want to use the
  105. ' bounds from the first dimension.  It's the default so I
  106. ' could have left it off, but I thought I should include
  107. ' it for reference in case you decide to use the array
  108. ' differently then I did and need the bound from a
  109. ' different dimension.
  110. intImageIdToShow = Int((UBound(arrImages, 1) - _
  111.     LBound(arrImages, 1) + 1) * Rnd + LBound(arrImages, 1))
  112.  
  113. ' Show the image:
  114. %>
  115. <h3>Method #2:</h3>
  116. <img
  117. src    = "./rndimgs/<%= arrImages(intImageIdToShow, 0) %>"
  118. width  = "<%= arrImages(intImageIdToShow, 1) %>"
  119. height = "<%= arrImages(intImageIdToShow, 2) %>"
  120. alt    = "<%= arrImages(intImageIdToShow, 3) %>"
  121. />
  122. <br / >
  123.  
  124. <%
  125. '==========================================================
  126. ' Method #3: This method is the most work for the server,
  127. ' but it allows you to simply drop a new image into the
  128. ' image directory specified and it will automatically
  129. ' start showing it randomly with the others.
  130. '==========================================================
  131. ' Set our random images pickup directory.  All files in
  132. ' this direcotory will be randomly displayed so it's
  133. ' important that the directory contain only image files
  134. ' that you want to display.  Non-image files and files
  135. ' that shouldn't be displayed should not be placed into
  136. ' this directory.
  137. Const IMGS_DIR = "./rndimgs/"
  138.  
  139. ' Variables for our FileSystemObject objects
  140. Dim objFSO, objFolderObject, objFileCollection, objFile
  141.  
  142. ' A pair of integers for our random image selection
  143. Dim intFileNumberToUse, intFileLooper
  144.  
  145. ' A "handle" to the file we choose to use
  146. Dim objImageFileToUse
  147.  
  148. ' A variable to build our image tag
  149. Dim strImageSrcText
  150.  
  151. ' Lets see what's in the directory:
  152. Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  153. Set objFolderObject = objFSO.GetFolder(Server.MapPath(IMGS_DIR))
  154. Set objFSO = Nothing
  155.  
  156. Set objFileCollection = objFolderObject.Files
  157. Set objFolderObject = Nothing
  158.  
  159. ' Get a count of files and use it to generate a random
  160. ' number from 1 to the count.
  161. intFileNumberToUse = Int(objFileCollection.Count * Rnd) + 1
  162.  
  163. ' Set up loop control so we exit when we get to the random
  164. ' file number we just picked.
  165. intFileLooper = 1
  166. For Each objFile in objFileCollection
  167.     If intFileLooper = intFileNumberToUse Then
  168.         ' Get a "handle" on the appropriate file
  169.         Set objImageFileToUse = objFile
  170.         Exit For
  171.     End If
  172.     intFileLooper = intFileLooper + 1
  173. Next
  174.  
  175. Set objFileCollection = Nothing
  176.  
  177. ' Build our img src tag text
  178. strImageSrcText = IMGS_DIR & objImageFileToUse.Name
  179.  
  180. Set objImageFileToUse = Nothing
  181.  
  182. ' Show the image:
  183. %>
  184. <h3>Method #3:</h3>
  185. <img
  186. src    = "<%= strImageSrcText %>"
  187. width  = "150"
  188. height = "75"
  189. alt    = "Random Image"
  190. />
  191. <br / >
  192.  
  193. <%
  194. '==========================================================
  195. ' Interesting Footnote:
  196. ' I always used to do things in this order because it
  197. ' seemed to make sense to me:
  198. '
  199. ' Get FSO
  200. '  Get Folder Using FSO
  201. '   Get File Collection Using Folder
  202. '    Get File Using File Collection
  203. '     Do File Stuff
  204. '    Release File
  205. '   Release File Collection
  206. '  Release Folder
  207. ' Release FSO
  208. '
  209. ' Notice in the code above I do this however:
  210. '
  211. ' Get FSO
  212. ' Get Folder Using FSO
  213. ' Release FSO
  214. ' Get File Collection Using Folder
  215. ' Release Folder
  216. ' Get File Using File Collection
  217. ' Release File Collection
  218. ' Do File Stuff
  219. ' Release File
  220. '
  221. ' Notice the difference... in the first method I have 4
  222. ' objects allocated at one point while in the second
  223. ' method I never have more then 2 objects open at once!
  224. '
  225. ' It seems trivial, but the easiest way to increase ASP
  226. ' performance is to get in and out as fast as possible
  227. ' and this type of code helps accomplish that.
  228. '==========================================================
  229. %>
  230.