神武80强化打造满属性:用asp如何获得服务器文件夹的图片的宽度和高度

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 07:54:40
请问不用组件的话要用asp获取服务器中图片的宽度和高度的代码应该怎么写?假设该图片在服务器中的地址为:"images/photo/abc.jpg"。
摆脱各位尽量给一个实例好吗?根据我给出的图片地址。

ASP本身不能实现该功能,但是可以用具有读写二进制文件功能的Adodb.Stream对象来间接实现
<%
'下面这段即可获得你要的那图片的宽和高
imgpath="images/photo/abc.jpg"
set pp=new imgInfo
w = pp.imgW(server.mappath(imgpath))
h = pp.imgH(server.mappath(imgpath))
set pp=nothing
%>

<%
'这一段是所要用的类
Class imgInfo
dim aso
Private Sub Class_Initialize
set aso=CreateObject("Adodb.Stream")
aso.Mode=3
aso.Type=1
aso.Open
End Sub
Private Sub Class_Terminate
err.clear
set aso=nothing
End Sub

Private Function Bin2Str(Bin)
Dim I, Str
For I=1 to LenB(Bin)
clow=MidB(Bin,I,1)
if ASCB(clow)<128 then
Str = Str & Chr(ASCB(clow))
else
I=I+1
if I <= LenB(Bin) then Str = Str & Chr(ASCW(MidB(Bin,I,1)&clow))
end if
Next
Bin2Str = Str
End Function

Private Function Num2Str(num,base,lens)
dim ret
ret = ""
while(num>=base)
ret = (num mod base) & ret
num = (num - num mod base)/base
wend
Num2Str = right(string(lens,"0") & num & ret,lens)
End Function

Private Function Str2Num(str,base)
dim ret
ret = 0
for i=1 to len(str)
ret = ret *base + cint(mid(str,i,1))
next
Str2Num=ret
End Function

Private Function BinVal(bin)
dim ret
ret = 0
for i = lenb(bin) to 1 step -1
ret = ret *256 + ascb(midb(bin,i,1))
next
BinVal=ret
End Function

Private Function BinVal2(bin)
dim ret
ret = 0
for i = 1 to lenb(bin)
ret = ret *256 + ascb(midb(bin,i,1))
next
BinVal2=ret
End Function

