标题:请问如何从数据库中读出图片路径后判断图片尺寸大小…………?
只看楼主
在编程中沦落
Rank: 2
等 级:新手上路
威 望:4
帖 子:411
专家分:0
注 册:2005-12-25
 问题点数:0 回复次数:3 
请问如何从数据库中读出图片路径后判断图片尺寸大小…………?
数据库中读取图片路径(<%=rs("foto")%>)后,请问如何判断图片的尺寸(Width*Height)…………?

因为我想通过图片尺寸而适当缩小图片显示大小,以免撑开页面布局…………

比如 当width大于500px时候就设置其宽度为500px,请问这样该怎么写,怎么判断这个条件…………?

哪位朋友有相关的代码或相应程序,能共享一下吗…………?谢谢…………

[此贴子已经被作者于2007-5-9 10:18:51编辑过]

搜索更多相关主题的帖子: 数据库 路径 尺寸 判断 
2007-05-09 10:18
海仅拥虾
Rank: 1
等 级:新手上路
威 望:1
帖 子:23
专家分:0
注 册:2007-5-9
得分:0 

<%
imgpath="default_22.gif"

set pp=new imgInfo
w = pp.imgW(server.mappath(imgpath))
h = pp.imgH(server.mappath(imgpath))
set pp=nothing

response.write "<img src='"&imgpath&"' border=0><br>宽:"&w&";高:"&h

Class imgInfo
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
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
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
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
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 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=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(pic_path)
Set fso1 = server.CreateObject("Scripting.FileSystemObject")
If (fso1.FileExists(pic_path)) Then
Set f1 = fso1.GetFile(pic_path)
ext=fso1.GetExtensionName(pic_path)
select case ext
case "gif","bmp","jpg","png":
arr=getImageSize(f1.path)
imgW = arr(1)
end select
Set f1=nothing
else
imgW = 0
End if
Set fso1=nothing
End Function

Public Function imgH(pic_path)
Set fso1 = server.CreateObject("Scripting.FileSystemObject")
If (fso1.FileExists(pic_path)) Then
Set f1 = fso1.GetFile(pic_path)
ext=fso1.GetExtensionName(pic_path)
select case ext
case "gif","bmp","jpg","png":
arr=getImageSize(f1.path)
imgH = arr(2)
end select
Set f1=nothing
else
imgH = 0
End if
Set fso1=nothing
End Function
End Class
%>


海鸿源码网※→Www.←※ 海鸿ASP群35428659
2007-05-09 12:09
lq7350684
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:5089
专家分:98
注 册:2006-11-6
得分:0 

你可以不用管数据库输出的图片有多大.
你可以直接把图片设成是500px的啊.
不知道这样能不能实现你要的效果.
[CODE]<img id="n" src="http://bbs.bc-cn.net/Images/userface/image1.jpg" onload="javascript:if(this.style.width>500);this.style.width=500;" />[/CODE]

[此贴子已经被作者于2007-5-9 14:21:53编辑过]

2007-05-09 14:21
在编程中沦落
Rank: 2
等 级:新手上路
威 望:4
帖 子:411
专家分:0
注 册:2005-12-25
得分:0 
回复:(lq7350684)你可以不用管数据库输出的图片有多...
To:lq7350684

我就是想这样,不过你那个在图片尺寸小于这个数的时候也更改尺寸为500PX了,我试了一下,这样应该行,不过好像经常会失效的…………


<img src="http://zi.csdn.net/net_2.jpg"onload="if(this.width>500){this.width=500}">

TO:海仅拥虾

谢谢你的回复,不过好像太复杂了,看得我头晕…………

[此贴子已经被作者于2007-5-10 10:20:10编辑过]


2007-05-10 10:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-137845-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014015 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved