Class that obtains the image width and height. jpg, GIF, PNG, and BMP are supported.

Source: Internet
Author: User

<%
Class imgwhinfo '': class for retrieving image width and height. jpg, GIF, PNG, and BMP are supported.
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)
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
%>
<%
imgpath = "test.jpg"

set pp = new imgwhinfo
W = pp. imgw (server. mappath (imgpath)
H = pp. imgh (server. mappath (imgpath)
set pp = nothing
%>
<%< br> dim W, pxheight
dim PP '// proportion
If W = 0 or W = "" Then
pxwidth = 0
pxheight = 0
else
PP = formatnumber (W/H, 2) '// aspect ratio
end if
If W> = h then
If W> = 300 then
pxwidth = 300
pxheight = formatnumber (300/PP, 0)
else
pxwidth = W
pxheight = H
end if
else
If pheight> = 260 then
pxheight = 260
pxwidth = formatnumber (260 * PP, 0)
else
pxwidth = W
pxheight = H
end if
response. write (pxwidth)
%>

Height = <% = pxheight %>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.