Use ADODB. Stream to get the height and width of the image (bmp jpg png gif swf)

Source: Internet
Author: User
The basic principle is to use ADODB. Stream to read and parse binary files, and then return an array
The first element is of type (bmp jpg png gif swf)
The second element is width {width}
The third element is the height {height}
The fourth element is a string with width = {width} and Height = {height }.

  1. <%
  2. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''
  3. 'When uploading an image or displaying SWF, you want to get its height and width.
  4. '----------------------------------------------
  5. 'Basic principle: Read the binary file using ADODB. Stream and parse it. Then an array is returned.
  6. 'The first element is of type (bmp jpg png gif swf)
  7. 'The second element is the width {width}
  8. 'The third element is the height {height}
  9. 'The fourth element is a string with width = {width} and Height = {height }.
  10. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''
  11. Class qswhimg
  12. Dim Aso
  13. Private sub class_initialize
  14. Set Aso = Createobject ("ADODB. Stream ")
  15. Aso. mode = 3
  16. Aso. type = 1
  17. Aso. Open
  18. End sub
  19. Private sub class_terminate
  20. Set Aso = nothing
  21. End sub
  22. Private function bin2str (BIN)
  23. Dim I, STR
  24. For I = 1 to lenb (BIN)
  25. Clow = midb (bin, I, 1)
  26. If ASCB (Clow) <128 then
  27. STR = STR & CHR (ASCB (Clow ))
  28. Else
  29. I = I + 1
  30. If I <= lenb (BIN) then STR = STR & CHR (ASCW (midb (bin, I, 1) & Clow ))
  31. End if
  32. Next
  33. Bin2str = Str
  34. End Function
  35. Private function num2str (Num, base, lens)
  36. Dim RET
  37. Ret = ""
  38. While (Num> = Base)
  39. Ret = (Num mod base) & RET
  40. Num = (num-num mod base)/base
  41. Wend
  42. Num2str = right (string (lens, "0") & num & ret, lens)
  43. End Function
  44. Private function str2num (STR, Base)
  45. Dim RET
  46. Ret = 0
  47. For I = 1 to Len (STR)
  48. Ret = RET * base + CINT (mid (STR, I, 1 ))
  49. Next
  50. Str2num = RET
  51. End Function
  52. Private function binval (BIN)
  53. Dim RET
  54. Ret = 0
  55. For I = lenb (BIN) to 1 step-1
  56. Ret = RET * 256 + ASCB (midb (bin, I, 1 ))
  57. Next
  58. Binval = RET
  59. End Function
  60. Private function binval2 (BIN)
  61. Dim RET
  62. Ret = 0
  63. For I = 1 to lenb (BIN)
  64. Ret = RET * 256 + ASCB (midb (bin, I, 1 ))
  65. Next
  66. Binval2 = RET
  67. End Function
  68. Function getimagesize (filespec)
  69. Dim RET (3)
  70. Aso. loadfromfile (filespec)
  71. Bflag = ASO. Read (3)
  72. Select case hex (binval (bflag ))
  73. Case "4e5089 ":
  74. Aso. Read (15)
  75. RET (0) = "PNG"
  76. RET (1) = binval2 (ASO. Read (2 ))
  77. Aso. Read (2)
  78. RET (2) = binval2 (ASO. Read (2 ))
  79. Case "464947 ":
  80. Aso. Read (3)
  81. RET (0) = "GIF"
  82. RET (1) = binval (ASO. Read (2 ))
  83. RET (2) = binval (ASO. Read (2 ))
  84. Case "535746 ":
  85. Aso. Read (5)
  86. Bindata = ASO. Read (1)
  87. Sconv = num2str (ASCB (bindata), 2, 8)
  88. Nbits = str2num (left (sconv, 5), 2)
  89. Sconv = mid (sconv, 6)
  90. While (LEN (sconv) <nbits * 4)
  91. Bindata = ASO. Read (1)
  92. Sconv = sconv & num2str (ASCB (bindata), 2, 8)
  93. Wend
  94. RET (0) = "SWF"
  95. RET (1) = int (ABS (str2num (mid (sconv, 1 * nbits + 1, nbits), 2)-str2num (mid (sconv, 0 * nbits + 1, nbits), 2)/20)
  96. RET (2) = int (ABS (str2num (mid (sconv, 3 * nbits + 1, nbits), 2)-str2num (mid (sconv, 2 * nbits + 1, nbits), 2)/20)
  97. Case "ffd8ff ":
  98. Do
  99. Do: p1 = binval (ASO. Read (1): loop while p1 = 255 and not ASO. Eos
  100. If P1> 191 and P1 <196 then exit do else ASO. Read (binval2 (ASO. Read (2)-2)
  101. Do: p1 = binval (ASO. Read (1): loop while P1 <255 and not ASO. Eos
  102. Loop while true
  103. Aso. Read (3)
  104. RET (0) = "jpg"
  105. RET (2) = binval2 (ASO. Read (2 ))
  106. RET (1) = binval2 (ASO. Read (2 ))
  107. Case else:
  108. If left (bin2str (bflag), 2) = "BM" then
  109. Aso. Read (15)
  110. RET (0) = "BMP"
  111. RET (1) = binval (ASO. Read (4 ))
  112. RET (2) = binval (ASO. Read (4 ))
  113. Else
  114. RET (0) = ""
  115. End if
  116. End select
  117. RET (3) = "width =" & RET (1) "" Height = "& RET (2 )""""
  118. Getimagesize = RET
  119. End Function
  120. End Class
  121. 'Example (read the width of all images in a directory ):
  122. Set qswh = new qswhimg
  123. Set FSO = Createobject ("scripting. FileSystemObject ")
  124. 'Set F = FSO. getfolder (server. mappath ("/update/pic_tem /"))
  125. 'Set fc = f. Files
  126. 'For each F1 in FC
  127. 'Ext = FSO. getextensionname (f1.path)
  128. 'Select case ext
  129. 'Case "GIF", "BMP", "jpg", "PNG ":
  130. 'Arr = qswh. getimagesize (f1.path)
  131. 'Response. write "<br>" & Arr (0) & "" & Arr (3) & ":" & f1.name & "width:" & Arr (1) & "height: "& Arr (2)
  132. 'Case "SWF"
  133. 'Arr = qswh. getimagesize (f1.path)
  134. 'Response. write "<br>" & Arr (0) & "" & Arr (3) & ":" & f1.name & "width:" & Arr (1) & "height: "& Arr (2)
  135. 'End select
  136. 'Next
  137. Response. Write ("<HR> ")
  138. Show_picwh = qswh. getimagesize (server. mappath ("/update/pic_tem/200852414295626.jpg "))
  139. Response. write ("image path:" & server. mappath ("/update/pic_tem/200852414295626.jpg")" <br> image format: "& show_picwh (0) &" <br> Image Width: "& show_picwh (1) & "<br> Image Height:" & show_picwh (2 ))
  140. Set fc = nothing
  141. Set F = nothing
  142. Set FSO = nothing
  143. Set qswh = nothing
  144. %>
  145. ========================================================== ==========================================
  146. '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
  147. 'Function name: return_qswhimg_str
  148. 'Function functions:
  149. 'Use the class qswhimg to scale down an image to one value in proportion.
  150. 'If the image size is smaller than the set value, the image will be taken as the original size, not processed
  151. 'Note:
  152. 'Url image address
  153. 'Wh image size range
  154. 'Return the image width =__ or height =__ string
  155. 'Call instance: border = "0">
  156. 'Date: 2008-06
  157. '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
  158. Function return_qswhimg_str (URL, Wh)
  159. On Error resume next
  160. If url = "" Or isnull (URL) or wh = "" Then exit function
  161. Set qswh = new qswhimg
  162. Set FSO = Createobject ("scripting. FileSystemObject ")
  163. Show_picwh = qswh. getimagesize (server. mappath ("/" & URL ))
  164. If CINT (show_picwh (1)> = CINT (show_picwh (2) and CINT (show_picwh (1)> wh then
  165. If show_picwh (1)> wh then show_picwh_str = "width ="
  166. Elseif CINT (show_picwh (1) <CINT (show_picwh (2) and CINT (show_picwh (2)> wh then
  167. If show_picwh (2)> wh then show_picwh_str = "Height ="
  168. Else
  169. Show_picwh_str = ""
  170. End if
  171. Set FSO = nothing
  172. Set qswh = nothing
  173. Return_qswhimg_str = show_picwh_str
  174. End Function

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.