预览模式: 普通 | 列表

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  

ASP-在 Access 中使用“存储过程”

 我们已经熟悉在 ASP 中通过调用 SQL Server 存储过程来执行数据库操作,不过大家是否知道,在桌面级数据库 Access 中,我们也能够创建并使用“存储过程”?

Access + ASP 是开发轻量级 Web 应用程序的绝佳组合:简单,快速,兼容性好,但是性能通常不高。并且,用 ADODB.Connection 和 Recordset 对象来执行 SQL 语句的方式,也有一些不方便,因为带参数的 SQL 语句的参数值也常常是拼接到字符串中,于是便有了诸如“单引号问题”这样的麻烦。使用存储过程的一个好处就是,支持 SQL 语句参数值的另外提供。

事实上,Access(2000 及以上版本)中所谓“存储过程”,和 SQL Server 中的 Stored Procedure 是不能比的。它只能算是“Stored Procedure Lite”,不支持多条 SQL 语句,不支持逻辑语句(呵呵,毕竟不是 T-SQL)等等,我也还不清楚它是不是预编译了。不过,正如同 VBScript 实现的所谓“类”仅仅具有封装性,对于代码结构的“美化”和程序重用性具有很大促进一样,Access 的“轻量存储过程”,对于规范,小出错几率的数据库操作应该也有帮助,并且性能可能会有提高。

下面我译 step by step 的方式,介绍如何在 Access 中创建存储过程,然后在 ASP 程序中使用它。

(一)在 Access 中创建“存储过程”

不知道大家的 Access 应用水平如何,反正它对于我来说,仅仅就是一个 MDB 数据库文件的创建工具,我只会新建 MDB 文件,然后在 Access 的界面中创建表,索引,约束等等,over~

Access 中的“查询”,就扮演了存储过程的角色。下文中我提到的 Access 的“存储过程”或者“查询”都是指这个东西

对于“查询”的创建,Access 提供了一个傻瓜工具,类似 VS.NET 里边建立 DataAdapter 时的向导。不过我喜欢直接写 SQL 代码

然后在 Access 主界面上点击左侧的“查询”按钮,再在右边双击“在设计视图中创建查询”,以打开查询设计视图。

这时弹出的是可视化的查询生成器,我们首先添加 SQL 语句需要涉及的表。

添加表之后,在设计视图上点击鼠标右键,选择“SQL 视图”,以切换到 SQL 代码编辑窗口。

好,下面说说 Access 的存储过程的特点。

Access 的查询,我目前的感觉是对 SQL 语句的一个包装,或许进行了某种优化如预编译。我们不能像写 SQL Server 存储过程那样使用多重操作,事务,逻辑判断,循环等等……

但是我们使用 Access 存储过程的主要目的,就是使用参数额外提供的查询,使用存储过程,我们不必再面对将参数值拼接到 SQL 语句字符串中时遇到的各种麻烦,比如:

代码:
Dim sql
sql = "SELECT * FROM Users WHERE UserName = '" & userName & "'"

以上代码中,如果字符串变量 userName 中含有“'”单引号,则会报错。我们必须手工转化:

代码:
Dim sql
sql = "SELECT * FROM Users WHERE UserName = '" & Replace(userName, "'", "''") & "'" '转化为连续两个单引号

而使用带参数查询,我们的 SQL 语句可以写为:

代码:
Dim sql
sql = "SELECT * FROM Users WHERE UserName = @userName"

然后把参数 @userName 的值以 Command 对象的 Parameter 属性来传入即可,很方便直观。

代码:
With cmd
    '创建参数对象
    .Parameters.Append .CreateParameter("@userName")

    '给各参数指定值
    .Parameters("@userName") = userName
End With

这里还要说明 Access 存储过程中参数的使用。和 SQL Server 的存储过程中用 @ 变量指定参数,然后同名传入参数对象不同,Access 中的参数,是以“顺序”而非“名字”来识别的。传入的参数无需指定名字,随便起,SQL 中的参数名字也可以随便起,只要传入参数值时,按照 SQL 语句中的参数出现顺序指定就行了。通常,我们使用 Command 对象的 Execute 方法,直接传入参数值数组来执行~

代码:
cmd.Execute , Array(userName)

再比如,你的一个 Access 存储过程这么写:

代码:
select * from Users where UserName = p_UserName and BookTitle = p_bookTitle

你可以就这么执行,通过传入参数值数组,但是顺序要对应:

代码:
cmd.Execute , Array(userName, bookTitle)

OK,看看我们的例子中使用的两个查询,一个是写入数据。写好 SQL 语句后保存并命名。

另一个读取数据的存储过程代码。


(二)使用存储过程

然后我们可以在 ASP 程序中调用这些存储过程了。

这里可以看到为什么我说 Access 中的查询就是它的存储过程——我们的 Command 对象的 CommandType 属性设置的是 4,即 Stored Proc!

so...

以下的代码很简单:

 

ASP/Visual Basic代码
  1. <%   
  2. Option Explicit   
  3. Dim s   
  4. Randomize   
  5. s = Rnd * 100   
  6.   
  7. Dim conn, cmd   
  8. Set conn = Server.CreateObject("ADODB.Connection")   
  9. Set cmd = Server.CreateObject("ADODB.Command")   
  10.   
  11. conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("sp.mdb")   
  12.   
  13. With cmd   
  14.     .ActiveConnection = conn   
  15.     .CommandType = &H0004 '存储过程   
  16.     .CommandText = "AddNewData"  
  17. End With  
  18.   
  19. cmd.Execute , Array(CStr(Now()), CSng(s))   
  20.   
  21. With cmd   
  22.     .ActiveConnection = conn   
  23.     .CommandType = &H0004 '存储过程   
  24.     .CommandText = "GetData"  
  25. End With  
  26.   
  27. Dim resultRS, resultArray   
  28. Set resultRS = cmd.Execute(, Null)   
  29.   
  30. If Not resultRS.EOF Then  
  31.     resultArray = resultRS.GetRows()   
  32. End If  
  33.   
  34. Set resultRS = Nothing  
  35. Set cmd = Nothing  
  36. conn.Close   
  37. Set conn = Nothing  
  38.   
  39. Response.Write "<ul>"  
  40. Dim i   
  41. For i = 0 To UBound(resultArray, 2)   
  42.     Response.Write "<li>" & resultArray(0, i)   
  43.     Response.Write " " & resultArray(1, i)   
  44.     Response.Write " " & resultArray(2, i)   
  45.     Response.Write "</li>"  
  46. Next  
  47. Response.Write "</ul>"  
  48. %>   
  • 1