<%Url=Trim(RequestQueryString("Url"))
IF Url="" Then
ResponseWrite"不存在"
ResponseEnd
End IF
Class ImgWHInfo '获取宽度和高度的类,支持JPG,GIF,PNG,BMP
Dim ASO
Private Sub Class_Initialize
Set ASO=ServerCreateObject("ADODBStream")
ASOMode=3
ASOType=1
ASOOpen
End Sub
Private Sub Class_Terminate
ErrClear
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)
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)
Dim Ret,I
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)
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
Private Function GetImageSize(filespec)
Dim bFlag
Dim Ret(3)
ASOLoadFromFile(filespec)
bFlag=ASORead(3)
Select Case Hex(binVal(bFlag))
Case "4E5089":
ASORead(15)
ret(0)="PNG"
ret(1)=BinVal2(ASORead(2))
ASORead(2)
ret(2)=BinVal2(ASORead(2))
Case "464947":
ASOread(3)
ret(0)="gif"
ret(1)=BinVal(ASORead(2))
ret(2)=BinVal(ASORead(2))
Case "535746":
ASOread(5)
binData=ASORead(1)
sConv=Num2Str(ascb(binData),2 ,8)
nBits=Str2Num(left(sConv,5),2)
sConv=mid(sConv,6)
While(len(sConv)<nBits4)
binData=ASORead(1)
sConv=sConv&Num2Str(AscB(binData),2 ,8)
Wend
ret(0)="SWF"
ret(1)=Int(Abs(Str2Num(Mid(sConv,1nBits+1,nBits),2)-Str2Num(Mid(sConv,0nBits+1,nBits),2))/20)
ret(2)=Int(Abs(Str2Num(Mid(sConv,3nBits+1,nBits),2)-Str2Num(Mid(sConv,2nBits+1,nBits),2))/20)
Case "FFD8FF":
Do
Do: p1=binVal(ASORead(1)): Loop While p1=255 And Not ASOEOS
If p1>191 And p1<196 Then Exit Do Else ASOread(binval2(ASORead(2))-2)
Do:p1=binVal(ASORead(1)):Loop While p1<255 And Not ASOEOS
Loop While True
ASORead(3)
ret(0)="JPG"
ret(2)=binval2(ASORead(2))
ret(1)=binval2(ASORead(2))
Case Else:
If left(Bin2Str(bFlag),2)="BM" Then
ASORead(15)
ret(0)="BMP"
ret(1)=binval(ASORead(4))
ret(2)=binval(ASORead(4))
Else
ret(0)=""
End If
End Select
ret(3)="width=""" & ret(1) &""" height=""" & ret(2) &""""
getimagesize=ret
End Function
Public Function imgW(IMGPath)
Dim FSO,IMGFile,FileExt,Arr
Set FSO = ServerCreateObject("ScriptingFileSystemObject")
If (FSOFileExists(IMGPath)) Then
Set IMGFile = FSOGetFile(IMGPath)
FileExt=FSOGetExtensionName(IMGPath)
Select Case FileExt
Case "gif","bmp","jpg","png":
Arr=GetImageSize(IMGFilePath)
imgW = Arr(1)
End Select
Set IMGFile=Nothing
Else
imgW = 0
End If
Set FSO=Nothing
End Function
Public Function imgH(IMGPath)
Dim FSO,IMGFile,FileExt,Arr
Set FSO = serverCreateObject("ScriptingFileSystemObject")
If (FSOFileExists(IMGPath)) Then
Set IMGFile = FSOGetFile(IMGPath)
FileExt=FSOGetExtensionName(IMGPath)
Select Case FileExt
Case "gif","bmp","jpg","png":
Arr=getImageSize(IMGFilePath)
imgH = Arr(2)
End Select
Set IMGFile=Nothing
Else
imgH = 0
End If
Set FSO=Nothing
End Function
End Class
IMGPath=Url
Set PP = New ImgWHInfo
W = PPimgW(ServerMappath(IMGPath))
H = PPimgH(ServerMappath(IMGPath))
Set pp = Nothing
IF W>467 And H<468 Then
Writ="width=467"
Else
IF W<468 And H>467 Then
Writ="height=467"
Else
IF W>467 And H>467 Then
IF W>H Then
Writ="width=467"
Else
Writ="height=467"
End IF
End IF
End IF
IF W<10 And H<10 Then
IF W>h Then
Writ="heigh=10"
Else
IF (W="" Or W=0) And (H=0 Or H="") Then
LJ="False"
Else
Writ="width=10"
End IF
End IF
End IF
End IF
IF Lj="" Then
ResponseWrite"<img src="&Url&" "&Writ&">"
Else
ResponseWrite"不存在"
End IF
%>
asp可以无组件生成缩略图,请看:
网上有不少生成缩略图的ASP组件。若你的虚拟空间不支持注册新组件,可能会感觉自己的网站失色不少。心晴不才,结合网上资源写了个无组件生成缩略图程序,仅供参考。
还是先看看基础部分吧。首先,我们知道在页面中显示是如下代码:
<img src="picgif" border="0" width="300" height="260">
src是路径,border控制边缘宽度,width是的长度,height是的高度。缩略图的生成其实就是在原始尺寸上缩放。但一般为了尽量少失真,我们都会按比例缩放。于是,获取的长宽尺寸也就成了生成缩略图的重点。
下面便是编写无组件生成缩略图的步骤:
1无组件获取大小
以前刚学ASP不久时看到过一篇利用ASCII码实现无组件获取尺寸的文章。后来试了试,发现在获取jpg类尺寸时总是不能正确显示,在网上查了查,居然有不少网站转载这个程序,但没有一家指出此程序的缺陷,也就更谈不上解决缺陷的办法了。后来又google了一下,终于找到了一篇介绍利用ADODBstream获取尺寸的文章,按其介绍的方法,修改里面的代码试了试,效果真的还不错,现在将其拿出来与大家分享:
利用ADODBstream获取尺寸的通用类
<%
'//////////// GPS:Get Picture Size //////////////
'//////////////利用ADODBstream获取尺寸//////////////
'/////////Cited By Leon(心晴) 2005年8月11日//////////
Class GPS
Dim aso
Private Sub Class_Initialize
Set aso=CreateObject("AdodbStream")
asoMode=3
asoType=1
asoOpen
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
'///以下是调用代码///
Function getImageSize(filespec)
'GPS (2002-8-11)
dim ret(3)
asoLoadFromFile(filespec)
bFlag=asoread(3)
select case hex(binVal(bFlag))
case "4E5089":
asoread(15)
ret(0)="PNG"
ret(1)=BinVal2(asoread(2))
asoread(2)
ret(2)=BinVal2(asoread(2))
case "464947":
asoread(3)
ret(0)="GIF"
ret(1)=BinVal(asoread(2))
ret(2)=BinVal(asoread(2))
case "535746":
asoread(5)
binData=asoRead(1)
sConv=Num2Str(ascb(binData),2 ,8)
nBits=Str2Num(left(sConv,5),2)
sConv=mid(sConv,6)
while(len(sConv)<nBits4)
binData=asoRead(1)
sConv=sConv&Num2Str(ascb(binData),2 ,8)
wend
ret(0)="SWF"
ret(1)=int(abs(Str2Num(mid(sConv,1nBits+1,nBits),2)-Str2Num(mid(sConv,0nBits+1,nBits),2))/20)
ret(2)=int(abs(Str2Num(mid(sConv,3nBits+1,nBits),2)-Str2Num(mid(sConv,2nBits+1,nBits),2))/20)
case "FFD8FF":
do
do: p1=binVal(asoRead(1)): loop while p1=255 and not asoEOS
if p1>191 and p1<196 then exit do else asoread(binval2(asoRead(2))-2)
do:p1=binVal(asoRead(1)):loop while p1<255 and not asoEOS
loop while true
asoRead(3)
ret(0)="JPG"
ret(2)=binval2(asoRead(2))
ret(1)=binval2(asoRead(2))
case else:
if left(Bin2Str(bFlag),2)="BM" then
asoRead(15)
ret(0)="BMP"
ret(1)=binval(asoRead(4))
ret(2)=binval(asoRead(4))
else
ret(0)=""
end if
end select
ret(3)="width=""" & ret(1) &""" height="""
& ret(2) &""""
getimagesize=ret
End Function
End Class
%>
将以上代码复制生成GPSasp文件,这样无组件获取尺寸的通用类就OK了。
2获取路径
由于不只一张,以及需分类存放,我们在数据库中设计了一个存放相对路径的字段ImgURL。我们把上传的都放在一个名为images的文件夹中(至于如何无组件上传心晴就不在多说了)。现在我们先设计一个ShowImgasp页面用来显示缩略图及相关信息。具体设计如下:
:
格式:
尺寸:
大小:
点击次数:
下面,我们获取的绝对路径。代码如下:
<%
'/////获取ShowImgasp的绝对路径/////
Dim curFile
curFile=Servermappath(Requestservervariables("PATH_INFO"))
Dim curfilename,filename
'/////相对路径(存于数据库中)
cufilename=rs("ImgURL")
'/////因为ShowImgasp与images在同一目录,所以我们用instrrev获取images的路径/////
filename=left(curFile,instrrev(curFile,"\"))&cufilename
'/////建立GPS类实体/////
Dim GetPicSize
Set GetPicSize=new GPS
Set fs=ServerCreateObject("ScriptingFileSystemObject")
'/////获取类型/////
Dim PicSuffixName
PicSuffixName=fsGetExtensionName(filename)
Dim PD '//Picture Dimension
Dim PWidth,PHeight
Select Case PicSuffixName
Case "gif","bmp","jpg","png":
'/////调用GPS通用类中的GetImageSize函数获取尺寸/////
PD=GetPicSizeGetImageSize(filename)
PWidth=PD(1) '//获取宽度
PHeight=PD(2) '//获取高度
Case "swf"
PD=GetPicSizeGetImageSize(filename)
PWidth=PD(1) '//获取Flash宽度
PHeight=PD(2) '//获取Flash高度
Case Else
End Select
Set fs=Nothing
Set GetPicSize=Nothing
%>
将上面的代码复制到<body>的上面就OK了!
当然,有人会说,获取路径不一定要用PATH_INFO,直接用servermappath()不就可以了嘛,呵呵,萝卜青菜各有所爱,主要是我用PATH_INFO可以实现FSO的一些功能而用servermappath()没有搞定,所以一直使用这个。
3定义缩略图尺寸
这部分代码就是仁者见仁,智者见智了。首先,我们需要规定缩略图显示尺寸范围,譬如: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(260Pp,0)
Else
PXWidth=PWidth
PXHeight=PHeight
End If
End If
%>
将上面的代码紧接第二步写下即可。调用时代码如下:
<img src=<%=curfilename%> border="0" width=<%=PXWidth%>
height=<%=PXHeight%>>
至于格式可以用得到,尺寸可以写成
<%
responsewrite PXWidth&"X"&PXHeight
%>
大小可以用FSOGetFileSize(filename)来实现,而点击次数可以简单地用SQL语句实现,具体编码就不再累述了。
这样,一个无组件生成缩略图程序就写好了
以上就是关于ASP图片自动调整大小的问题全部的内容,包括:ASP图片自动调整大小的问题、求ASP类似组件、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)