ASP中取得图片宽度和高度的类(无组件)

 1   
 2Class ImgWHInfo '获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP   
 3Dim ASO   
 4Private Sub Class_Initialize   
 5Set ASO=Server.CreateObject("ADODB.Stream")   
 6ASO.Mode=3   
 7ASO.Type=1   
 8ASO.Open   
 9End Sub   
10Private Sub Class_Terminate   
11Err.Clear   
12Set ASO=Nothing   
13End Sub   
14  
15Private Function Bin2Str(Bin)   
16Dim I, Str   
17For I=1 To LenB(Bin)   
18clow=MidB(Bin,I,1)   
19If ASCB(clow)<128 Then   
20Str = Str & Chr(ASCB(clow))   
21Else   
22I=I+1   
23If I <= LenB(Bin) Then Str = Str & Chr(ASCW(MidB(Bin,I,1)&clow))   
24End If   
25Next   
26Bin2Str = Str   
27End Function   
28  
29Private Function Num2Str(Num,Base,Lens)   
30Dim Ret   
31Ret = ""   
32While(Num>=Base)   
33Ret = (Num Mod Base) & Ret   
34Num = (Num - Num Mod Base)/Base   
35Wend   
36Num2Str = Right(String(Lens,"0") & Num & Ret,Lens)   
37End Function   
38  
39Private Function Str2Num(Str,Base)   
40Dim Ret,I   
41Ret = 0   
42For I=1 To Len(Str)   
43Ret = Ret *base + Cint(Mid(Str,I,1))   
44Next   
45Str2Num=Ret   
46End Function   
47  
48Private Function BinVal(Bin)   
49Dim Ret,I   
50Ret = 0   
51For I = LenB(Bin) To 1 Step -1   
52Ret = Ret *256 + AscB(MidB(Bin,I,1))   
53Next   
54BinVal=Ret   
55End Function   
56  
57Private Function BinVal2(Bin)   
58Dim Ret,I   
59Ret = 0   
60For I = 1 To LenB(Bin)   
61Ret = Ret *256 + AscB(MidB(Bin,I,1))   
62Next   
63BinVal2=Ret   
64End Function   
65  
66Private Function GetImageSize(filespec)   
67Dim bFlag   
68Dim Ret(3)   
69ASO.LoadFromFile(filespec)   
70bFlag=ASO.Read(3)   
71Select Case Hex(binVal(bFlag))   
72Case "4E5089":   
73ASO.Read(15)   
74ret(0)="PNG"   
75ret(1)=BinVal2(ASO.Read(2))   
76ASO.Read(2)   
77ret(2)=BinVal2(ASO.Read(2))   
78Case "464947":   
79ASO.read(3)   
80ret(0)="gif"   
81ret(1)=BinVal(ASO.Read(2))   
82ret(2)=BinVal(ASO.Read(2))   
83Case "535746":   
84ASO.read(5)   
85binData=ASO.Read(1)   
86sConv=Num2Str(ascb(binData),2 ,8)   
87nBits=Str2Num(left(sConv,5),2)   
88sConv=mid(sConv,6)   
89While(len(sConv)

<nbits4) "ffd8ff":="" ,8)="" and="" aso.eos="" bindata="ASO.Read(1)" case="" do="" do:="" if="" loop="" not="" p1="" ret(0)="SWF" ret(1)="Int(Abs(Str2Num(Mid(sConv,1nBits+1,nBits),2)-Str2Num(Mid(sConv,0nBits+1,nBits),2))/20)" ret(2)="Int(Abs(Str2Num(Mid(sConv,3nBits+1,nBits),2)-Str2Num(Mid(sConv,2*nBits+1,nBits),2))/20)" sconv="sConv&amp;Num2Str(AscB(binData),2" wend="" while="">191 And p1&lt;196 Then Exit Do Else ASO.read(binval2(ASO.Read(2))-2)
Do:p1=binVal(ASO.Read(1)):Loop While p1&lt;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=""" &amp; ret(1) &amp;""" height=""" &amp; ret(2) &amp;""""
getimagesize=ret
End Function

Public Function imgW(IMGPath)
Dim FSO,IMGFile,FileExt,Arr
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(IMGPath)) Then
Set IMGFile = FSO.GetFile(IMGPath)
FileExt=FSO.GetExtensionName(IMGPath)
Select Case FileExt
Case "gif","bmp","jpg","png":
Arr=GetImageSize(IMGFile.Path)
imgW = Arr(1)
End Select
Set IMGFile=Nothing
Else
imgW = 0
End If
Set FSO=Nothing
End Function

Public Function imgH(IMGPath)
Dim FSO,IMGFile,FileExt,Arr
Set FSO = server.CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(IMGPath)) Then
Set IMGFile = FSO.GetFile(IMGPath)
FileExt=FSO.GetExtensionName(IMGPath)
Select Case FileExt
Case "gif","bmp","jpg","png":
Arr=getImageSize(IMGFile.Path)
imgH = Arr(2)
End Select
Set IMGFile=Nothing
Else
imgH = 0
End If
Set FSO=Nothing
End Function
End Class

IMGPath="Test.jpg"

Set PP = New ImgWHInfo
W = PP.imgW(Server.Mappath(IMGPath))
H = PP.imgH(Server.Mappath(IMGPath))
Set pp = Nothing

Response.Write("<img border="0" src='"&amp;IMGPath&amp;"'/><br/>宽:"&amp;W&amp;";高:"&amp;H)

Published At
Categories with Web编程
Tagged with
comments powered by Disqus