ASP photo handler
I'm writing an ASP script to handle photo albums. The way I'm trying to set it up is that I have two files:
photo-library.asp - an array of all album names/directories, and arrays of filenames in each album
<%
Dim albumList(8) ' Directory name
albumList(0) = "photoshoot-2003/"
albumList(1) = "attic-2003/"
albumList(2) = "gathering-grounds-2003/"
albumList(3) = "avenue-rueben/"
albumList(4) = "studio-2003/"
albumList(5) = "random-pics/"
albumList(6) = "starbucks-acoustic/"
albumList(7) = "goodstock-2004/"
Dim photoshoot(10)
photoshoot(0) = "1.jpg"
photoshoot(1) = "2.jpg"
photoshoot(2) = "3.jpg"
photoshoot(3) = "4.jpg"
photoshoot(4) = "5.jpg"
photoshoot(5) = "6.jpg"
photoshoot(6) = "7.jpg"
photoshoot(7) = "8.jpg"
photoshoot(8) = "9.jpg"
photoshoot(9) = "10.jpg"
%>
photos.asp - includes photo-library.asp and displays an image based on parameters passed on the URL
<!-- #include virtual="\header.asp" -->
<!-- #include virtual="\photo-library.asp" -->
<%
Dim strAlbum ' will be pulled from URL
strAlbum = 0
Dim strAlbumName
strAlbumName = albumList(strAlbum)
Dim strBaseDir
strBaseDir ="/images/photo-albums/"
response.write "<img src='" & strBaseDir & albumList(strAlbum) & photoshoot(0) & "' />"
%>
<!-- #include virtual="\footer.asp" -->
The problem I've got is that I can't think of a way to reference the name of the array containing the filenames. In the code above photoshoot(0) is the first image in the photoshoot array, but I want to be able to call that dynamically instead of manually. I tried albumList(strAlbum)(0) but that didn't work. Any ideas? Or, is there a better way to structure this entire thing?
chrishirst posted this at 08:30 — 12th July 2005.
He has: 379 posts
Joined: Apr 2005
Using a database would be the best way, but you could use the FSO to build an associative array (Scripting Dictionary) of images from the folder. This has the benefit of if you add or remove images you don't need to rewrite your array. It is also possible to build the folder array in the same way.
dim FSO
dim Folders
dim Files
dim File
dim i
dim PhotoShoot
set PhotoShoot = server.CreateObject("Scripting.Dictionary")
Set FSO = CreateObject("Scripting.FileSystemObject")
set Folders=FSO.GetFolder(server.mappath(Folder))
Set Files = Folders.Files
i = 0
for each File In Files
if lCase(Right(File.Name,4)) = ".jpg" then
PhotoShoot.add i, File.Name
i = i + 1
end if
next
and to reference the image you use it just the same as an array.
And don't forget to destroy the objects at the end of the code.
BTW I didn't test this code (I may do later in the day) but it looks right.
Chris
Indifference will be the downfall of mankind, but who cares?
Venue Capacity Monitoring
Code Samples
IanD posted this at 02:50 — 13th July 2005.
They have: 222 posts
Joined: Sep 1999
That works, thanks
<!-- #include virtual="\header.asp" -->
<%
Dim strAlbum
Dim strPhoto
strAlbum = Request.QueryString("album")
strPhoto = Request.QueryString("photo")
Dim objFSO
Dim objDictionary
Dim objAlbums
Dim objAlbum
Dim objFile
Dim varCounter
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If strAlbum <> "" And strPhoto = "" Then
' If only album given in then display thumbnails for all photos in that album
Set objAlbum = objFSO.GetFolder(Server.MapPath("/images/photo-albums/" & strAlbum))
Set objDictionary = CreateObject("Scripting.Dictionary")
' Build array of large photos, will be identical to array built on large photo page.
varCounter = 0
For Each objFile in objAlbum.Files
If InStr(objFile.Name, "-thumb") = 0 Then
objDictionary.Add varCounter, objFile.Name
varCounter = varCounter + 1
End If
Next
Response.Write "<div style='text-align: center'>"
For varCounter = 0 To (objDictionary.Count - 1)
Response.Write "<a href='/photos.asp?album=" & strAlbum & "&photo=" & varCounter & "' class='linked-image'>"
Response.Write "<img src='/images/photo-albums/" & strAlbum & "/" & Replace(objDictionary(varCounter), ".jpg", "-thumb.jpg") & "' style='border: 1px solid white;'/></a> "
Next
Response.Write "</div>"
Elseif strAlbum <> "" And strPhoto <> "" Then
' If album and photo# given then display big photo
Set objAlbum = objFSO.GetFolder(Server.MapPath("/images/photo-albums/" & strAlbum))
Set objDictionary = CreateObject("Scripting.Dictionary")
' Build array of large photos, will be identical to array built on thumbnail page.
varCounter = 0
For Each objFile in objAlbum.Files
If InStr(objFile.Name, "-thumb") = 0 Then
objDictionary.Add varCounter, objFile.Name
varCounter = varCounter + 1
End If
Next
Response.Write "<div style='text-align: center'>"
If strPhoto <> 0 Then
Response.Write "<a href='/photos.asp?album=" & strAlbum & "&photo=" & strPhoto - 1 & "' style='text-decoration: none'>"
End If
Response.Write "«</a> |"
Response.Write "<a href='/photos.asp?album=" & strAlbum & "' style='text-decoration: none'>index</a> | "
If cint(strPhoto) <> (objDictionary.Count -1 ) Then
Response.Write "<a href='/photos.asp?album=" & strAlbum & "&photo=" & strPhoto + 1 & "' style='text-decoration: none'>"
End If
Response.Write "»</a>"
Response.Write "<p><img src='/images/photo-albums/" & strAlbum & "/" & objDictionary(cint(strPhoto)) & "' style='border: 1px solid white;' /></p>"
Response.Write "</div>"
Else
' If no params given, display list of albums
Set objAlbums = (objFSO.GetFolder(Server.MapPath("/images/photo-albums/" & strAlbum))).SubFolders
For Each objAlbum In objAlbums
Response.Write "<a href='/photos.asp?album=" & objAlbum.Name & "'>" & Replace(objAlbum.Name, "-", " ") & "</a><br />"
Next
End If
Set objFSO = nothing
Set objDictionary = nothing
Set objAlbums = nothing
Set objAlbum = nothing
Set objFile = nothing
%>
<!-- #include virtual="\footer.asp" -->
Fighting for a Lost Cause.net
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.