ASP获取图片的大小,宽,高,类型,属性

ASP/Visual Basic代码
  1. '====================================/\=======================================   
  2. 'Powered By CMSDream Copyright © 2007-2008 All rights reserved.   
  3. '20:43 2008-7-15   
  4. '该功能支持的格式有:jpg,bmp,png,gif,swf   
  5. '====================================\/=======================================   
  6. Class cnGhostImage   
  7.     Private ASO, FSO, m_IMGPath, m_IMGExsits   
  8.     Private Sub Class_Initialize   
  9.         m_IMGPath = ""  
  10.     End Sub  
  11.   
  12.     Private Sub Class_Terminate   
  13.         Set ASO=Nothing  
  14.         Set FSO=Nothing  
  15.     End Sub  
  16.   
  17.     Public Property Let Path(mValue)   
  18.         m_IMGPath = mValue   
  19.         If m_IMGPath = "" OR IsNull(m_IMGPath) Then  
  20.             m_IMGExsits = False  
  21.         Else  
  22.             if not instr(m_IMGPath,":") > 0 then m_IMGPath = Server.MapPath(m_IMGPath)   
  23.             Set FSO = Server.CreateObject("Scripting.FileSystemObject")    
  24.             m_IMGExsits = FSO.FileExists(m_IMGPath)   
  25.             If m_IMGExsits Then  
  26.                 Set ASO=Server.CreateObject("ADODB.Stream")   
  27.                 ASO.Mode=3   
  28.                 ASO.Type=1   
  29.                 ASO.Open   
  30.             End If  
  31.         End If  
  32.     End Property  
  33.   
  34.     Private Function Bin2Str(Bin)   
  35.         On Error Resume Next  
  36.         Dim I, Str   
  37.         For I=1 To LenB(Bin)   
  38.             clow=MidB(Bin,I,1)   
  39.             If ASCB(clow)<128 Then  
  40.                 Str = Str & Chr(ASCB(clow))   
  41.             Else  
  42.                 I=I+1   
  43.                 If I <= LenB(Bin) Then Str = Str & Chr(ASCW(MidB(Bin,I,1)&clow))   
  44.             End If  
  45.         Next  
  46.         Bin2Str = Str   
  47.     End Function  
  48.          
  49.     Private Function Num2Str(Num,Base,Lens)   
  50.         Dim Ret   
  51.         Ret = ""  
  52.         While(Num>=Base)   
  53.             Ret = (Num Mod Base) & Ret   
  54.             Num = (Num - Num Mod Base)/Base   
  55.         Wend   
  56.         Num2Str = Right(String(Lens,"0") & Num & Ret,Lens)   
  57.     End Function  
  58.   
  59.     Private Function Str2Num(Str,Base)    
  60.         Dim Ret,I   
  61.         Ret = 0    
  62.         For I=1 To Len(Str)    
  63.             Ret = Ret *base + Cint(Mid(Str,I,1))    
  64.         Next    
  65.         Str2Num=Ret    
  66.     End Function    
  67.   
  68.     Private Function BinVal(Bin)    
  69.         On Error Resume Next  
  70.         Dim Ret,I   
  71.         Ret = 0    
  72.         For I = LenB(Bin) To 1 Step -1    
  73.             Ret = Ret *256 + AscB(MidB(Bin,I,1))    
  74.         Next    
  75.         BinVal=Ret    
  76.     End Function    
  77.   
  78.     Private Function BinVal2(Bin)    
  79.         Dim Ret,I   
  80.         Ret = 0    
  81.         For I = 1 To LenB(Bin)    
  82.             Ret = Ret *256 + AscB(MidB(Bin,I,1))    
  83.         Next    
  84.         BinVal2=Ret    
  85.     End Function    
  86.   
  87.     Private Function GetImageSize(filespec)   
  88.         Dim bFlag   
  89.         Dim Ret(3)    
  90.         ASO.LoadFromFile(filespec)    
  91.         bFlag=ASO.Read(3)    
  92.         Select Case Hex(binVal(bFlag))    
  93.             Case "4E5089":    
  94.                 ASO.Read(15)    
  95.                 ret(0)="PNG"    
  96.                 ret(1)=BinVal2(ASO.Read(2))    
  97.                 ASO.Read(2)    
  98.                 ret(2)=BinVal2(ASO.Read(2))    
  99.             Case "464947":    
  100.                 ASO.read(3)    
  101.                 ret(0)="gif"    
  102.                 ret(1)=BinVal(ASO.Read(2))    
  103.                 ret(2)=BinVal(ASO.Read(2))    
  104.             Case "535746":    
  105.                 ASO.read(5)    
  106.                 binData=ASO.Read(1)    
  107.                 sConv=Num2Str(ascb(binData),2 ,8)    
  108.                 nBits=Str2Num(left(sConv,5),2)    
  109.                 sConv=mid(sConv,6)    
  110.                 While(len(sConv)<nBits*4)    
  111.                     binData=ASO.Read(1)    
  112.                     sConv=sConv&Num2Str(AscB(binData),2 ,8)    
  113.                 Wend    
  114.                 ret(0)="SWF"    
  115.                 ret(1)=Int(Abs(Str2Num(Mid(sConv,1*nBits+1,nBits),2)-Str2Num(Mid(sConv,0*nBits+1,nBits),2))/20)    
  116.                 ret(2)=Int(Abs(Str2Num(Mid(sConv,3*nBits+1,nBits),2)-Str2Num(Mid(sConv,2*nBits+1,nBits),2))/20)    
  117.             Case "FFD8FF":    
  118.                 dim p1   
  119.                 Do     
  120.                 Do: p1=binVal(ASO.Read(1)): Loop While p1=255 And Not ASO.EOS    
  121.                     If p1>191 And p1<196 Then Exit Do Else ASO.read(binval2(ASO.Read(2))-2)    
  122.                     Do:p1=binVal(ASO.Read(1)):Loop While p1<255 And Not ASO.EOS    
  123.                 Loop While True    
  124.                 ASO.Read(3)    
  125.                 ret(0)="JPG"    
  126.                 ret(2)=binval2(ASO.Read(2))    
  127.                 ret(1)=binval2(ASO.Read(2))    
  128.             Case Else:    
  129.                 If left(Bin2Str(bFlag),2)="BM" Then    
  130.                     ASO.Read(15)    
  131.                     ret(0)="BMP"    
  132.                     ret(1)=binval(ASO.Read(4))    
  133.                     ret(2)=binval(ASO.Read(4))    
  134.                 Else    
  135.                     ret(0)=""    
  136.                 End If    
  137.         End Select    
  138.         ret(3)="width=""" & ret(1) """ height=""" & ret(2) """"    
  139.         getimagesize=ret    
  140.     End Function    
  141.   
  142.     Public Function W()   
  143.         if m_IMGPath <> "" then m_IMGPath = lcase(m_IMGPath)   
  144.         If m_IMGExsits Then    
  145.             Dim IMGFile,FileExt,Arr   
  146.             Set IMGFile = FSO.GetFile(m_IMGPath)    
  147.             FileExt=FSO.GetExtensionName(m_IMGPath)    
  148.             Select Case FileExt    
  149.                 Case "gif","bmp","jpg","png":    
  150.                     Arr=GetImageSize(IMGFile.Path)    
  151.                     W = Arr(1)    
  152.             End Select    
  153.             Set IMGFile=Nothing    
  154.         Else  
  155.             W = 0   
  156.         End If  
  157.     End Function    
  158.   
  159.     Public Function H()   
  160.         if m_IMGPath <> "" then m_IMGPath = lcase(m_IMGPath)   
  161.         If m_IMGExsits Then    
  162.             Dim IMGFile,FileExt,Arr   
  163.             Set IMGFile = FSO.GetFile(m_IMGPath)    
  164.             FileExt=FSO.GetExtensionName(m_IMGPath)    
  165.             Select Case FileExt    
  166.                 Case "gif","bmp","jpg","png":    
  167.                     Arr=getImageSize(IMGFile.Path)    
  168.                     H = Arr(2)    
  169.             End Select    
  170.             Set IMGFile=Nothing    
  171.         Else  
  172.             H = 0    
  173.         End If        
  174.     End Function  
  175.   
  176.     Public Function S()   
  177.         if m_IMGPath <> "" then m_IMGPath = lcase(m_IMGPath)   
  178.         If m_IMGExsits Then    
  179.             Dim IMGFile   
  180.             Set IMGFile = FSO.GetFile(m_IMGPath)    
  181.             S = IMGFile.Size   
  182.             Set IMGFile=Nothing    
  183.         Else  
  184.             S = 0    
  185.         End If        
  186.     End Function  
  187.   
  188.     Public Function T()   
  189.         if m_IMGPath <> "" then m_IMGPath = lcase(m_IMGPath)   
  190.         If m_IMGExsits Then    
  191.             Dim IMGFile   
  192.             Set IMGFile = FSO.GetFile(m_IMGPath)    
  193.             T = IMGFile.Type   
  194.             Set IMGFile=Nothing    
  195.         Else  
  196.             T = "unknown"  
  197.         End If        
  198.     End Function  
  199. End Class  

应用实例代码:

ASP/Visual Basic代码
  1. Dim img   
  2. set img = new cnGhostImage   
  3. img.Path = Server.MapPath("文件地址.jpg")   
  4. '输出宽度:   
  5. response.write "宽度:" & img.W() & " px<br/>"  
  6. '输出高度:   
  7. response.write "高度:" & img.H() & " px<br/>"  
  8. '输出大小:   
  9. response.write "大小:" & img.S() & " bytes<br/>"  
  10. '输出类型:   
  11. response.write "类型:" & img.T()   
  12.   
  13. set img = Nothing  
引用通告地址: 点击获取引用地址
评论: 3 | 引用: 0 | 阅读: 1927 | 打印 | 打包
aaaa [ 2008-11-07 15:53 ]
该内容只有管理员可见
原来阿甘 [ 2009-04-19 21:14 ]
1、Microsoft VBScript 编译器错误 '800a0401'

语句未结束

wwwrootindex.asp, line 140

2、能否判断压缩swf尺寸。。。
原来阿甘 [ 2009-04-19 21:55 ]
压缩swf尺寸不能判断
  • 1