读取图像尺寸的问题

shaojie 2006-08-11 04:32:45
服务器上有一批图片,现在想生成一个显示缩略图的网页,但不准备为每张图片另外做一张小图,所有原始图片都不是同一规则的,希望生成的缩略图为按比例缩小的,比如所有缩略图都显示在200×200的表格中,原先为800×600的图片显示为200×150,原先为600×800的图片显示为150×200,可否先将原始图片的尺寸读取出来,然后计算长和宽哪个值大就以哪个为基数定为200,另一边按比例计算大小,最后把两个值写入显示图片的长和宽位置上,此法是否可行?如何读取图片的尺寸?
...全文
250 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
eliphe 2006-12-22
  • 打赏
  • 举报
回复
也可以使用第三方的组件,如wfImage,地址是http://www.wfsoft.com/wf_wfImage.asp
afflatus80 2006-08-14
  • 打赏
  • 举报
回复
支持JPG,GIF,PNG,BMP
上面那个类你可以作为一个单独的文件,然而include到需要计算尺寸的页面.
下面是调用
<%
Set PP = New ImgWHInfo
W = PP.imgW(Server.Mappath(图片路径))
H = PP.imgH(Server.Mappath(图片路径))
if w=0 then
HEIGHT=0
WIDTH=0
else
HEIGHT=150*(H/W) '高度
WIDTH=200
end if
%>
W 计算出的就是该图片的宽度
H 计算出的就是该图片的高度

Server.Mappath是转为相对路径
提示出错的原因可能是你的路径搞错.

在html中
<img src='"&rs("pic")&"' border='0' width=<%=WIDTH%> height=<%=HEIGHT%>>
shaojie 2006-08-11
  • 打赏
  • 举报
回复
谢谢,数据库里存的只是图片的地址,如image/00001.jpg,显示图片时只是把地址提取出来,这该如何对其计算尺寸呢?
leohuang 2006-08-11
  • 打赏
  • 举报
回复
图片是LoadFromFile的,如下所示
ASO.LoadFromFile(filespec)

你应该先把图片从数据库里存出来,再用这个类缩放图片
shaojie 2006-08-11
  • 打赏
  • 举报
回复
谢谢楼上的,不过需要考虑文件格式吗?图片的地址是以数据库中的记录形式保存的,调用图片时的HTML是
<img src='"&rs("pic")&"' border='0' width=? height=?>

"pic"为数据中保存图片地址的字段,我把这个字段代入图片路径,却提示缺少对象,请问哪里错了?
afflatus80 2006-08-11
  • 打赏
  • 举报
回复
<%
Class ImgWHInfo '获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP
Dim ASO
Private Sub Class_Initialize
Set ASO=Server.CreateObject("ADODB.Stream")
ASO.Mode=3
ASO.Type=1
ASO.Open
End Sub
Private Sub Class_Terminate
Err.Clear
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)
DIM 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 "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=sConv&Num2Str(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 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
ret(3)="width=""" & ret(1) &""" height=""" & ret(2) &""""
getimagesize=ret
End Function

Public Function imgW(IMGPath)
Dim FSO,IMGFile,FileExt,Arr
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(IMGPath)) Then
Set IMGFile = FSO.GetFile(IMGPath)
FileExt=FSO.GetExtensionName(IMGPath)
Select Case FileExt
Case "gif","bmp","jpg","png":
Arr=GetImageSize(IMGFile.Path)
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 = server.CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(IMGPath)) Then
Set IMGFile = FSO.GetFile(IMGPath)
FileExt=FSO.GetExtensionName(IMGPath)
Select Case FileExt
Case "gif","bmp","jpg","png":
Arr=getImageSize(IMGFile.Path)
imgH = Arr(2)
End Select
Set IMGFile=Nothing
Else
imgH = 0
End If
Set FSO=Nothing
End Function
End Class


%>

下面是调用
<%



Set PP = New ImgWHInfo
W = PP.imgW(Server.Mappath(图片路径))
H = PP.imgH(Server.Mappath(图片路径))
if w=0 then
HEIGHT=0
WIDTH=0
else
HEIGHT=150*(H/W) '高度
WIDTH=200
end if

%>

<image src="图片路径" width=<%=width%> height=<%=height%>>



btbtd 2006-08-11
  • 打赏
  • 举报
回复
我想你用那个函数, 应该是按比例缩小. 只要调整一两个参数就OK.
kendo7 2006-08-11
  • 打赏
  • 举报
回复
使用组件吧
shaojie 2006-08-11
  • 打赏
  • 举报
回复
谢谢,不过你好像没明白我的意思,我的图片有的长大于宽,有的宽大于长,要求全部按比例缩小。
btbtd 2006-08-11
  • 打赏
  • 举报
回复
//全局控制页面图片大小, 只需在 <body> 中加上 onload="ppshow()", 如 <body onload="ppshow()">.
function ppshow() {
for(i=0; i<document.images.length;i++){
if(document.images[i].width>500) {
document.images[i].width=500
} else {
document.images[i].width=document.images[i].width
}
}
}//shawl.qiu

该函数作用于页面全部图片, 如要在具体ID下起作用,适当修改一下,

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