I have organized some code commonly used by ASP beginners (2)

xiaoxiao2021-03-06  97

[Recommended] Use FSO to get BMP, JPG, PNG, GIF file information (size, wide, high)

<%

'::: BMP, GIF, JPG and PNG :::

':: this function gets a specified number of bytes from any :::

'::: File, Starting At the Offset (Base 1) ::::

'::: :::

'::: passed: :::

'::: flnm => filespec of file to read :::

'::: Offset => Offset at Which To Start Reading :::

'::: bytes => How much bytes to read :::

'::: :::

': :::::::::::::::::::::::::: ::::::::::::::::::

Function getBytes (flnm, offset, bytes)

DIM OBJFSO

DIM Objftemp

DIM ObjTextStream

Dim lngsize

ON Error ResMe next

Set objfso = creteObject ("scripting.filesystemobject")

'First, we get the filesis

Set objftemp = objfso.getfile (flnm)

LNGSIZE = Objftemp.size

Set objftemp = Nothing

FsoForReading = 1

Set objTextStream = objfso.opentextfile (flnm, fsoforreading)

IF offset> 0 THEN

Strbuff = ObjtextStream.read (Offset - 1)

END IF

iftes = -1 Then 'get all!

GetBytes = ObjtextStream.Read (LNGSIZE) 'Readall

Else

GetBytes = ObjTextStream.read (bytes)

END IF

ObjtextStream.close

Set objtextStream = Nothing

Set objfso = Nothing

END FUNCTION

': :::::::::::::::::::::::::: ::::::::::::::::::

'::: :::

'::: Functions to Convert Two bytes to a Numeric Value (long) :::

'::: (Both Little-endian and big-endian) :::

'::: :::

': :::::::::::::::::::::::::: ::::::::::::::::::

Function LNGCONVERT (STRTEMP)

LNGCONVERT = CLNG (ASC (LEFT (STRTEMP, 1)) ((ASC (Right (strTemp, 1)) * 256))))))

END FUNCTION

Function LNGCONVERT2 (STRTEMP)

LNGCONVERT2 = CLNG (ASC (Right (strTemp, 1)) ((ASC (LEFT (STRTEMP, 1)) * 256))) End Function

': :::::::::::::::::::::::::: ::::::::::::::::::

'::: :::

'::: this function does Most of the real work. It will attempt :::

'::: r r f, regardless of the extension, and will :::

'::: Identify if it is a graphical image. :::

'::: :::

'::: passed: :::

'::: flnm => filespec of file to read :::

'::: width => width of image :::

'::: height => Height of Image :::

'::: Depth => Color Depth (in Number of Colors) :::

'::: strimagetype => Type of Image (E.G. GIF, BMP, ETC.) :::

'::: :::

': :::::::::::::::::::::::::: ::::::::::::::::::

Function GFXSPEX (FLNM, Width, Height, Depth, StrimageType)

DIM STRPNG

DIM STRGIF

DIM STRBMP

DIM STRTYPE

Strtype = ""

StrimageType = "(unknown"

GFXSPEX = FALSE

Strpng = CHR (137) & chr (80) & chr (78)

Strgif = "gif"

StrBMP = CHR (66) & chr (77)

Strtype = getBytes (flnm, 0, 3)

IF strgif1 'is gif

StrimageType = "gif"

Width = LNGCONVERT (GetBytes (FLNM, 7, 2))

Height = LNGCONVERT (GetBytes (FLNM, 9, 2))

Depth = 2 ^ ((ASC (Gettes (FLNM, 11, 1)) and 7) 1)

GFXSPEX = TRUE

Elseif Left (Strtype, 2) = Strbmp Then 'Is BMP

StrimageType = "BMP"

Width = LNGCONVERT (GetBytes (FLNM, 19, 2))

Height = LNGCONVERT (GetBytes (FLNM, 23, 2))

Depth = 2 ^ (ASC (GetBytes (FLNM, 29, 1))))

GFXSPEX = TRUE

Elseif Strtype = STRPNG THEN 'IS PNG

StrimageType = "PNG"

Width = LNGCONVERT2 (GetBytes (FLNM, 19, 2)) HEIGHT = LNGCONVERT2 (GetBytes (FLNM, 23, 2))

Depth = GetBytes (flnm, 25, 2)

SELECT CASE ASC (Right (depth, 1))

Case 0

DEPTH = 2 ^ (ASC (Left (depth, 1))))

GFXSPEX = TRUE

Case 2

DEPTH = 2 ^ (ASC (Left (depth, 1)) * 3)

GFXSPEX = TRUE

Case 3

DEPTH = 2 ^ (ASC (Left (depth, 1))) '8

GFXSPEX = TRUE

Case 4

DEPTH = 2 ^ (ASC (Left (depth, 1)) * 2)

GFXSPEX = TRUE

Case 6

DEPTH = 2 ^ (ASC (Left (depth, 1)) * 4)

GFXSPEX = TRUE

Case Else

DEPTH = -1

End SELECT

Else

Strbuff = Gettes (flnm, 0, -1) 'get alltes from file

LNGSIZE = LEN (STRBUFF)

FLGFOUND = 0

STRTARGET = CHR (255) & chr (216) & chr (255)

FLGFOUND = INSTR (Strbuff, Strtarget)

IF flground = 0 THEN

EXIT FUNCTION

END IF

StrimageType = "jpg"

LNGPOS = flgfound 2

Exitloop = false

Do while exitLoop = false and lngpos

Do While ASC (MID (Strbuff, LNGPOS, 1) = 255 and lngpos

LNGPOS = LNGPOS 1

loop

IF ASC (MID (Strbuff, LNGPOS, 1)) <192 or ASC (MID (Strbuff, LNGPOS, 1)> 195 THEN

LNGMARKERSIZE = LNGCONVERT2 (MID (Strbuff, LNGPOS 1, 2))

LNGPOS = LNGPOS LNGMARKERSIZE 1

Else

ExitLoop = true

END IF

loop

'

IF exitloop = false kil

Width = -1

HEIGHT = -1

DEPTH = -1

Else

HEIGHT = LNGCONVERT2 (MID (Strbuff, LNGPOS 4, 2))

Width = LNGCONVERT2 (MID (StrBuff, LNGPOS 6, 2))

Depth = 2 ^ (ASC (MID (Strbuff, LNGPOS 8, 1)) * 8)

GFXSPEX = TRUE

END IF

END IF

END FUNCTION

': :::::::::::::::::::::::::: ::::::::::::::::::

'::: Test Harness :::

': :::::::::::::::::::::::::: :::::::::::: test, we'll Just try to show all files with a .gif extension in the root of c:

Set objfso = creteObject ("scripting.filesystemobject")

Set objf = objfso.getfolder ("c: /")

Set objfc = Objf.files

Response.write "

"

For Each F1 in Objfc

IF INSTR (ucase (f1.name), ".gif") THEN

Response.write "

" & f1.name & " " & f1.datecreated & " " & f1.size & " < TD> "

IF GFXSPEX (f1.path, w, h, c, startpe) = true kil

Response.write W & "X" & H & "& C &" Colors "

Else

Response.write ""

END IF

Response.write " "

END IF

NEXT

Response.write ""

Set objfc = Nothing

Set objf = Nothing

Set objfso = Nothing

%>

24. Click to return to the previous page code: