佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 無組件生成縮略圖

無組件生成縮略圖

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) Linux服務(wù)器 銀行業(yè)務(wù) 團(tuán)購網(wǎng)站 服務(wù)器配置 阿里云 電子圍欄
網(wǎng)上有不少生成縮略圖的ASP組件。若你的虛擬空間不支持注冊新組件,可能會感覺自己的網(wǎng)站失色不少。心晴不才,結(jié)合網(wǎng)上資源寫了個無組件生成縮略圖程序,僅供參考。

還是先看看基礎(chǔ)部分吧。首先,我們知道在頁面中顯示圖片是如下代碼:


img src="pic.gif" border="0" width="300" height="260">

src是圖片路徑,border控制圖片邊緣寬度,width是圖片的長度,height是圖片的高度。縮略圖的生成其實就是在原始尺寸上縮放。但一般為了盡量少失真,我們都會按比例縮放。于是,獲取圖片的長寬尺寸也就成了生成縮略圖的重點。

下面便是編寫無組件生成縮略圖的步驟:

1.無組件獲取圖片大小

以前剛學(xué)ASP不久時看到過一篇利用ASCII碼實現(xiàn)無組件獲取圖片尺寸的文章。后來試了試,發(fā)現(xiàn)在獲取jpg類圖片尺寸時總是不能正確顯示,在網(wǎng)上查了查,居然有不少網(wǎng)站轉(zhuǎn)載這個程序,但沒有一家指出此程序的缺陷,也就更談不上解決缺陷的辦法了。后來又google了一下,終于找到了一篇介紹利用ADODB.stream獲取圖片尺寸的文章,按其介紹的方法,修改里面的代碼試了試,效果真的還不錯,現(xiàn)在將其拿出來與大家分享:

利用ADODB.stream獲取圖片尺寸的通用類 %
'//////////// GPS:Get Picture Size //////////////
'//////////////利用ADODB.stream獲取圖片尺寸//////////////
'/////////Cited By Leon(心晴) 2005年8月11日//////////
Class GPS
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
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 Num2Str(num,base,lens)
'GPS (2005-8-11)
dim ret
ret = ""
while(num>=base)
ret = (num mod base)  ret
num = (num - num mod base)/base
wend
Num2Str = right(string(lens,"0")  num  ret,lens)
End Function

Private Function Str2Num(str,base)
'GPS (2005-8-11)
dim ret
ret = 0
for i=1 to len(str)
ret = ret *base + cint(mid(str,i,1))
next
Str2Num=ret
End Function

Private Function BinVal(bin)
'GPS (2002-8-11)
dim 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)
'GPS (2002-8-11)
dim ret
ret = 0
for i = 1 to lenb(bin)
ret = ret *256 + ascb(midb(bin,i,1))
next
BinVal2=ret
End Function

'///以下是調(diào)用代碼///
Function getImageSize(filespec) 
'GPS (2002-8-11)
dim ret(3)
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 "535746":
aso.read(5)
binData=aso.Read(1)
sConv=Num2Str(ascb(binData),2 ,8)
nBits=Str2Num(left(sConv,5),2)
sConv=mid(sConv,6)
while(len(sConv)nBits*4)
binData=aso.Read(1)
sConv=sConvNum2Str(ascb(binData),2 ,8)
wend
ret(0)="SWF"
ret(1)=int(abs(Str2Num(mid(sConv,1*nBits+1,nBits),2)-Str2Num(mid(sConv,0*nBits+1,nBits),2))/20)
ret(2)=int(abs(Str2Num(mid(sConv,3*nBits+1,nBits),2)-Str2Num(mid(sConv,2*nBits+1,nBits),2))/20)
case "FFD8FF":
do 
do: p1=binVal(aso.Read(1)): loop while p1=255 and not aso.EOS
if p1>191 and p1196 then exit do else aso.read(binval2(aso.Read(2))-2)
do:p1=binVal(aso.Read(1)):loop while p1255 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="""  ret(1) """ height="""  ret(2) """"
getimagesize=ret
End Function
End Class
%>

將以上代碼復(fù)制生成GPS.asp文件,這樣無組件獲取圖片尺寸的通用類就OK了。



2.獲取圖片路徑

由于不只一張圖片,以及圖片需分類存放,我們在數(shù)據(jù)庫中設(shè)計了一個存放圖片相對路徑的字段ImgURL。我們把上傳的圖片都放在一個名為images的文件夾中(至于如何無組件上傳圖片心晴就不在多說了)?,F(xiàn)在我們先設(shè)計一個ShowImg.asp頁面用來顯示縮略圖及相關(guān)信息。具體設(shè)計如下:

圖片:

圖片格式:

圖片尺寸:

圖片大?。?

點擊次數(shù):


下面,我們獲取圖片的絕對路徑。代碼如下:


'/////獲取ShowImg.asp的絕對路徑/////
Dim curFile
curFile=Server.mappath(Request.servervariables("PATH_INFO"))

Dim curfilename,filename

'/////圖片相對路徑(存于數(shù)據(jù)庫中)
cufilename=rs("ImgURL")
'/////因為ShowImg.asp與images在同一目錄,所以我們用instrrev獲取images的路徑/////
filename=left(curFile,instrrev(curFile,"\"))cufilename

'/////建立GPS類實體/////
Dim GetPicSize
Set GetPicSize=new GPS

Set fs=Server.CreateObject("Scripting.FileSystemObject")
'/////獲取圖片類型/////
Dim PicSuffixName
PicSuffixName=fs.GetExtensionName(filename)

Dim PD '//Picture Dimension
Dim PWidth,PHeight

Select Case PicSuffixName
Case "gif","bmp","jpg","png":
'/////調(diào)用GPS通用類中的GetImageSize函數(shù)獲取圖片尺寸/////
PD=GetPicSize.GetImageSize(filename)
PWidth=PD(1) '//獲取圖片寬度
PHeight=PD(2) '//獲取圖片高度
Case "swf"
PD=GetPicSize.GetImageSize(filename)
PWidth=PD(1) '//獲取Flash寬度
PHeight=PD(2) '//獲取Flash高度
Case Else
End Select

Set fs=Nothing
Set GetPicSize=Nothing
%>



將上面的代碼復(fù)制到body>的上面就OK了!

當(dāng)然,有人會說,獲取路徑不一定要用PATH_INFO,直接用server.mappath()不就可以了嘛,呵呵,蘿卜青菜各有所愛,主要是我用PATH_INFO可以實現(xiàn)FSO的一些功能而用server.mappath()沒有搞定,所以一直使用這個。

3.定義縮略圖尺寸
這部分代碼就是仁者見仁,智者見智了。首先,我們需要規(guī)定縮略圖顯示尺寸范圍,譬如:300X260,代碼可以這樣寫:
%
Dim PXWidth,PXHeight
Dim Pp '//Proportion
If PWidth=0 Or PWidth="" Then
PXWidth=0
PXHeight=0
Else
Pp=FormatNumber(PWidth/PHeight,2) '//長寬比
End If
If PWidth>=PHeight Then
If PWidth>=300 Then
PXWidth=300
PXHeight=FormatNumber(300/Pp,0)
Else
PXWidth=PWidth
PXHeight=PHeight
End If
Else
If PHeight>=260 Then
PXHeight=260
PXWidth=FormatNumber(260*Pp,0)
Else
PXWidth=PWidth
PXHeight=PHeight
End If
End If
%>
將上面的代碼緊接第二步寫下即可。調(diào)用時代碼如下:
img src=%=curfilename%> border="0" width=%=PXWidth%> height=%=PXHeight%>>
至于圖片格式可以用%=PicSuffixName%>得到,圖片尺寸可以寫成

response.write PXWidth"X"PXHeight
%>
圖片大小可以用FSO.GetFileSize(filename)來實現(xiàn),而點擊次數(shù)可以簡單地用SQL語句實現(xiàn),具體編碼就不再累述了。

這樣,一個無組件生成縮略圖程序就寫好了,可能有點拿來主義,不過只要大家能將方法掌握相信還是有很大提高的。
您可能感興趣的文章:
  • asp無組件生成驗證碼 GIF圖片格式
  • Asp無組件生成縮略圖的代碼
  • Asp無組件上傳進(jìn)度條解決方案
  • 無組件上傳圖片之文件采用方案
  • 在無組件的情況下,如何上傳圖片?
  • 無組件實現(xiàn)文件上傳/下載
  • ASP無組件分頁實現(xiàn)思路及代碼

標(biāo)簽:廣元 江蘇 蚌埠 大理 萍鄉(xiāng) 棗莊 衡水 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《無組件生成縮略圖》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    同仁县| 潼南县| 肇东市| 巴彦淖尔市| 福清市| 铁岭市| 武鸣县| 阿图什市| 宜州市| 漠河县| 邹平县| 长寿区| 阳信县| 泗洪县| 东光县| 镇宁| 洛阳市| 安泽县| 仁寿县| 邳州市| 宿松县| 芜湖县| 金阳县| 漠河县| 阳春市| 罗定市| 蒙阴县| 大连市| 齐河县| 乐都县| 社会| 兰考县| 新丰县| 封丘县| 邳州市| 肇州县| 安岳县| 三河市| 桐梓县| 肇州县| 温宿县|