虛擬主機不支援註冊 COM 組件?無法使用 ASPJpeg、ASPImage?本文提供純 ASP + ADODB.Stream 讀取 JPG/PNG/GIF/BMP/SWF 真實尺寸,配合等比縮放算法,零組件生成高質量縮略圖顯示。
傳統 ASCII 解析法對 JPG 失效。利用 ADODB.Stream 讀取二進制頭部,解析各格式標準結構:
<%
Class GPS
Private aso
Private Sub Class_Initialize
Set aso = CreateObject("ADODB.Stream")
aso.Mode = 3 ' adModeReadWrite
aso.Type = 1 ' adTypeBinary
aso.Open
End Sub
Private Sub Class_Terminate
Set aso = Nothing
End Sub
' 核心:獲取圖片尺寸
' 返回陣列:Array(類型, 寬度, 高度, "width=xxx height=yyy")
Public Function GetImageSize(fileSpec)
Dim ret(3), bFlag, binData, nBits, sConv
aso.LoadFromFile(fileSpec)
bFlag = aso.Read(3)
Select Case Hex(BinVal(bFlag))
Case "4E5089" ' PNG: 89 50 4E 47
aso.Read(15) ' 跳過 IHDR 前 15 字節
ret(0) = "PNG"
ret(1) = BinVal2(aso.Read(2)) ' 寬度
aso.Read(2)
ret(2) = BinVal2(aso.Read(2)) ' 高度
Case "464947" ' GIF: 47 49 46
aso.Read(3) ' 跳過版本號
ret(0) = "GIF"
ret(1) = BinVal(aso.Read(2)) ' 寬度
ret(2) = BinVal(aso.Read(2)) ' 高度
Case "535746" ' SWF: 46 57 53 (FWS) 或 43 57 53 (CWS)
' SWF 解析較複雜,此處略過詳細位運算
ret(0) = "SWF"
' ... 位流解析邏輯 ...
Case "FFD8FF" ' JPEG: FF D8 FF
ret(0) = "JPG"
' 掃描 SOF 標記 (0xFFC0~0xFFCF)
Do
Do: p1 = BinVal(aso.Read(1)): Loop While p1 = 255 And Not aso.EOS
If p1 > 191 And p1 < 196 Then Exit Do
aso.Read(BinVal2(aso.Read(2)) - 2)
Loop While True
aso.Read(3) ' 精度 + 保留
ret(2) = BinVal2(aso.Read(2)) ' 高度
ret(1) = BinVal2(aso.Read(2)) ' 寬度
Case Else
' BMP 檢測
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
' --- 輔助函數:二進制轉數值 ---
Private Function BinVal(bin) ' 大端序 (高位在前)
Dim i, 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) ' 小端序 (低位在前,Intel x86)
Dim i, ret : ret = 0
For i = 1 To LenB(bin)
ret = ret * 256 + AscB(MidB(bin, i, 1))
Next
BinVal2 = ret
End Function
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
End Class
%>
<%
' 獲取圖片絕對路徑
Dim curFile, relPath, absPath, fso, ext, gps, dims
curFile = Server.MapPath(Request.ServerVariables("PATH_INFO"))
relPath = rs("ImgURL") ' 資料庫存儲的相對路徑
absPath = Left(curFile, InStrRev(curFile, "\")) & relPath
' 實例化 GPS 類
Set gps = New GPS
Set fso = Server.CreateObject("Scripting.FileSystemObject")
ext = LCase(fso.GetExtensionName(absPath))
Dim width, height
Select Case ext
Case "jpg","jpeg","png","gif","bmp","swf"
dims = gps.GetImageSize(absPath)
width = dims(1)
height = dims(2)
Case Else
width = 0 : height = 0
End Select
Set gps = Nothing
Set fso = Nothing
%>
<html><body>
<h3>圖片預覽</h3>
<%
If width > 0 And height > 0 Then
' 等比縮放算法:最大寬 300、高 260
Dim maxW, maxH, ratio, dispW, dispH
maxW = 300 : maxH = 260
ratio = width / height
If width > maxW Then
dispW = maxW
dispH = maxW / ratio
Else
dispW = width
dispH = height
End If
If dispH > maxH Then
dispH = maxH
dispW = maxH * ratio
End If
%>
<img src="<%=relPath%>" width="<%=dispW%>" height="<%=dispH%>" border="0">
<br>原始尺寸:<%=width%> x <%=height%>
<br>顯示尺寸:<%=Round(dispW)%> x <%=Round(dispH)%>
<br>格式:<%=UCase(ext)%>
<%
Else
Response.Write "不支援的圖片格式或檔案不存在"
End If
%>
</body></html>
ADODB.Stream.Type=1 讀取原始位元組min(maxW/width, maxH/height) 保持長寬比Class_Terminate 確保 Stream 關閉Server.Transfer 實現動態縮略圖輸出 (輸出二進制 + Content-Type)純 ASP 解決圖片尺寸獲取與縮略顯示,零成本部署,經典方案永流傳。