如何用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

理论上应该没错,但我实用的时候发现读出来的数据根本不对.
百思不得其解,望高手指点.
...全文
227 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)
Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解的依赖注入和RPC机制。 Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以直接在你的网页里面显示搜查的结果。 github-java-api github-java-api 是 Github 网站 API 的 Java 语言版本。 java缓存工具 SimpleCache SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的定义 4. 使用redis作缓存时,支持list类型的级数据结构,更适合论坛帖子列表这种类型的数据 5. 支持混合使用redis缓存和memcached缓存。可以将列表数据缓存到redis中,其他kv结构数据继续缓存到memcached 6. 支持redis的主从集群,可以做读写分离。缓存读取自redis的slave节点,写入到redis的master节点。 Java对象的SQL接口 JoSQL JoSQL(SQLforJavaObjects)为Java开发者提供运用SQL语句来操作Java对象集的能力.利用JoSQL可以像操作数据库中的数据一样对任何Java对象集进行查询,排序,分组。 搜索自动提示 Autotips AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用程序)。 重复文件检查工具 FindDup.tar FindDup 是一个简单易用的工具,用来检查计算机上重复的文件。 OpenID的Java客户端 JOpenID JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor 的文件持久化组件,以及一个吞吐量的可靠事务日志组件。 Google地图JSP标签库 利用Google:maps JSP标签库就能够在你的Web站点上实现GoogleMaps的所有功能而且不需要javascript或AJAX编程。它还能够与JSTL相结合生成数据库驱动的动态Maps。 OAuth 实现框架 Agorava Agorava 是一个实现了 OAuth 1.0a 和 OAuth 2.0 的框架,提供了简单的方式通过社交媒体进行身份认证的功能。 Eclipse的JavaScript插件 JSEditor JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个性能的开源java数据库连接池实现库。它的设计初衷就是为了提数据库连接池的性能,根据某些试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K

28,405

社区成员

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

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