Rev 64 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<%' This class has been extended to include support for the following:' BMP, GIF, JPG, PNG' AVI, MOV, MPG/MPEG' SWFClass clsImagePrivate mStrBinaryDataPrivate mLngWidthPrivate mLngHeightPrivate mStrTypePrivate mStrContentTypePrivate mLngSizePrivate mStrPathPrivate Sub Class_Initialize()mStrBinaryData = ChrB(0)mLngWidth = -1mLngHeight = -1mLngSize = -1mStrPath = "Undefined"mStrType = "Unknown"mStrContentType = "application/octet-stream"End SubPublic Sub Read(ByVal pStrFilePath)' ResetmStrBinaryData = ""mLngWidth = -1mLngHeight = -1mLngSize = -1mStrType = "Unknown"mStrContentType = "application/octet-stream"If InStr(1, pStrFilePath, ":\") = 0 ThenpStrFilePath = Server.MapPath(pStrFilePath)End IfmStrPath = pStrFilePathDim lObjFSODim lObjFileSet lObjFSO = Server.CreateObject("Scripting.FileSystemObject")If lObjFSO.FileExists(pStrFilePath) ThenSet lObjFile = lObjFSO.OpenTextFile(pStrFilePath)If Not lObjFile.AtEndOfStream ThenmStrBinaryData = ChrB(Asc(lObjFile.Read(1)))While Not lObjFile.AtEndOfStreammStrBinaryData = mStrBinaryData & ChrB(Asc(lObjFile.Read(1)))WendEnd IflObjFile.CloseCall ReadDimensions()End IfSet lObjFSO = NothingEnd SubPublic Property Let DataStream(ByRef pStrBinaryData)mStrPath = "DataStream"mStrBinaryData = pStrBinaryDataCall ReadDimensions()End PropertyPublic Property Get DataStream()DataStream = mStrBinaryDataEnd PropertyPublic Property Get Width()Width = mLngWidthEnd PropertyPublic Property Get Height()Height = mLngHeightEnd PropertyPublic Property Get ImageType()ImageType = mStrTypeEnd PropertyPublic Property Get ContentType()ContentType = mStrContentTypeEnd PropertyPublic Property Get Size()Size = mLngSizeEnd PropertyPublic Property Get Path()Path = mStrPathEnd PropertyPrivate Sub ReadDimensions()mLngWidth = -1mLngHeight = -1mLngSize = 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 GIFDim lBinJPG ' Signature of JPGDim lBinBMP ' Signature of BMPDim lBinPNG ' Signature of PNGDim lBinAVI ' Signature of AVIDim lBinSWF ' Signature of SWFDim lBinMOV ' Signature of MOVDim lBinMPG ' Signature of MPGlBinGIF = 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 FileIf InStrB(1, mStrBinaryData, lBinGIF) = 1 ThenmStrType = "GIF"mStrContentType = "image/gif"mLngWidth = CLng("&h" & HexAt(8) & HexAt(7))mLngHeight = CLng("&h" & HexAt(10) & HexAt(9))' JPEG fileElseIf InStrB(1, mStrBinaryData, lBinJPG) = 7 ThenDim lBinPrefixDim lLngStartmStrType = "JPG"mStrContentType = "image/jpeg"' Prefix found before image dimensionslBinPrefix = ChrB(&h00) & ChrB(&h11) & ChrB(&h08)' Find the last prefix (so we don't confuse it with data)lLngStart = 1DoIf InStrB(lLngStart, mStrBinaryData, lBinPrefix) + 3 = 3 Then Exit DolLngStart = InStrB(lLngStart, mStrBinaryData, lBinPrefix) + 3Loop' If a prefix was foundIf Not lLngStart = 1 ThenmLngWidth = CLng("&h" & HexAt(lLngStart+2) & HexAt(lLngStart+3))mLngHeight = CLng("&h" & HexAt(lLngStart) & HexAt(lLngStart+1))End If' Bitmap FileElseIf InStrB(1, mStrBinaryData, lBinBMP) = 1 ThenmStrType = "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 FileElseIf InStrB(1, mStrBinaryData, lBinPNG) = 1 ThenmStrType = "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 FileElseIf InStrB(1, mStrBinaryData, lBinAVI) = 1 ThenDim lBinAVIH, bpAVIHlBinAVIH = ChrB(Asc("a")) & ChrB(Asc("v")) & ChrB(Asc("i")) & ChrB(Asc("h"))bpAVIH = InStrB(1, mStrBinaryData, lBinAVIH)If bpAVIH > 1 ThenbpAVIH = bpAVIH + 40mStrType = "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 FileElseIf InStrB(1, mStrBinaryData, lBinSWF) = 1 ThenmStrType = "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 lBinSWFNBitsDim lBinSWFXMinDim lBinSWFXMaxDim lBinSWFYMinDim lBinSWFYMaxDim lBinSWFTBytesDim 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 ThenlBinSWFTBytes = lBinSWFTBytes + 1End If' Determine number of bytes needed to total to the bitslBinSWFTBytes = fix(((lBinSWFNBits * 4) + 5) / 8)If (((lBinSWFNBits * 4) + 5) Mod 8) > 0 ThenlBinSWFTBytes = lBinSWFTBytes + 1End If' Read in all the bits needed.lBinSWFVal = MidB(mStrBinaryData, 9, lBinSWFTBytes)' Determine Y-MaximumlBinSWFVal = 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-MinimumlBinSWFVal = 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-MaximumlBinSWFVal = 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-MinimumlBinSWFVal = 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 pixelsmLngWidth = ((lBinSWFXMax - lBinSWFXMin) + 1) \ 20mLngHeight = ((lBinSWFYMax - lBinSWFYMin) + 1) \ 20' MPEG FileElseIf InStrB(1, mStrBinaryData, lBinMPG) > 0 ThenmStrType = "MPG"mStrContentType = "video/mpeg"Dim lBinMPGPosDim lBinMPGVallBinMPGPos = 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 FileElseIf InStrB(1, mStrBinaryData, lBinMOV) > 0 ThenmStrType = "MOV"mStrContentType = "video/quicktime"Dim lBinMOVPoslBinMOVPos = 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 SubPrivate Function HexAt(ByRef pLngPosition)If pLngPosition > LenB(mStrBinaryData) Or pLngPosition <= 0 Then Exit FunctionHexAt = 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) ThenReverseB = NullExit FunctionEnd IfFor iCur = 1 To iLeniRes = iRes & MidB(sValue, iLen - iCur + 1, 1)NextReverseB = iResEnd FunctionPrivate Function ATOI(sValue)Dim iCur, iLen, iVal, iRes : iRes = 0iLen = LenB(sValue)If (iLen > 4) Or (iLen < 1) ThenATOI = NullExit FunctionEnd IfFor iCur = 1 To iLeniVal = CLng(AscB(MidB(sValue, iLen - iCur + 1, 1)))If iCur > 1 TheniVal = iVal * (256 ^ (iCur - 1))End IfiRes = iRes + iValNextATOI = iResEnd FunctionPrivate Function LShift(sValue, iBits)Dim i__BYTE : i__BYTE = 8Dim sResult, sHold, iPartialDim iLen, iCur, sByte, iByte' Do nothing if no bit shift requested, or perform LShift.If iBits = 0 ThenLShift = sValueExit FunctionElseIf iBits < 0 ThenLShift = RShift(sValue, Abs(iBits))Exit FunctionElseIf LenB(sValue) < Fix(iBits / i__BYTE) ThenLShift = sValueExit FunctionEnd If' Add whole bytesiLen = Fix(iBits / i__BYTE)sResult = sValueIf iLen > 0 ThenFor iCur = 1 To iLensResult = sResult & ChrB(0)NextEnd IfiPartial = iBits Mod i__BYTEIf iPartial = 0 ThenLShift = sResultExit FunctionEnd IfsHold = sResultsResult = ""' Byte by Byte, shift remaining bits.iLen = LenB(sHold)For iCur = 1 To iLenIf iCur < iLen ThensByte = MidB(sHold, iCur, 2)iByte = (AscB(MidB(sByte, 1, 1)) * 256) + AscB(MidB(sByte, 2, 1))ElsesByte = MidB(sHold, iCur, 1)iByte = (AscB(sByte) * 256)End If' Perform the shiftiByte = Fix(CLng(iByte) * (2 ^ iPartial))' Convert back to stringIf iCur = 1 Then' 2 Left Most BytessByte = 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 BytesByte = 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 IfNextLShift = sResultEnd FunctionPrivate Function RShift(sValue, iBits)Dim i__BYTE : i__BYTE = 8Dim sResult, sHold, iPartialDim iLen, iCur, sByte, iByte' Do nothing if no bit shift requested, or perform LShift.If iBits = 0 ThenRShift = sValueExit FunctionElseIf iBits < 0 ThenRShift = LShift(sValue, Abs(iBits))Exit FunctionElseIf LenB(sValue) < Fix(iBits / i__BYTE) ThenRShift = sValueExit FunctionEnd If' Remove whole bytesIf Fix(iBits / i__BYTE) > 0 ThensResult = MidB(sValue, 1, LenB(sValue) - Fix(iBits / i__BYTE))ElsesResult = sValueEnd IfiPartial = iBits Mod i__BYTEIf iPartial = 0 ThenRShift = sResultExit FunctionEnd IfsHold = sResultsResult = ""' Byte by Byte, shift remaining bits.iLen = LenB(sHold)For iCur = iLen To 1 Step -1If 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))ElsesByte = MidB(sHold, iCur, 1)iByte = AscB(sByte)End If' Perform the shiftiByte = Fix(CLng(iByte) * 2 ^ (-1 * iPartial))' Convert back to stringsByte = ChrB(CLng("&h" & Right(("00" & Hex(iByte)), 2)))sResult = sByte & sResultNext' Finally, readd empty bytes as necessaryiLen = Fix(iBits / i__BYTE)If iLen > 0 ThenFor iCur = 1 To iLensResult = ChrB(0) & sResultNextEnd IfRShift = sResultEnd FunctionPrivate Function ToBinary(sVal)Dim iLen, iCur, iByte, iVal, iB, OUT, OUTHiLen = LenB(sVal)If iLen = 0 ThenToBinary = ""Exit FunctionEnd IfFor iCur = 1 To iLeniByte = MidB(sVal, iCur, 1)iVal = AscB(iByte)OUTH = OUTH & Right("0" & Hex(iVal), 2)For iB = 7 To 1 Step -1If iVal >= (2 ^ iB) ThenOUT = OUT & "1"iVal = iVal - (2 ^ iB)ElseOUT = OUT & "0"End IfNextIf iVal > 0 ThenOUT = OUT & "1"ElseOUT = OUT & "0"End IfOUT = OUT & "."NextToBinary = OUTH & " " & OUTEnd FunctionEnd Class%>