1

I need to read a graphics file and get the width/height in VBScript (ASP). I found a package called gfxSpex that seems to be what a lot of people use but the GIFs get the width right but not the height. PNGs don't work at all as the routine is looking for the type in 0-3 and that's %PN in the .png files.

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 strType = strGIf Then                ' is GIF
        strImageType = "GIF"
        Width = lngConvert(GetBytes(flnm, 7, 2))
        Height = lngConvert(GetBytes(flnm, 9, 2))
        Depth = 2 ^ ((Asc(GetBytes(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 = GetBytes(flnm, 0, -1)     ' Get all bytes from file
        lngSize = Len(strBuff)
        flgFound = 0

        strTarget = Chr(255) & Chr(216) & Chr(255)
        flgFound = InStr(strBuff, strTarget)

        If flgFound = 0 Then
            Exit Function
        End If

        strImageType = "JPG"
        lngPos = flgFound + 2
        ExitLoop = False

        Do While ExitLoop = False And lngPos < lngSize
            Do While Asc(Mid(strBuff, lngPos, 1)) = 255 And lngPos < lngSize
                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 Then
            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

Function GetBytes(flnm, offset, bytes)
    Dim objFSO
    Dim objFTemp
    Dim objTextStream
    Dim lngSize

    On Error Resume Next

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' First, we get the filesize
    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

    If bytes = -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

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

Is anyone using this gfxSpex function and have they modified it? Is there a better way to get the width and height?

5
  • Yep, that will work! Never knew about WIAimage!
    – Velocedge
    Aug 12, 2019 at 15:51
  • 1
    Looks like whatever comment you replied to is now deleted for some reason. If you found a solution, please post it as answer. Aug 13, 2019 at 11:07
  • Anyway, I used gfxSpex for years as part of my file upload (see here) and it worked just fine, for all image types as far as I remember. That was years ago though. Aug 13, 2019 at 11:10
  • Do you still have the gfsSpex code? Any different than what I posted?
    – Velocedge
    Aug 14, 2019 at 10:59
  • I have the code, but no classic ASP server. And too lazy to install one. :) Aug 14, 2019 at 11:26

2 Answers 2

4

Yes, I'm not sure why the comment was deleted! Basically, it gave me a link to https://learn.microsoft.com/en-us/windows-hardware/drivers/image/wia-image-processing-filter. It's a lot more than I needed but good to know it's there. I just used:

set oIMG = CreateObject("WIA.ImageFile")
oIMG.loadFile(path)
iHeight = oIMG.Height
iWidth = oIMG.Width
set oIMG = nothing

It worked for gif, jpg, and png's. I didn't even need to register it.

2
  • You may accept your own answer to your question. It'll help people know that you found the solution.
    – ejderuby
    Aug 14, 2019 at 15:26
  • is did not get this to work on .webp images
    – MeSo2
    Nov 27, 2021 at 17:56
0

If you have root access to your server I have a COM DLL that uses ExifTool to analyse file information and meta data for any file type and return the results as a JSON string.

Here's an example implementation:

Function file_information(ByVal full_path)

    Dim ExifTool : Set ExifTool = Server.CreateObject("ClassicASP.ExifTool")

        ' If the file isn't found it returns an error string: "Error: the file... couldn't be found"

        file_information = ExifTool.FileInfo(full_path)

    Set ExifTool = nothing

End Function 

Response.Write file_information(Server.MapPath("image.png"))

Output:

[  
   {  
      "SourceFile":"C:/inetpub/wwwroot/exiftool/image.png",
      "ExifToolVersion":11.57,
      "FileName":"image.png",
      "Directory":"C:/inetpub/wwwroot/exiftool",
      "FileSize":"4.5 MB",
      "FileModifyDate":"2019:08:14 15:34:18+01:00",
      "FileAccessDate":"2019:08:14 15:34:06+01:00",
      "FileCreateDate":"2019:08:14 15:34:17+01:00",
      "FilePermissions":"rw-rw-rw-",
      "FileType":"PNG",
      "FileTypeExtension":"png",
      "MIMEType":"image/png",
      "ImageWidth":1600,
      "ImageHeight":1354,
      "BitDepth":8,
      "ColorType":"RGB with Alpha",
      "Compression":"Deflate/Inflate",
      "Filter":"Adaptive",
      "Interlace":"Noninterlaced",
      "ImageSize":"1600x1354",
      "Megapixels":2.2
   }
]

The files and installation instructions are available at: https://github.com/as08/ClassicASP.ExifTool

2
  • Is it working on .webp files?
    – MeSo2
    Nov 27, 2021 at 18:10
  • @MeSo2 It works on any file type.
    – Adam
    Nov 30, 2021 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.