Private Function getImageSize(filespec)
dim ret(3)
aso.LoadFromFile(filespec)
bFlag=aso.read(3)
select case hex(binVal(bFlag))
case "4E5089":
aso.read(15)
ret(0)="PNG"
ret(1)=BinVal2(aso.read(2))
aso.read(2)
ret(2)=BinVal2(aso.read(2))
case "464947":
aso.read(3)
ret(0)="GIF"
ret(1)=BinVal(aso.read(2))
ret(2)=BinVal(aso.read(2))
case "535746":
aso.read(5)
binData=aso.Read(1)
sConv=Num2Str(ascb(binData),2 ,8)
nBits=Str2Num(left(sConv,5),2)
sConv=mid(sConv,6)
while(len(sConv)<nBits*4)
binData=aso.Read(1)
sConv=sConv&Num2Str(ascb(binData),2 ,8)
wend
ret(0)="SWF"
ret(1)=int(abs(Str2Num(mid(sConv,1*nBits+1,nBits),2)-Str2Num(mid(sConv,0*nBits+1,nBits),2))/20)
ret(2)=int(abs(Str2Num(mid(sConv,3*nBits+1,nBits),2)-Str2Num(mid(sConv,2*nBits+1,nBits),2))/20)
case "FFD8FF":
do
d p1=binVal(aso.Read(1)): loop while p1=255 and not aso.EOS
if p1>191 and p1<196 then exit do else aso.read(binval2(aso.Read(2))-2)
dp1=binVal(aso.Read(1)):loop while p1<255 and not aso.EOS
loop while true
aso.Read(3)
ret(0)="JPG"
ret(2)=binval2(aso.Read(2))
ret(1)=binval2(aso.Read(2))
case else:
if left(Bin2Str(bFlag),2)="BM" then
aso.Read(15)
ret(0)="BMP"
ret(1)=binval(aso.Read(4))
ret(2)=binval(aso.Read(4))
else
ret(0)=""
end if
end select
ret(3)="width=""" & ret(1) &""" height=""" & ret(2) &""""
getimagesize=ret
End Function

Public Function imgW(pic_path)
Set fso1 = server.CreateObject("Scripting.FileSystemObject")
If (fso1.FileExists(pic_path)) Then
Set f1 = fso1.GetFile(pic_path)
ext=fso1.GetExtensionName(pic_path)
select case ext
case "gif","bmp","jpg","png":
arr=getImageSize(f1.path)
imgW = arr(1)
end select
Set f1=nothing
else
imgW = 0
End if
Set fso1=nothing
End Function

Public Function imgH(pic_path)
Set fso1 = server.CreateObject("Scripting.FileSystemObject")
If (fso1.FileExists(pic_path)) Then
Set f1 = fso1.GetFile(pic_path)
ext=fso1.GetExtensionName(pic_path)
select case ext
case "gif","bmp","jpg","png":
arr=getImageSize(f1.path)
imgH = arr(2)
end select
Set f1=nothing
else
imgH = 0
End if
Set fso1=nothing
End Function
End Class

%>

<%
'----------------------------------------------------------------------
'转发时请保留此声明信息,这段声明不并会影响你的速度!
'******************* 无惧上传类 V1.0 *********************************
'作者:梁无惧
'网站:http://www.25cn.com
'电子邮件:yjlrb@21cn.com
'版权声明:版权所有,源代码公开,各种用途均可免费使用,但是修改后必须把修改后的文件
'发送一份给作者.
'***********************************************************************
'***********************************************************************
'上传类强化记录
'修改者:Fssunwin
'----------------------------------------------------------------------
'添加以下属性:
'InceptFileType 允许上传的文件类型,以英文逗号“,”分隔。
'添加以下方法:
'FileWidth 图片宽度
'FileHeight 图片高度
'----------------------------------------------------------------------
'***********************************************************************

Dim oUpFileStream

Class UpFile_Class
Public Form,File,Version,Err
Private CHK_FileType,CHK_MaxSize

Private Sub Class_Initialize
Version = "无惧上传类 Version V1.0"
Err = -1
CHK_FileType = ""
CHK_MaxSize = -1
Set Form = Server.CreateObject ("Scripting.Dictionary")
Set File = Server.CreateObject ("Scripting.Dictionary")
Set oUpFileStream = Server.CreateObject ("Adodb.Stream")
Form.CompareMode = 1
File.CompareMode = 1
oUpFileStream.Type = 1
oUpFileStream.Mode = 3
oUpFileStream.Open
End Sub

Private Sub Class_Terminate
'清除变量及对像
Form.RemoveAll
Set Form = Nothing
File.RemoveAll
Set File = Nothing
oUpFileStream.Close
Set oUpFileStream = Nothing
End Sub

Public Property Get InceptFileType
InceptFileType = CHK_FileType
End Property
Public Property Let InceptFileType(Byval vType)
CHK_FileType = vType
End Property

Public Property Get MaxSize
MaxSize = CHK_MaxSize
End Property
Public Property Let MaxSize(vSize)
If IsNumeric(vSize) Then CHK_MaxSize = Int(vSize)
End Property

Public Sub GetDate()
'定义变量
Dim RequestBinDate,sSpace,bCrLf,sInfo,iInfoEnd,tStream,iStart,oFileInfo
Dim sFormValue,sFileName,sFormName,RequestSize
Dim iFindStart,iFindEnd,iFormStart,iFormEnd,FileBlag
'代码开始
RequestSize = Int(Request.TotalBytes)
If RequestSize < 1 Then
Err = 1
Exit Sub
End If
Set tStream = Server.CreateObject ("Adodb.Stream")
oUpFileStream.Write Request.BinaryRead (RequestSize)
oUpFileStream.Position = 0
RequestBinDate = oUpFileStream.Read
iFormEnd = oUpFileStream.Size

bCrLf = ChrB (13) & ChrB (10)
'取得每个项目之间的分隔符
sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)
iStart = LenB (sSpace)
iFormStart = iStart+2
'分解项目
Do
iInfoEnd = InStrB (iFormStart,RequestBinDate,bCrLf & bCrLf)+3
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iFormStart
oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.CharSet = "gb2312"
sInfo = tStream.ReadText
'取得表单项目名称
iFormStart = InStrB (iInfoEnd,RequestBinDate,sSpace)-1
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = Mid(sinfo,iFindStart,iFindEnd-iFindStart)
'如果是文件
If InStr(45,sInfo,"filename=""",1) > 0 Then
Set oFileInfo = new FileInfo_Class
'取得文件属性
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid(sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = Mid(sFileName,InStrRev(sFileName, "\")+1)
oFileInfo.FilePath = Left(sFileName,InStrRev(sFileName, "\"))
oFileInfo.FileExt = Lcase(Mid(sFileName,InStrRev(sFileName, ".")+1))
iFindStart = InStr (iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr (iFindStart,sInfo,vbCr)
oFileInfo.FileType = Ucase(Mid(sinfo,iFindStart,iFindEnd-iFindStart))
oFileInfo.FileStart = iInfoEnd
oFileInfo.FileSize = iFormStart -iInfoEnd -2
oFileInfo.FormName = sFormName
If Instr(oFileInfo.FileType,"IMAGE/") Or Instr(oFileInfo.FileType,"FLASH") Then
FileBlag = GetImageSize
oFileInfo.FileExt = FileBlag(0)
oFileInfo.FileWidth = FileBlag(1)
oFileInfo.FileHeight = FileBlag(2)
FileBlag = Empty
End If
If CHK_MaxSize > 0 Then
If oFileInfo.FileSize > CHK_MaxSize Then
Err = 2
Exit Sub
End If
End If
If CheckErr(oFileInfo.FileExt) = False Then
Exit Sub
End If
File.Add sFormName,oFileInfo
Else
'如果是表单项目
tStream.Close
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iInfoEnd
oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
tStream.Position = 0
tStream.Type = 2
tStream.CharSet = "gb2312"
sFormValue = tStream.ReadText
If Form.Exists (sFormName) Then _
Form (sFormName) = Form (sFormName) & ", " & sFormValue _
Else _
Form.Add sFormName,sFormValue
End If
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
Loop Until (iFormStart+2) = iFormEnd
RequestBinDate = ""
Set tStream = Nothing
End Sub

'====================================================================
'验证上传类型
'====================================================================
Private Function CheckErr(Byval ChkExt)
CheckErr=False
If CHK_FileType = "" Then CheckErr=True : Exit Function
Dim ChkStr
ChkStr = ","&Lcase(CHK_FileType)&","
If Instr(ChkStr,","&ChkExt&",")>0 Then
CheckErr=True
Else
Err = 3
End If
End Function
'====================================================================
'图像宽高类型读取
'====================================================================
Private Function Bin2Str(Byval Bin)
Dim i, Str, Sclow
For i = 1 To LenB(Bin)
Sclow = MidB(Bin,i,1)
If ASCB(Sclow)<128 Then
Str = Str & Chr(ASCB(Sclow))
Else
i = i+1
If i <= LenB(Bin) Then Str = Str & Chr(ASCW(MidB(Bin,i,1)&Sclow))
End If
Next
Bin2Str = Str
End Function

Private Function Num2Str(Byval num,Byval Base,Byval Lens)
Dim ImageSize
ImageSize = ""
While(num>=Base)
ImageSize = (num mod Base) & ImageSize
num = (num - num mod Base)/Base
Wend
Num2Str = Right(String(Lens,"0") & num & ImageSize,Lens)
End Function

Private Function Str2Num(Byval str,Byval Base)
Dim ImageSize,i
ImageSize = 0
For i=1 To Len(str)
ImageSize = ImageSize *Base + Cint(Mid(str,i,1))
Next
Str2Num = ImageSize
End Function

Private Function BinVal(Byval bin)
Dim ImageSize,i
ImageSize = 0
For i = lenb(bin) To 1 Step -1
ImageSize = ImageSize *256 + ASCB(Midb(bin,i,1))
Next
BinVal = ImageSize
End Function

Private Function BinVal2(Byval bin)
Dim ImageSize,i
ImageSize = 0
For i = 1 To Lenb(bin)
ImageSize = ImageSize *256 + ASCB(Midb(bin,i,1))
Next
BinVal2 = ImageSize
End Function

Private Function GetImageSize()
Dim ImageSize(2),bFlag
bFlag = oUpFileStream.Read(3)

Select Case Hex(BinVal(bFlag))
Case "4E5089":
oUpFileStream.Read(15)
ImageSize(0) = "png"
ImageSize(1) = BinVal2(oUpFileStream.Read(2))
oUpFileStream.Read(2)
ImageSize(2) = BinVal2(oUpFileStream.Read(2))
Case "464947":
oUpFileStream.Read(3)
ImageSize(0) = "gif"
ImageSize(1) = BinVal(oUpFileStream.Read(2))
ImageSize(2) = BinVal(oUpFileStream.Read(2))
Case "535746":
Dim BinData,sConv,nBits
oUpFileStream.Read(5)
BinData = oUpFileStream.Read(1)
sConv = Num2Str(ASCB(BinData),2 ,8)
nBits = Str2Num(Left(sConv,5),2)
sConv = Mid(sConv,6)
While(Len(sConv)<nBits*4)
BinData = oUpFileStream.Read(1)
sConv = sConv&Num2Str(ASCB(BinData),2 ,8)
Wend
ImageSize(0) = "swf"
ImageSize(1) = Int(ABS(Str2Num(Mid(sConv,1*nBits+1,nBits),2)-Str2Num(Mid(sConv,0*nBits+1,nBits),2))/20)
ImageSize(2) = Int(ABS(Str2Num(Mid(sConv,3*nBits+1,nBits),2)-Str2Num(Mid(sConv,2*nBits+1,nBits),2))/20)
Case "535743":'flashmx
ImageSize(0) = "swf"
ImageSize(1) = 0
ImageSize(2) = 0
Case "FFD8FF":
Dim p1
Do
Do: p1 = BinVal(oUpFileStream.Read(1)): Loop While p1 = 255 And Not oUpFileStream.EOS
If p1>191 and p1<196 Then Exit Do Else oUpFileStream.Read(BinVal2(oUpFileStream.Read(2))-2)
Do:p1 = BinVal(oUpFileStream.Read(1)):Loop While p1<255 And Not oUpFileStream.EOS
Loop While True
oUpFileStream.Read(3)
ImageSize(0) = "jpg"
ImageSize(2) = BinVal2(oUpFileStream.Read(2))
ImageSize(1) = BinVal2(oUpFileStream.Read(2))
Case Else:
If Left(Bin2Str(bFlag),2) = "BM" Then
oUpFileStream.Read(15)
ImageSize(0) = "bmp"
ImageSize(1) = BinVal(oUpFileStream.Read(4))
ImageSize(2) = BinVal(oUpFileStream.Read(4))
Else
ImageSize(0) = "(UNKNOWN)"
End If
End Select
GetImagesize = ImageSize
End Function
End Class

'文件属性类
Class FileInfo_Class
Public FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt,FileWidth,FileHeight
Private Sub Class_Initialize
FileWidth=0
FileHeight=0
End Sub
'保存文件方法
Public Sub SaveToFile (Byval Path)
Dim Ext,oFileStream
Ext = LCase(Mid(Path, InStrRev(Path, ".") + 1))
If Ext <> FileExt Then Exit Sub
If Trim(Path)="" or FileStart=0 or FileName="" or Right(Path,1)="/" Then Exit Sub
'On Error Resume Next
Set oFileStream = CreateObject ("Adodb.Stream")
oFileStream.Type = 1
oFileStream.Mode = 3
oFileStream.Open
oUpFileStream.Position = FileStart
oUpFileStream.CopyTo oFileStream,FileSize
oFileStream.SaveToFile Path,2
oFileStream.Close
Set oFileStream = Nothing
End Sub
'取得文件数据
Public Function FileData
oUpFileStream.Position = FileStart
FileData = oUpFileStream.Read (FileSize)
End Function
End Class
%>