如何用ASP测GIF图片文件的大小(不是文件字节大小,而是宽和高的长度)?

Hoper 2002-01-11 11:53:24
Function ReadGIF(filename)
Dim fso, ts, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(filename), 1)
s = Right(ts.Read(10), 4)
response.write mid(s,2,1) & mid(s,1,1)&"<br>" '宽的十六进制
response.write mid(s,4,1) & mid(s,3,1) '高的十六进制
ts.Close
End Function

理论上应该没错,但我实用的时候发现读出来的数据根本不对.
百思不得其解,望高手指点.
...全文
225 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hoper 2002-01-17
  • 打赏
  • 举报
回复
我再顶...
Hoper 2002-01-14
  • 打赏
  • 举报
回复
baggio785(狗狗),你写的这个只能测小图片的尺寸,稍微大点的图片一样是错的.
baggio785 2002-01-14
  • 打赏
  • 举报
回复
<!--#include virtual="/learn/test/lib_graphicdetect.asp"-->
<html><head>
<TITLE>dbtable.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<%
graphic="images/learnaspiconmain.gif"
HW = ReadImg(graphic)
Response.Write graphic & " Dimensions: " & HW(0) & "x" & HW(1) & "<br>"
response.write "<img src=""/" & graphic & """"
response.write height=""" & HW(0) & """
response.write width=""" & HW(0) & "">"
%>
</body></html>



The library that is included is:

<%
Dim HW

Function AscAt(s, n)
AscAt = Asc(Mid(s, n, 1))
End Function

Function HexAt(s, n)
HexAt = Hex(AscAt(s, n))
End Function


Function isJPG(fichero)
If inStr(uCase(fichero), ".JPG") <> 0 Then
isJPG = true
Else
isJPG = false
End If
End Function


Function isPNG(fichero)
If inStr(uCase(fichero), ".PNG") <> 0 Then
isPNG = true
Else
isPNG = false
End If
End Function


Function isGIF(fichero)
If inStr(uCase(fichero), ".GIF") <> 0 Then
isGIF = true
Else
isGIF = false
End If
End Function


Function isBMP(fichero)
If inStr(uCase(fichero), ".BMP") <> 0 Then
isBMP = true
Else
isBMP = false
End If
End Function


Function isWMF(fichero)
If inStr(uCase(fichero), ".WMF") <> 0 Then
isWMF = true
Else
isWMF = false
End If
End Function


Function isWebImg(f)
If isGIF(f) Or isJPG(f) Or isPNG(f) Or isBMP(f) Or isWMF(f) Then
isWebImg = true
Else
isWebImg = true
End If
End Function


Function ReadImg(fichero)
If isGIF(fichero) Then
ReadImg = ReadGIF(fichero)
Else
If isJPG(fichero) Then
ReadImg = ReadJPG(fichero)
Else
If isPNG(fichero) Then
ReadImg = ReadPNG(fichero)
Else
If isBMP(fichero) Then
ReadImg = ReadPNG(fichero)
Else
If isWMF(fichero) Then
ReadImg = ReadWMF(fichero)
Else
ReadImg = Array(0,0)
End If
End If
End If
End If
End If
End Function


Function ReadJPG(fichero)
Dim fso, ts, s, HW, nbytes
HW = Array("","")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
s = Right(ts.Read(167), 4)
HW(0) = HexToDec(HexAt(s,3) & HexAt(s,4))
HW(1) = HexToDec(HexAt(s,1) & HexAt(s,2))
ts.Close
ReadJPG = HW
End Function


Function ReadPNG(fichero)
Dim fso, ts, s, HW, nbytes
HW = Array("","")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
s = Right(ts.Read(24), 8)
HW(0) = HexToDec(HexAt(s,3) & HexAt(s,4))
HW(1) = HexToDec(HexAt(s,7) & HexAt(s,8))
ts.Close
ReadPNG = HW
End Function


Function ReadGIF(fichero)
Dim fso, ts, s, HW, nbytes
HW = Array("","")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
s = Right(ts.Read(10), 4)
HW(0) = HexToDec(HexAt(s,2) & HexAt(s,1))
HW(1) = HexToDec(HexAt(s,4) & HexAt(s,3))
ts.Close
ReadGIF = HW
End Function


Function ReadWMF(fichero)
Dim fso, ts, s, HW, nbytes
HW = Array("","")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
s = Right(ts.Read(14), 4)
HW(0) = HexToDec(HexAt(s,2) & HexAt(s,1))
HW(1) = HexToDec(HexAt(s,4) & HexAt(s,3))
ts.Close
ReadWMF = HW
End Function


Function ReadBMP(fichero)
Dim fso, ts, s, HW, nbytes
HW = Array("","")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath("/" & fichero), 1)
s = Right(ts.Read(24), 8)
HW(0) = HexToDec(HexAt(s,4) & HexAt(s,3))
HW(1) = HexToDec(HexAt(s,8) & HexAt(s,7))
ts.Close
ReadBMP = HW
End Function


