日期:2010-08-25  浏览次数:21040 次

<%
Class DealImgSize
 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
  set aso=nothing
 End Sub

 Private Function Bin2Str(Bin)
  Dim I, Str, clow
  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 BinVal(bin)
  dim ret,i
  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,i
  ret = 0
  for i = 1 to lenb(bin)
   ret = ret *256 + ascb(midb(bin,i,1))
  next
  BinVal2=ret
 End Function
 
 Function getImageSize(filespec)
  dim ret(2),bFlag,p1
  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 "FFD8FF":
   do
    do: 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)
    do:p1=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
  getImageSize=ret
 End Function
 
 Function Resize(ow,oh,rw,rh)
  dim wh(1)
  if cint(ow)>cint(oh) then
   if cint(ow)>cint(rw) then
     wh(0)=rw
  wh(1)=int(oh/(ow/rw))
   else
     wh(0)=ow
  wh(1)=oh
   end if
  else
   if cint(oh)>cint(rh) then
     wh(0)=int(ow/(oh/rh))
  wh(1)=rh
   else
     wh(0)=ow
  wh(1)=oh
   end if
  end if
  Resize=wh
 End Function
End Class
%>

'********************

'  使用方法

'********************
<%
  Dim aa
  Set aa=new DealImgSize
  Dim pw,ph,rpw,rph
  pw=aa.getImageSize(Server.MapPath("01.jpg"))(1)
  ph=aa.getImageSize(Server.MapPath("01.jpg"))(2)
  rpw=aa.Resize(pw,ph,160,120)(0)
  rph=aa.Resize(pw,ph,160,120)(1)
  Response.Write("<img src="http://www.webjx.com/01.jpg" width="&rpw&" height="&rph&">") 
  Set aa=Nothing
%>