API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Calculate Height/Width Of Images
If you have a web site where users attach GIF/JPG files (like a company logo, for example) you can determine the height and width of the files with this bit of LotusScript code. Once you have the height and width, you can save that as part of the document with the attachment and then use that information when displaying the image. This will allow the browser to allocate the correct amount of space on the screen when generating the HTML, becuase the image always takes a bit longer to load than it takes the HTML to be rendered.

THIS CODE HAS BEEN CHANGED. It was discovered that JPG format wasn't as basic as the old version of the code made it out to be. After doing a lot of research on the internet, the code has been updated to (hopefully) handle JPG's correctly. It has passed several tests, but there are a few different standards for JPG's, so this may still need to "evolve" more in the future.

Function ImageSize(fileName As String) As Variant
   ' Given a source file name (path to the GIF or JPG on disk), return an array containing
   ' the width (1st element) and height (2nd element).
   Dim retVal As Variant
   Dim header As String
   Dim f As Integer
   Dim wHi As Variant
   Dim wLo As Variant
   Dim hHi As Variant
   Dim hLo As Variant
   Dim w As Integer ' width of image
   Dim h As Integer ' height of image
   Dim foundMarker As Integer
   
   Redim retVal(2) As Integer
   Redim retVal(Lbound(retVal)+1)     ' Size it so there's 2 entries
   retVal(Lbound(retVal)) = 0
   retVal(Ubound(retVal)) = 0
   f = Freefile()
   On Error Resume Next
   Open fileName For Input As #f
   On Error Goto 0
   If Err <> 0 Then
      ImageSize = retVal  ' File name incorrect - return zero for both the height and width
      Exit Function
   End If
   If Lcase(Right(fileName, 3)) = "gif" Then
      ' GIF's height and width stored in a fixed location
      header = Input(10, f)
      wHi = Mid(header, 8, 1)
      wLo = Mid(header, 7, 1)
      hHi = Mid(header, 10, 1)
      hLo = Mid(header, 9, 1)
      w = Asc(wHi) * 256 + Asc(wLo)
      h = Asc(hHi) * 256 + Asc(hLo)
   Elseif Lcase(Right(fileName, 3)) = "jpg" Then
      ' JPG's stored in a variable location. The code has been verified with JFIF
      ' file format (the most common format)
      On Error Goto EndOfFile     ' In case we run over the file for some reason
      header = Input(2, f)
      If header = Chr$(255) & Chr$(216) Then   ' Must start with hex FF D8
         foundMarker = False   ' Look for the marker that will contain the height and width
         While Not foundMarker
            header = Input(2, f)    ' Grab the next marker
            ' Look for the marker (in hex) FF C0, FF C1, FF C2, or FF C3
            If header = Chr$(255) & Chr$(192) Or header = Chr$(255) & Chr$(193) _
            Or header = Chr$(255) & Chr$(194) Or header = Chr$(255) & Chr$(195) Then
               ' Next two bytes are the length, then a single byte that can be ignored.
               header = Input(3, f)
               ' Next two bytes are the height of the image
               header = Input(2, f)
               hHi = Asc(Midbp(header, 1, 1))
               hLo = Asc(Midbp(header, 2, 1))
               h = hHi * 256 + hLo
               ' Next two bytes are the width of the image
               header = Input(2, f)
               wHi = Asc(Midbp(header, 1, 1))
               wLo = Asc(Midbp(header, 2, 1))
               w = wHi * 256 + wLo
               foundMarker = True     ' Exit the while loop
            Else   ' It's not one of the special markers - skip over it
               header = Input(2, f)   ' Next two bytes are the marker length
               wHi = Asc(Midbp(header, 1, 1))
               wLo = Asc(Midbp(header, 2, 1))
               w = wHi * 256 + wLo
               header = Input(w-2, f) ' Skip over that many bytes (minus the 2 byte length already read)
               w = 0   ' Clear the variable
            End If
         Wend   ' Continue until the marker is found
      End If    ' Ends the check to see if the file starts with FF D8
EndOfFile:
      If Err <> 0 Then
         Err = 0
         Resume AfterError
      End If
   End If   ' Ends the check to see if the format is GIF or JPG
AfterError:
   retVal(Lbound(retVal)) = w
   retVal(Ubound(retVal)) = h
   Close #f
   ImageSize = retVal
End Function