Function isDigit(c)
If inStr("0123456789", c) <> 0 Then
isDigit = true
Else
isDigit = false
End If
End Function


Function isHex(c)
If inStr("0123456789ABCDEFabcdef", c) <> 0 Then
isHex = true
Else
ishex = false
End If
End Function


Function HexToDec(cadhex)
Dim n, i, ch, decimal
decimal = 0
n = Len(cadhex)
For i=1 To n
ch = Mid(cadhex, i, 1)
If isHex(ch) Then
decimal = decimal * 16
If isDigit(c) Then
decimal = decimal + ch
Else
decimal = decimal + Asc(uCase(ch)) - Asc("A")
End If
Else
HexToDec = -1
End If
Next
HexToDec = decimal
End Function
%>
neweb 2002-01-14
  • 打赏
  • 举报
回复
关键是要把打开的文件转换成二进制,而这正是ASP力不成心的地方,不过下面两个函数应该有点帮助,是将字符转换成二进制用的,研究一下吧:

"函数1:
"将字符串转换成二进制
Function StoB(varstr)
str2bin = ""
For i = 1 To Len(varstr)
varchar = Mid(varstr, i, 1)
str2bin = str2bin & ChrB(AscB(varchar))
Next
StoB = str2bin
End Function

"函数2:
"将每两个字符前面加"&H"表示是十六进制,然后把"&H??"加到"chr()"里面
"具体功能是什麽,我也说不清,反正是必须的

Function SS(varstr)
BB = ""
For i = 1 To Len(varstr)/2
varchar = "&H" & Mid(varstr, 2*i-1, 2)
BB=BB & chr(varchar)
Next
SS = BB
End Function
shuangyulove 2002-01-14
  • 打赏
  • 举报
回复
一定要用组件了
Hoper 2002-01-14
  • 打赏
  • 举报
回复
用midb还是不行,数据还是对不上
Hoper 2002-01-14
  • 打赏
  • 举报
回复
我顶....
julyclyde 2002-01-12
  • 打赏
  • 举报
回复
你不会融会贯通一下!打开文件读字节就可以了嘛
Hoper 2002-01-12
  • 打赏
  • 举报
回复
beyond_xiruo(希偌)的方法是测定表单中上传图片的尺寸,我所想要的是在目录中已存在的图片文件的尺寸.
julyclyde 2002-01-12
  • 打赏
  • 举报
回复
用MIDB函数,不要用MID
Hoper 2002-01-12
  • 打赏
  • 举报
回复
问题就在于不能用组件,如果能用我早自己写了。
我觉得应该可以用纯asp完成这个问题的。
既然文件已经可以读出来了,关键在于如何处理读出来的数据。。。
neweb 2002-01-12
  • 打赏
  • 举报
回复
非组件或 ISAPI 做不到,我可以帮你写一个,如果你不急着要的话。

怎么谢我?
ar7_top 2002-01-12
  • 打赏
  • 举报
回复
┏━━━━━━━━━━━━━━━┓
┃               ┃
┃Good Good Study┃
┃ Sky  Sky  Up  ┃
┃               ┃
┗━━━━━━━━━━━━━━━┛
Hoper 2002-01-12
  • 打赏
  • 举报
回复
问题就出在这里,打开文件读字节,理论上全对的,实际取过来的数据却对不上。
再附上我写的代码:
Function ReadGIF(filename)
Dim fso, ts, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(filename), 1)
s = Right(ts.Read(10), 4)
response.write mid(s,2,1) & mid(s,1,1)&"<br>" '宽的十六进制
response.write mid(s,4,1) & mid(s,3,1) '高的十六进制
ts.Close
End Function
HapTears 2002-01-11
  • 打赏
  • 举报
回复
不好意思看错了,那个是文件字节大小!
HapTears 2002-01-11
  • 打赏
  • 举报
回复
Function ShowFileSize(filespec)'filespec为文件名或目录名
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(filespec)
s = UCase(f.Name) & " 大小为 " & f.size & " 字节。"
ShowFileSize = s
End Function

Hoper 2002-01-11
  • 打赏
  • 举报
回复
我要的不是上传图片的尺寸,我要的是测定已存在的图片的尺寸.
希偌 2002-01-11
  • 打赏
  • 举报
回复
呵呵!
<%
formsize = Request.TotalBytes
formdata = Request.BinaryRead (FormSize)

file = GetFile(formsize,formdata)
ImageInfo = checkImageFormat(file)

function lngConvert2(strTemp)
str1=leftb(strTemp,1)
str2=rightb(strTemp,1)
lngConvert2 = clng(ascb(str2) + ((ascb(str1) * 256)))
end function

function lngConvert(strTemp)
str1=leftb(strTemp,1)
str2=rightb(strTemp,1)
len1=ascb(str1)
len2=ascb(str2)
lngConvert = clng(ascb(str1) + ascb(str2) * 256)
end function

