-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsImage.asp
436 lines (390 loc) · 13 KB
/
clsImage.asp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<%
' This class has been extended to include support for the following:
' BMP, GIF, JPG, PNG
' AVI, MOV, MPG/MPEG
' SWF
Class clsImage
Private mStrBinaryData
Private mLngWidth
Private mLngHeight
Private mStrType
Private mStrContentType
Private mLngSize
Private mStrPath
Private Sub Class_Initialize()
mStrBinaryData = ChrB(0)
mLngWidth = -1
mLngHeight = -1
mLngSize = -1
mStrPath = "Undefined"
mStrType = "Unknown"
mStrContentType = "application/octet-stream"
End Sub
Public Sub Read(ByVal pStrFilePath)
' Reset
mStrBinaryData = ""
mLngWidth = -1
mLngHeight = -1
mLngSize = -1
mStrType = "Unknown"
mStrContentType = "application/octet-stream"
If InStr(1, pStrFilePath, ":\") = 0 Then
pStrFilePath = Server.MapPath(pStrFilePath)
End If
mStrPath = pStrFilePath
Dim lObjFSO
Dim lObjFile
Set lObjFSO = Server.CreateObject("Scripting.FileSystemObject")
If lObjFSO.FileExists(pStrFilePath) Then
Set lObjFile = lObjFSO.OpenTextFile(pStrFilePath)
If Not lObjFile.AtEndOfStream Then
mStrBinaryData = ChrB(Asc(lObjFile.Read(1)))
While Not lObjFile.AtEndOfStream
mStrBinaryData = mStrBinaryData & ChrB(Asc(lObjFile.Read(1)))
Wend
End If
lObjFile.Close
Call ReadDimensions()
End If
Set lObjFSO = Nothing
End Sub
Public Property Let DataStream(ByRef pStrBinaryData)
mStrPath = "DataStream"
mStrBinaryData = pStrBinaryData
Call ReadDimensions()
End Property
Public Property Get DataStream()
DataStream = mStrBinaryData
End Property
Public Property Get Width()
Width = mLngWidth
End Property
Public Property Get Height()
Height = mLngHeight
End Property
Public Property Get ImageType()
ImageType = mStrType
End Property
Public Property Get ContentType()
ContentType = mStrContentType
End Property
Public Property Get Size()
Size = mLngSize
End Property
Public Property Get Path()
Path = mStrPath
End Property
Private Sub ReadDimensions()
mLngWidth = -1
mLngHeight = -1
mLngSize = LenB(mStrBinaryData)
mStrType = "Unknown"
mStrContentType = "application/octet-stream"
' I refer to Ascii data as Binary data or "BIN" in this script.
Dim lBinGIF ' Signature of GIF
Dim lBinJPG ' Signature of JPG
Dim lBinBMP ' Signature of BMP
Dim lBinPNG ' Signature of PNG
Dim lBinAVI ' Signature of AVI
Dim lBinSWF ' Signature of SWF
Dim lBinMOV ' Signature of MOV
Dim lBinMPG ' Signature of MPG
lBinGIF = ChrB(Asc("G")) & ChrB(Asc("I")) & ChrB(Asc("F"))
lBinJPG = ChrB(Asc("J")) & ChrB(Asc("F")) & ChrB(Asc("I")) & ChrB(Asc("F"))
lBinBMP = ChrB(Asc("B")) & ChrB(Asc("M"))
lBinPNG = ChrB(&h89) & ChrB(Asc("P")) & ChrB(Asc("N")) & ChrB(Asc("G"))
lBinAVI = ChrB(Asc("R")) & ChrB(Asc("I")) & ChrB(Asc("F")) & ChrB(Asc("F"))
lBinSWF = ChrB(Asc("F")) & ChrB(Asc("W")) & ChrB(Asc("S"))
lBinMOV = ChrB(Asc("t")) & ChrB(Asc("k")) & ChrB(Asc("h")) & ChrB(Asc("d"))
lBinMPG = ChrB(0) & ChrB(0) & ChrB(1) & ChrB(179)
' GIF File
If InStrB(1, mStrBinaryData, lBinGIF) = 1 Then
mStrType = "GIF"
mStrContentType = "image/gif"
mLngWidth = CLng("&h" & HexAt(8) & HexAt(7))
mLngHeight = CLng("&h" & HexAt(10) & HexAt(9))
' JPEG file
ElseIf InStrB(1, mStrBinaryData, lBinJPG) = 7 Then
Dim lBinPrefix
Dim lLngStart
mStrType = "JPG"
mStrContentType = "image/jpeg"
' Prefix found before image dimensions
lBinPrefix = ChrB(&h00) & ChrB(&h11) & ChrB(&h08)
' Find the last prefix (so we don't confuse it with data)
lLngStart = 1
Do
If InStrB(lLngStart, mStrBinaryData, lBinPrefix) + 3 = 3 Then Exit Do
lLngStart = InStrB(lLngStart, mStrBinaryData, lBinPrefix) + 3
Loop
' If a prefix was found
If Not lLngStart = 1 Then
mLngWidth = CLng("&h" & HexAt(lLngStart+2) & HexAt(lLngStart+3))
mLngHeight = CLng("&h" & HexAt(lLngStart) & HexAt(lLngStart+1))
End If
' Bitmap File
ElseIf InStrB(1, mStrBinaryData, lBinBMP) = 1 Then
mStrType = "BMP"
mStrContentType = "image/bmp"
mLngWidth = CLng("&h" & HexAt(22) & HexAt(21) & HexAt(20) & HexAt(19))
mLngHeight = CLng("&h" & HexAt(26) & HexAt(25) & HexAt(24) & HexAt(23))
' PNG File
ElseIf InStrB(1, mStrBinaryData, lBinPNG) = 1 Then
mStrType = "PNG"
mStrContentType = "image/png"
mLngWidth = CLng("&h" & HexAt(17) & HexAt(18) & HexAt(19) & HexAt(20))
mLngHeight = CLng("&h" & HexAt(21) & HexAt(22) & HexAt(23) & HexAt(24))
' AVI File
ElseIf InStrB(1, mStrBinaryData, lBinAVI) = 1 Then
Dim lBinAVIH, bpAVIH
lBinAVIH = ChrB(Asc("a")) & ChrB(Asc("v")) & ChrB(Asc("i")) & ChrB(Asc("h"))
bpAVIH = InStrB(1, mStrBinaryData, lBinAVIH)
If bpAVIH > 1 Then
bpAVIH = bpAVIH + 40
mStrType = "AVI"
mStrContentType = "video/avi"
mLngWidth = CLng("&h" & HexAt(bpAVIH + 3) & HexAt(bpAVIH + 2) & HexAt(bpAVIH + 1) & HexAt(bpAVIH))
mLngHeight = CLng("&h" & HexAt(bpAVIH + 7) & HexAt(bpAVIH + 6) & HexAt(bpAVIH + 5) & HexAt(bpAVIH + 4))
End If
' Shockwave Flash File
ElseIf InStrB(1, mStrBinaryData, lBinSWF) = 1 Then
mStrType = "SWF"
mStrContentType = "application/x-shockwave-flash"
' Get FrameSize. Note: According to specification, NBits will
' always be 15. This parser assumes that X and Y minimums are
' always 0, or rather, b000000000000000, and that numbers are
' expressed in 20 twips/pixel. The FrameSize RECT utilizes 9
' bytes, starting at position 9.
' This segment has been coded to handle dynamic NBit values, and
' should technically handle the max size of 31 in the future.
Dim lBinSWFNBits
Dim lBinSWFXMin
Dim lBinSWFXMax
Dim lBinSWFYMin
Dim lBinSWFYMax
Dim lBinSWFTBytes
Dim lBinSWFVal
' Determine NBits size (should be 15)
lBinSWFNBits = AscB(RShift(ChrB(CLng("&h" & HexAt(9))), 3))
lBinSWFTBytes = ((5 + lBinSWFNBits) / 8)
If ((5 + lBinSWFNBits) Mod 8) > 0 Then
lBinSWFTBytes = lBinSWFTBytes + 1
End If
' Determine number of bytes needed to total to the bits
lBinSWFTBytes = fix(((lBinSWFNBits * 4) + 5) / 8)
If (((lBinSWFNBits * 4) + 5) Mod 8) > 0 Then
lBinSWFTBytes = lBinSWFTBytes + 1
End If
' Read in all the bits needed.
lBinSWFVal = MidB(mStrBinaryData, 9, lBinSWFTBytes)
' Determine Y-Maximum
lBinSWFVal = RShift(lBinSWFVal, (lBinSWFTBytes * 8) - ((lBinSWFNBits * 4) + 5))
lBinSWFYMax = ATOI(RShift(MidB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1), (LenB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1)) - 4) + 1, 4), 1)) And ((2 ^ lBinSWFNBits) - 1)
' Determine Y-Minimum
lBinSWFVal = RShift(lBinSWFVal, lBinSWFNBits)
lBinSWFYMin = ATOI(RShift(MidB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1), (LenB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1)) - 4) + 1, 4), 1)) And ((2 ^ lBinSWFNBits) - 1)
' Determine X-Maximum
lBinSWFVal = RShift(lBinSWFVal, lBinSWFNBits)
lBinSWFXMax = ATOI(RShift(MidB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1), (LenB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1)) - 4) + 1, 4), 1)) And ((2 ^ lBinSWFNBits) - 1)
' Determine X-Minimum
lBinSWFVal = RShift(lBinSWFVal, lBinSWFNBits)
lBinSWFXMin = ATOI(RShift(MidB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1), (LenB(LShift(MidB(lBinSWFVal, (LenB(lBinSWFVal) - 4) + 1, 4), 1)) - 4) + 1, 4), 1)) And ((2 ^ lBinSWFNBits) - 1)
' Now calculate the Width and Height in pixels
mLngWidth = ((lBinSWFXMax - lBinSWFXMin) + 1) \ 20
mLngHeight = ((lBinSWFYMax - lBinSWFYMin) + 1) \ 20
' MPEG File
ElseIf InStrB(1, mStrBinaryData, lBinMPG) > 0 Then
mStrType = "MPG"
mStrContentType = "video/mpeg"
Dim lBinMPGPos
Dim lBinMPGVal
lBinMPGPos = InStrB(1, mStrBinaryData, lBinMPG) + LenB(lBinMPG)
lBinMPGVal = MidB(mStrBinaryData, lBinMPGPos, 3)
mLngHeight = ATOI(lBinMPGVal) And ((2 ^ 12) - 1)
lBinMPGVal = RShift(lBinMPGVal, 12)
mLngWidth = ATOI(lBinMPGVal) And ((2 ^ 12) - 1)
' Quicktime Movie File
ElseIf InStrB(1, mStrBinaryData, lBinMOV) > 0 Then
mStrType = "MOV"
mStrContentType = "video/quicktime"
Dim lBinMOVPos
lBinMOVPos = InStrB(1, mStrBinaryData, lBinMov) + LenB(lBinMov)
mLngWidth = ATOI(ReverseB(MidB(mStrBinaryData, lBinMOVPos + 77, 4)))
mLngHeight = ATOI(ReverseB(MidB(mStrBinaryData, lBinMOVPos + 77 + 4, 4)))
End If
' Response.Write "<UL><LI>mStrType = " & mStrType & "<LI>mStrContentType = " & mStrContentType & "<LI>mLngWidth = " & mLngWidth & "<LI>mLngHeight = " & mLngHeight & "</UL>"
End Sub
Private Function HexAt(ByRef pLngPosition)
If pLngPosition > LenB(mStrBinaryData) Or pLngPosition <= 0 Then Exit Function
HexAt = Right("0" & Hex(AscB(MidB(mStrBinaryData, pLngPosition, 1))), 2)
End Function
' --------------------------- MOVE TO COMMON FUNCTIONS ----------------------------
Private Function ReverseB(sValue)
Dim iCur, iLen, iRes : iRes = ""
iLen = LenB(sValue)
If (iLen < 1) Then
ReverseB = Null
Exit Function
End If
For iCur = 1 To iLen
iRes = iRes & MidB(sValue, iLen - iCur + 1, 1)
Next
ReverseB = iRes
End Function
Private Function ATOI(sValue)
Dim iCur, iLen, iVal, iRes : iRes = 0
iLen = LenB(sValue)
If (iLen > 4) Or (iLen < 1) Then
ATOI = Null
Exit Function
End If
For iCur = 1 To iLen
iVal = CLng(AscB(MidB(sValue, iLen - iCur + 1, 1)))
If iCur > 1 Then
iVal = iVal * (256 ^ (iCur - 1))
End If
iRes = iRes + iVal
Next
ATOI = iRes
End Function
Private Function LShift(sValue, iBits)
Dim i__BYTE : i__BYTE = 8
Dim sResult, sHold, iPartial
Dim iLen, iCur, sByte, iByte
' Do nothing if no bit shift requested, or perform LShift.
If iBits = 0 Then
LShift = sValue
Exit Function
ElseIf iBits < 0 Then
LShift = RShift(sValue, Abs(iBits))
Exit Function
ElseIf LenB(sValue) < Fix(iBits / i__BYTE) Then
LShift = sValue
Exit Function
End If
' Add whole bytes
iLen = Fix(iBits / i__BYTE)
sResult = sValue
If iLen > 0 Then
For iCur = 1 To iLen
sResult = sResult & ChrB(0)
Next
End If
iPartial = iBits Mod i__BYTE
If iPartial = 0 Then
LShift = sResult
Exit Function
End If
sHold = sResult
sResult = ""
' Byte by Byte, shift remaining bits.
iLen = LenB(sHold)
For iCur = 1 To iLen
If iCur < iLen Then
sByte = MidB(sHold, iCur, 2)
iByte = (AscB(MidB(sByte, 1, 1)) * 256) + AscB(MidB(sByte, 2, 1))
Else
sByte = MidB(sHold, iCur, 1)
iByte = (AscB(sByte) * 256)
End If
' Perform the shift
iByte = Fix(CLng(iByte) * (2 ^ iPartial))
' Convert back to string
If iCur = 1 Then
' 2 Left Most Bytes
sByte = String(Len(Hex(iByte)) Mod 2, "0") & Hex(iByte) & String(6,"0")
sByte = Left(sByte, Len(sByte) - 2)
sResult = sResult & ChrB(CLng("&h" & String(6, "0") & Left(sByte, 2)))
sResult = sResult & ChrB(CLng("&h" & String(6, "0") & Mid(sByte, 3, 2)))
Else
' Middle Byte
sByte = Right(String(6, "0") & String(Len(Hex(iByte)) Mod 2, "0") & Hex(iByte), 6)
sResult = sResult & ChrB(CLng("&h" & String(6, "0") & Mid(sByte, 3, 2)))
End If
Next
LShift = sResult
End Function
Private Function RShift(sValue, iBits)
Dim i__BYTE : i__BYTE = 8
Dim sResult, sHold, iPartial
Dim iLen, iCur, sByte, iByte
' Do nothing if no bit shift requested, or perform LShift.
If iBits = 0 Then
RShift = sValue
Exit Function
ElseIf iBits < 0 Then
RShift = LShift(sValue, Abs(iBits))
Exit Function
ElseIf LenB(sValue) < Fix(iBits / i__BYTE) Then
RShift = sValue
Exit Function
End If
' Remove whole bytes
If Fix(iBits / i__BYTE) > 0 Then
sResult = MidB(sValue, 1, LenB(sValue) - Fix(iBits / i__BYTE))
Else
sResult = sValue
End If
iPartial = iBits Mod i__BYTE
If iPartial = 0 Then
RShift = sResult
Exit Function
End If
sHold = sResult
sResult = ""
' Byte by Byte, shift remaining bits.
iLen = LenB(sHold)
For iCur = iLen To 1 Step -1
If iCur > 1 Then
' Get this byte (with additional byte prefix)
sByte = MidB(sHold, iCur - 1, 2)
iByte = (AscB(MidB(sByte, 1, 1)) * 256) + AscB(MidB(sByte, 2, 1))
Else
sByte = MidB(sHold, iCur, 1)
iByte = AscB(sByte)
End If
' Perform the shift
iByte = Fix(CLng(iByte) * 2 ^ (-1 * iPartial))
' Convert back to string
sByte = ChrB(CLng("&h" & Right(("00" & Hex(iByte)), 2)))
sResult = sByte & sResult
Next
' Finally, readd empty bytes as necessary
iLen = Fix(iBits / i__BYTE)
If iLen > 0 Then
For iCur = 1 To iLen
sResult = ChrB(0) & sResult
Next
End If
RShift = sResult
End Function
Private Function ToBinary(sVal)
Dim iLen, iCur, iByte, iVal, iB, OUT, OUTH
iLen = LenB(sVal)
If iLen = 0 Then
ToBinary = ""
Exit Function
End If
For iCur = 1 To iLen
iByte = MidB(sVal, iCur, 1)
iVal = AscB(iByte)
OUTH = OUTH & Right("0" & Hex(iVal), 2)
For iB = 7 To 1 Step -1
If iVal >= (2 ^ iB) Then
OUT = OUT & "1"
iVal = iVal - (2 ^ iB)
Else
OUT = OUT & "0"
End If
Next
If iVal > 0 Then
OUT = OUT & "1"
Else
OUT = OUT & "0"
End If
OUT = OUT & "."
Next
ToBinary = OUTH & " " & OUT
End Function
End Class
%>