利用FSO取得BMP,JPG,PNG,GIF文件信息
程序员文章站
2022-05-18 11:37:44
<%
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::::::::...
<%
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: bmp, gif, jpg and png :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: this function gets a specified number of bytes from any :::
'::: file, starting at the offset (base 1) :::
'::: :::
'::: passed: :::
'::: flnm => filespec of file to read :::
'::: offset => offset at which to start reading :::
'::: bytes => how many bytes to read :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function getbytes(flnm, offset, bytes)
dim objfso
dim objftemp
dim objtextstream
dim lngsize
on error resume next
set objfso = createobject("scripting.filesystemobject")
' first, we get the filesize
set objftemp = objfso.getfile(flnm)
lngsize = objftemp.size
set objftemp = nothing
fsoforreading = 1
set objtextstream = objfso.opentextfile(flnm, fsoforreading)
if offset > 0 then
strbuff = objtextstream.read(offset - 1)
end if
if bytes = -1 then ' get all!
getbytes = objtextstream.read(lngsize) 'readall
else
getbytes = objtextstream.read(bytes)
end if
objtextstream.close
set objtextstream = nothing
set objfso = nothing
end function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: functions to convert two bytes to a numeric value (long) :::
'::: (both little-endian and big-endian) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function lngconvert(strtemp)
lngconvert = clng(asc(left(strtemp, 1)) + ((asc(right(strtemp, 1)) * 256)))
end function
function lngconvert2(strtemp)
lngconvert2 = clng(asc(right(strtemp, 1)) + ((asc(left(strtemp, 1)) * 256)))
end function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: this function does most of the real work. it will attempt :::
'::: to read any file, regardless of the extension, and will :::
'::: identify if it is a graphical image. :::
'::: :::
'::: passed: :::
'::: flnm => filespec of file to read :::
'::: width => width of image :::
'::: height => height of image :::
'::: depth => color depth (in number of colors) :::
'::: strimagetype=> type of image (e.g. gif, bmp, etc.) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function gfxspex(flnm, width, height, depth, strimagetype)
dim strpng
dim strgif
dim strbmp
dim strtype
strtype = ""
strimagetype = "(unknown)"
gfxspex = false
strpng = chr(137) & chr(80) & chr(78)
strgif = "gif"
strbmp = chr(66) & chr(77)
strtype = getbytes(flnm, 0, 3)
if strtype = strgif then ' is gif
strimagetype = "gif"
width = lngconvert(getbytes(flnm, 7, 2))
height = lngconvert(getbytes(flnm, 9, 2))
depth = 2 ^ ((asc(getbytes(flnm, 11, 1)) and 7) + 1)
gfxspex = true
elseif left(strtype, 2) = strbmp then ' is bmp
strimagetype = "bmp"
width = lngconvert(getbytes(flnm, 19, 2))
height = lngconvert(getbytes(flnm, 23, 2))
depth = 2 ^ (asc(getbytes(flnm, 29, 1)))
gfxspex = true
elseif strtype = strpng then ' is png
strimagetype = "png"
width = lngconvert2(getbytes(flnm, 19, 2))
height = lngconvert2(getbytes(flnm, 23, 2))
depth = getbytes(flnm, 25, 2)
select case asc(right(depth,1))
case 0
depth = 2 ^ (asc(left(depth, 1)))
gfxspex = true
case 2
depth = 2 ^ (asc(left(depth, 1)) * 3)
gfxspex = true
case 3
depth = 2 ^ (asc(left(depth, 1))) '8
gfxspex = true
case 4
depth = 2 ^ (asc(left(depth, 1)) * 2)
gfxspex = true
case 6
depth = 2 ^ (asc(left(depth, 1)) * 4)
gfxspex = true
case else
depth = -1
end select
else
strbuff = getbytes(flnm, 0, -1) ' get all bytes from file
lngsize = len(strbuff)
flgfound = 0
strtarget = chr(255) & chr(216) & chr(255)
flgfound = instr(strbuff, strtarget)
if flgfound = 0 then
exit function
end if
strimagetype = "jpg"
lngpos = flgfound + 2
exitloop = false
do while exitloop = false and lngpos < lngsize
do while asc(mid(strbuff, lngpos, 1)) = 255 and lngpos < lngsize
lngpos = lngpos + 1
loop
if asc(mid(strbuff, lngpos, 1)) < 192 or asc(mid(strbuff, lngpos, 1)) > 195 then
lngmarkersize = lngconvert2(mid(strbuff, lngpos + 1, 2))
lngpos = lngpos + lngmarkersize + 1
else
exitloop = true
end if
loop
'
if exitloop = false then
width = -1
height = -1
depth = -1
else
height = lngconvert2(mid(strbuff, lngpos + 4, 2))
width = lngconvert2(mid(strbuff, lngpos + 6, 2))
depth = 2 ^ (asc(mid(strbuff, lngpos + 8, 1)) * 8)
gfxspex = true
end if
end if
end function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: test harness :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' to test, we'll just try to show all files with a .gif extension in the root of c:
set objfso = createobject("scripting.filesystemobject")
set objf = objfso.getfolder("c:\")
set objfc = objf.files
response.write "<table border=""0"" cellpadding=""5"">"
for each f1 in objfc
if instr(ucase(f1.name), ".gif") then
response.write "<tr><td>" & f1.name & "</td><td>" & f1.datecreated & "</td><td>" & f1.size & "</td><td>"
if gfxspex(f1.path, w, h, c, strtype) = true then
response.write w & " x " & h & " " & c & " colors"
else
response.write " "
end if
response.write "</td></tr>"
end if
next
response.write "</table>"
set objfc = nothing
set objf = nothing
set objfso = nothing
%>
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: bmp, gif, jpg and png :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: this function gets a specified number of bytes from any :::
'::: file, starting at the offset (base 1) :::
'::: :::
'::: passed: :::
'::: flnm => filespec of file to read :::
'::: offset => offset at which to start reading :::
'::: bytes => how many bytes to read :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function getbytes(flnm, offset, bytes)
dim objfso
dim objftemp
dim objtextstream
dim lngsize
on error resume next
set objfso = createobject("scripting.filesystemobject")
' first, we get the filesize
set objftemp = objfso.getfile(flnm)
lngsize = objftemp.size
set objftemp = nothing
fsoforreading = 1
set objtextstream = objfso.opentextfile(flnm, fsoforreading)
if offset > 0 then
strbuff = objtextstream.read(offset - 1)
end if
if bytes = -1 then ' get all!
getbytes = objtextstream.read(lngsize) 'readall
else
getbytes = objtextstream.read(bytes)
end if
objtextstream.close
set objtextstream = nothing
set objfso = nothing
end function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: functions to convert two bytes to a numeric value (long) :::
'::: (both little-endian and big-endian) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function lngconvert(strtemp)
lngconvert = clng(asc(left(strtemp, 1)) + ((asc(right(strtemp, 1)) * 256)))
end function
function lngconvert2(strtemp)
lngconvert2 = clng(asc(right(strtemp, 1)) + ((asc(left(strtemp, 1)) * 256)))
end function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: :::
'::: this function does most of the real work. it will attempt :::
'::: to read any file, regardless of the extension, and will :::
'::: identify if it is a graphical image. :::
'::: :::
'::: passed: :::
'::: flnm => filespec of file to read :::
'::: width => width of image :::
'::: height => height of image :::
'::: depth => color depth (in number of colors) :::
'::: strimagetype=> type of image (e.g. gif, bmp, etc.) :::
'::: :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function gfxspex(flnm, width, height, depth, strimagetype)
dim strpng
dim strgif
dim strbmp
dim strtype
strtype = ""
strimagetype = "(unknown)"
gfxspex = false
strpng = chr(137) & chr(80) & chr(78)
strgif = "gif"
strbmp = chr(66) & chr(77)
strtype = getbytes(flnm, 0, 3)
if strtype = strgif then ' is gif
strimagetype = "gif"
width = lngconvert(getbytes(flnm, 7, 2))
height = lngconvert(getbytes(flnm, 9, 2))
depth = 2 ^ ((asc(getbytes(flnm, 11, 1)) and 7) + 1)
gfxspex = true
elseif left(strtype, 2) = strbmp then ' is bmp
strimagetype = "bmp"
width = lngconvert(getbytes(flnm, 19, 2))
height = lngconvert(getbytes(flnm, 23, 2))
depth = 2 ^ (asc(getbytes(flnm, 29, 1)))
gfxspex = true
elseif strtype = strpng then ' is png
strimagetype = "png"
width = lngconvert2(getbytes(flnm, 19, 2))
height = lngconvert2(getbytes(flnm, 23, 2))
depth = getbytes(flnm, 25, 2)
select case asc(right(depth,1))
case 0
depth = 2 ^ (asc(left(depth, 1)))
gfxspex = true
case 2
depth = 2 ^ (asc(left(depth, 1)) * 3)
gfxspex = true
case 3
depth = 2 ^ (asc(left(depth, 1))) '8
gfxspex = true
case 4
depth = 2 ^ (asc(left(depth, 1)) * 2)
gfxspex = true
case 6
depth = 2 ^ (asc(left(depth, 1)) * 4)
gfxspex = true
case else
depth = -1
end select
else
strbuff = getbytes(flnm, 0, -1) ' get all bytes from file
lngsize = len(strbuff)
flgfound = 0
strtarget = chr(255) & chr(216) & chr(255)
flgfound = instr(strbuff, strtarget)
if flgfound = 0 then
exit function
end if
strimagetype = "jpg"
lngpos = flgfound + 2
exitloop = false
do while exitloop = false and lngpos < lngsize
do while asc(mid(strbuff, lngpos, 1)) = 255 and lngpos < lngsize
lngpos = lngpos + 1
loop
if asc(mid(strbuff, lngpos, 1)) < 192 or asc(mid(strbuff, lngpos, 1)) > 195 then
lngmarkersize = lngconvert2(mid(strbuff, lngpos + 1, 2))
lngpos = lngpos + lngmarkersize + 1
else
exitloop = true
end if
loop
'
if exitloop = false then
width = -1
height = -1
depth = -1
else
height = lngconvert2(mid(strbuff, lngpos + 4, 2))
width = lngconvert2(mid(strbuff, lngpos + 6, 2))
depth = 2 ^ (asc(mid(strbuff, lngpos + 8, 1)) * 8)
gfxspex = true
end if
end if
end function
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: test harness :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' to test, we'll just try to show all files with a .gif extension in the root of c:
set objfso = createobject("scripting.filesystemobject")
set objf = objfso.getfolder("c:\")
set objfc = objf.files
response.write "<table border=""0"" cellpadding=""5"">"
for each f1 in objfc
if instr(ucase(f1.name), ".gif") then
response.write "<tr><td>" & f1.name & "</td><td>" & f1.datecreated & "</td><td>" & f1.size & "</td><td>"
if gfxspex(f1.path, w, h, c, strtype) = true then
response.write w & " x " & h & " " & c & " colors"
else
response.write " "
end if
response.write "</td></tr>"
end if
next
response.write "</table>"
set objfc = nothing
set objf = nothing
set objfso = nothing
%>
上一篇: 使用FSO把文本信息导入数据库
下一篇: 文件遍历排序函数