"---------------------------------------RETURN THE IMAGE FORMAT--------------------
function checkImageFormat(checkdata)
""--return: A,B,C (A is image format; B is height pixels; C is width pixels; D is color depth)

flag=0 """"""""0 is not jpg/gif/png image; 1 is jpg/gif/png

"-------------------------------------------------------check jpg----------------
if flag=0 then
tempstr=Leftb(checkdata,10)
tstr=chrb(255)&chrb(216)&chrb(255)&chrb(224)&chrb(0)&chrb(16)&chrb(74)&chrb(70)&chrb(73)&chrb(70)

if strcomp(tempstr,tstr,0)=0 then
msgstr01="jpg"
lngSize = len(checkdata)
flgFound = 0

strTarget = chrb(255) & chrb(216) & chrb(255)
flgFound = instrb(checkdata, strTarget)

lngPos = flgFound + 2
ExitLoop = false

do while ExitLoop = False and lngPos < lngSize

do while ascb(midb(checkdata, lngPos, 1)) = 255 and lngPos < lngSize
lngPos = lngPos + 1
loop

if ascb(midb(checkdata, lngPos, 1)) < 192 or ascb(midb(checkdata, lngPos, 1)) > 195 then
lngMarkerSize = lngConvert2(midb(checkdata, lngPos + 1, 2))
lngPos = lngPos + lngMarkerSize + 1
else
ExitLoop = True
end if

loop

i_Height = lngConvert2(midb(checkdata, lngPos +4, 2))
i_Width = lngConvert2(midb(checkdata, lngPos +6, 2))
i_Depth = 2 ^ (ascb(midb(checkdata, lngPos + 8, 1)) * 8)

msgstr02=","& i_height & "," & i_width &","& i_Depth
flag=2
else
flag=0
end if


end if
""--------------------------------------------------check gif--------------------------------
if flag=0 then
tempstr=Leftb(checkdata,6)
tstr=chrb(71)&chrb(73)&chrb(70)&chrb(56)&chrb(57)&chrb(97)
tstr2=chrb(71)&chrb(73)&chrb(70)&chrb(56)&chrb(55)&chrb(97)
if strcomp(tempstr,tstr,0)=0 or strcomp(tempstr,tstr2)=0 then
msgstr03="gif"

i_width=lngConvert(midb(checkdata,7,2))
i_height=lngConvert(midb(checkdata,9,2))
i_Depth = 2 ^ ((ascb(midb(checkdata, 11, 1)) and 7) + 1)
msgstr04=","& i_height & ","& i_width &","& i_Depth

flag=2
else
flag=0
end if
end if

""---------------------------------------------------check png------------------------------
if flag=0 then
tempstr=Leftb(checkdata,4)
tstr=chrb(137)&chrb(80)&chrb(78)&chrb(71)
if strcomp(tempstr,tstr,0)=0 then
msgstr05="png"

i_Width = lngConvert2(midb(checkdata, 19, 2))
i_Height = lngConvert2(midb(checkdata, 23, 2))
i_Depth = lngConvert(midb(checkdata, 25, 2))

select case ascb(right(i_Depth,1))
case 0
i_Depth = 2 ^ (asc(left(i_Depth, 1)))
gfxSpex = True
case 2
i_Depth = 2 ^ (asc(left(i_Depth, 1)) * 3)
gfxSpex = True
case 3
i_Depth = 2 ^ (asc(left(i_Depth, 1))) ""8
gfxSpex = True
case 4
i_Depth = 2 ^ (asc(left(i_Depth, 1)) * 2)
gfxSpex = True
case 6
i_Depth = 2 ^ (asc(left(i_Depth, 1)) * 4)
gfxSpex = True
case else
i_Depth = -1
end select
msgstr06=","& i_height & ","& i_width &","& i_Depth

flag=2
else
flag=0
end if
end if
if flag=0 then
msgstr07="no image"
end if
msgstr=msgstr01 & msgstr02 & msgstr03 & msgstr04 & msgstr05 & msgstr06 & msgstr07

checkImageFormat=msgstr
end function

%>
<HTML>
<HEAD>
<META content="text/html; charset=GB2312" http-equiv=Content-Type>
</HEAD>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<br>
<br>

<table width="60%" border="0" cellspacing="0" cellpadding="0" bordercolorlight="#666666" bordercolordark="#FFFFFF" align="center">
<tr>
<td> <br>
<form name="form1" method="post" action="" enctype="multipart/form-data">
文件:<input type="file" name="file1" size="50"><br>
<!-- 说明:<input type="text" name="info" size="50"><br> -->
<input type="submit" name="Submit" value="上传">
</FORM>
</td>
</tr>
</table>
图片信息(格式,宽,高,色深):<%=ImageInfo%>
</BODY>
</HTML
Hoper 2002-01-11
  • 打赏
  • 举报
回复
可以打开,上面的程序运行起来一切正常,就是读取的数据不正确.
talentboy 2002-01-11
  • 打赏
  • 举报
回复
OpenTextFile能打开图片吗?
加载更多回复(4)

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