Zikko's Resources


GetJPGInfo - retrieving type, width and height from a JPG image with ASP/VB

Getting the dimensions of a JPG is a pretty common task, especially when it comes to web. In PHP there's a handy build-in thingy for it (getimagesize), but if you're stuck with ASP/VBScript there isn't. Or maybe there was - somebody said something about it - only it was slow and unhandy. If the server at hand has some image manipulation component installed on it, that component is likely to include some way to retrieve the dimensions. But if it hasn't, or if you don't want your code to depend on it, the code below might be helpful.

Baseline or progressive

Also, there are two types of JPGs: baseline ones and progressive ones. Progressive are the ones that load gradually, starting with crappy quality and then improving a couple of times 'til it's good. Baseline is when this doesn't happen. Since the information about which type of JPG a file is is stored in the same place as the width and height, I made the code retrieve it as well. Oh yeah, why? Well one possible usage is when you want to prevent a Flash movie from trying to load certain JPG files, since Flash doesn't support the loading of progressive JPGs, at least not in some older versions.

Input

The function below takes two parameters. The first needs to be a FileSystemObject, the other is the filename of the JPG file to be analyzed. You'll probably need to use Server.MapPath on it first.

Output

The function returns a Dictionary object with three keys: "Width", "Height" and "Type".

Example

Set FS = CreateObject("Scripting.FileSystemObject")
Set Info = GetJPEGInfo( FS, Server.MapPath( "image.jpg" ) )
Response.Write "Width:" & Info("Width")
Response.Write "Height:" & Info("Height")
Response.Write "Type:" & Info("Type")

And here's the code

Function GetJPEGInfo( FS, FileName )

    Dim Stream
    Dim DimensionBytes
    Dim Width
    Dim Height
    Dim MarkerBytes
    Dim MarkerType
    Dim SizeBytes
    Dim Size
    
    Set Stream = FS.OpenTextFile( FileName, 1 )
    
    Stream.Skip( 2 )
    
    Set GetJPEGInfo = CreateObject( "Scripting.Dictionary" )
    
    Do While Not Stream.AtEndOfStream
    
        MarkerBytes = Stream.Read( 2 )
        MarkerType = Asc( Right( MarkerBytes, 1 ) )
        
        SizeBytes = Stream.Read( 2 )
        Size = Asc( Left(SizeBytes,1) ) * 256 + Asc( Right(SizeBytes,1) )
        
        If MarkerType = 192 Or MarkerType = 194 Then
        
            If MarkerType = 192 Then
                GetJPEGInfo.Add "Type", "baseLine"
            Elseif MarkerType = 194 Then
                GetJPEGInfo.Add "Type", "progressive"
            End if
            
            Stream.Skip( 1 )
            
            DimensionBytes = Stream.Read( 4 )
                        
            Width = Asc( Mid( DimensionBytes, 3, 1 ) ) * 256 + Asc( Mid( DimensionBytes, 4, 1 ) )
            Height = Asc( Mid( DimensionBytes, 1, 1 ) ) * 256 + Asc( Mid( DimensionBytes, 2, 1 ) )
            
            GetJPEGInfo.Add "Width", Width
            GetJPEGInfo.Add "Height", Height
            
            Exit Do
        Else
            Stream.Skip( Size - 2 )
        End If

    Loop

    Stream.Close

End Function


All content on these pages may be used, copied and modifed freely. Questions or comments may be sent to . Also visit zikko.se.