Asp, php, and js get and control the image size
This article describes two ways to get the image size: php and asp. js, and js to get the image size and then use js to change the image size, the following is an example.
Php is quite simple. Just use the getimagesize function. We only need to process the obtained array.
<?
$ Arr = getimagesize ("images/album_01.gif ");
Echo $ arr [3];
$ Strarr = explode ("\" ", $ arr [3]);
Echo $ strarr [1];
?>
Let's look at the function of asp to get and scale the image size.
<%
Class imgwhinfo': Class used to obtain the 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
Response. Write (" <br> width:" & W & "; height:" & H)
%>
The following is an example of how to get the image size and control the size in js.
<HTML>
<HEAD>
<TITLE> scale down the demo image proportion </TITLE>
<Script>
Function Wa_SetImgAutoSize (img)
{
// Var img = document. all. img1; // Obtain the image
Var MaxWidth = 200; // sets the image width limit.
Var MaxHeight = 100; // sets the image height limit.
Var HeightWidth = img. offsetHeight/img. offsetWidth; // you can specify the aspect ratio.
Var WidthHeight = img. offsetWidth/img. offsetHeight; // Set the aspect ratio.
Alert ("test" + img. offsetHeight + img. fileSize );
If (img. offsetHeight> 1) alert (img. offsetHeight );
If (img. readyState! = "Complete "){
Return false; // make sure the image is fully loaded.
}
If (img. offsetWidth> MaxWidth ){
Img. width = MaxWidth;
Img. height = MaxWidth * HeightWidth;
}
If (img. offsetHeight> MaxHeight ){
Img. height = MaxHeight;
Img. width = MaxHeight * WidthHeight;
}
}
Function CheckImg (img)
{
Var message = "";
Var MaxWidth = 1; // sets the image width limit.
Var MaxHeight = 1; // sets the image height limit.
If (img. readyState! = "Complete "){
Return false; // make sure the image is fully loaded.
}
If (img. offsetHeight> MaxHeight) message + = "\ r height excess:" + img. offsetHeight;
If (img. offsetWidth> MaxWidth) message + = "\ r width exceeded:" + img. offsetWidth;
If (message! = "") Alert (message );
}
</Script>
</HEAD>
<BODY>
<Br>
<Input id = indium type = "file" onpropertychange = "img1.src = this. value;">
</BODY>
</HTML>