一个纯FSO写的类:获取图片width,height

本文介绍了一种利用文件系统对象(FSO)获取不同格式图片(如BMP、GIF、JPG和PNG)尺寸的方法。通过VBScript实现,详细展示了如何读取文件头信息并解析宽度和高度。
<
imgpath
="080124/images/back.jpg"

set pp=new imginfo 
= pp.imgw(server.mappath(imgpath)) 
= pp.imgh(server.mappath(imgpath)) 
set pp=nothing 

response.write 
"<img src='http://shop.feng.com/"&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
%
>


一个纯FSO写的示例
<
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
'
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

''::: BMP, GIF, JPG and PNG ::: 
'
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
'
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
'
'::: ::: 
'
'::: This function gets a specified number of bytes from any ::: 
'
'::: file, starting at the offset (base 1) ::: 
'
'::: ::: 
'
'::: Passed: ::: 
'
'::: flnm => Filespec of file to read ::: 
'
'::: offset => Offset at which to start reading ::: 
'
'::: bytes => How many bytes to read ::: 
'
'::: ::: 
'
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
function GetBytes(flnm, offset, bytes) 
Dim objFSO 
Dim objFTemp 
Dim objTextStream 
Dim lngSize 
on error resume next 
Set objFSO = CreateObject("Scripting.FileSystemObject"

'' First, we get the filesize 
Set objFTemp = objFSO.GetFile(flnm) 
lngSize 
= objFTemp.Size 
set objFTemp = nothing 
fsoForReading 
= 1 
Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading) 
if offset > 0 then 
strBuff 
= objTextStream.Read(offset - 1
end if 
if bytes = -1 then '' Get All! 
GetBytes = objTextStream.Read(lngSize) ''ReadAll 
else 
GetBytes 
= objTextStream.Read(bytes) 
end if 
objTextStream.Close 
set objTextStream = nothing 
set objFSO = nothing 
end function 
''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
'
'::: ::: 
'
'::: Functions to convert two bytes to a numeric value (long) ::: 
'
'::: (both little-endian and big-endian) ::: 
'
'::: ::: 
'
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
function lngConvert(strTemp) 
lngConvert 
= clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256))) 
end function 
function lngConvert2(strTemp) 
lngConvert2 
= clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256))) 
end function 

''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
'
'::: ::: 
'
'::: This function does most of the real work. It will attempt ::: 
'
'::: to read any file, regardless of the extension, and will ::: 
'
'::: identify if it is a graphical image. ::: 
'
'::: ::: 
'
'::: Passed: ::: 
'
'::: flnm => Filespec of file to read ::: 
'
'::: width => width of image ::: 
'
'::: height => height of image ::: 
'
'::: depth => color depth (in number of colors) ::: 
'
'::: strImageType=> type of image (e.g. GIF, BMP, etc.) ::: 
'
'::: ::: 
'
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
function gfxSpex(flnm, width, height, depth, strImageType) 
dim strPNG 
dim strGIF 
dim strBMP 
dim strType 
strType 
= "" 
strImageType 
= "(unknown)" 
gfxSpex 
= False 
strPNG 
= chr(137& chr(80& chr(78
strGIF 
= "GIF" 
strBMP 
= chr(66& chr(77
strType 
= GetBytes(flnm, 03
if strType = strGIF then '' is GIF 
strImageType = "GIF" 
Width 
= lngConvert(GetBytes(flnm, 72)) 
Height 
= lngConvert(GetBytes(flnm, 92)) 
Depth 
= 2 ^ ((asc(GetBytes(flnm, 111)) and 7+ 1
gfxSpex 
= True 
elseif left(strType, 2= strBMP then '' is BMP 
strImageType = "BMP" 
Width 
= lngConvert(GetBytes(flnm, 192)) 
Height 
= lngConvert(GetBytes(flnm, 232)) 
Depth 
= 2 ^ (asc(GetBytes(flnm, 291))) 
gfxSpex 
= True 
elseif strType = strPNG then '' Is PNG 
strImageType = "PNG" 
Width 
= lngConvert2(GetBytes(flnm, 192)) 
Height 
= lngConvert2(GetBytes(flnm, 232)) 
Depth 
= getBytes(flnm, 252
select case asc(right(Depth,1)) 
case 0 
Depth 
= 2 ^ (asc(left(Depth, 1))) 
gfxSpex 
= True 
case 2 
Depth 
= 2 ^ (asc(left(Depth, 1)) * 3
gfxSpex 
= True 
case 3 
Depth 
= 2 ^ (asc(left(Depth, 1))) ''8 
gfxSpex = True 
case 4 
Depth 
= 2 ^ (asc(left(Depth, 1)) * 2
gfxSpex 
= True 
case 6 
Depth 
= 2 ^ (asc(left(Depth, 1)) * 4
gfxSpex 
= True 
case else 
Depth 
= -1 
end select 

else 
strBuff 
= GetBytes(flnm, 0-1'' Get all bytes from file 
lngSize = len(strBuff) 
flgFound 
= 0 
strTarget 
= chr(255& chr(216& chr(255
flgFound 
= instr(strBuff, strTarget) 
if flgFound = 0 then 
exit function 
end if 
strImageType 
= "JPG" 
lngPos 
= flgFound + 2 
ExitLoop 
= false 
do while ExitLoop = False and lngPos < lngSize 

do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize 
lngPos 
= lngPos + 1 
loop 
if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then 
lngMarkerSize 
= lngConvert2(mid(strBuff, lngPos + 12)) 
lngPos 
= lngPos + lngMarkerSize + 1 
else 
ExitLoop 
= True 
end if 
loop 
'
if ExitLoop = False then 
Width 
= -1 
Height 
= -1 
Depth 
= -1 
else 
Height 
= lngConvert2(mid(strBuff, lngPos + 42)) 
Width 
= lngConvert2(mid(strBuff, lngPos + 62)) 
Depth 
= 2 ^ (asc(mid(strBuff, lngPos + 81)) * 8
gfxSpex 
= True 
end if 

end if 
end function 

''::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
'
'::: Test Harness ::: 
'
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

'' To test, we''ll just try to show all files with a .GIF extension in the root of C: 
Set objFSO = CreateObject("Scripting.FileSystemObject"
Set objF = objFSO.GetFolder("c:"
Set objFC = objF.Files 
response.write 
"<table border=""0"" cellpadding=""5"">" 
For Each f1 in objFC 
if instr(ucase(f1.Name), ".GIF"then 
response.write 
"<tr><td>" & f1.name & "</td><td>" & f1.DateCreated & "</td><td>" & f1.Size & "</td><td>" 
if gfxSpex(f1.Path, w, h, c, strType) = true then 
response.write w 
& " x " & h & " " & c & " colors" 
else 
response.write 
" " 
end if 
response.write 
"</td></tr>" 
end if 
Next 
response.write 
"</table>" 
set objFC = nothing 
set objF = nothing 
set objFSO = nothing 

%
> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值