how to get image width with server-side vbscript? Asp classic

14,967

Solution 1

Here is a post I saw awhile ago, looks like it could possibly simplify things a bit. I have not tested, so let me know your results.

<%
dim iWidth, iheight
sub ImgDimension(img)
dim myImg, fs
Set fs= CreateObject("Scripting.FileSystemObject")
if not fs.fileExists(img) then exit sub
set myImg = loadpicture(img)
iWidth = round(myImg.width / 26.4583)
iheight = round(myImg.height / 26.4583)
set myImg = nothing
end sub

ImgDimension(Server.MapPath("server image file"))
%> 

See here for post: http://www.haneng.com/asp-forum/ASP---Get-Image-Size_12971.html

UPDATE: Seeing that this method will not work in 64bit. Here is a link to another alternative method: http://www.4guysfromrolla.com/webtech/050300-1.shtml

Solution 2

I use this simple function to return width, height and file size (eg. 640x480 - 200KBytes):

   Function ImgDim(img)
    Dim objFSO, objGF, objLP, imgWdt, imgHgt, imgSiz
    img = Server.MapPath("/pictures/"& img) 'path to picture
    Set objFSO= CreateObject("Scripting.FileSystemObject")
    If objFSO.fileExists(img) Then
     Set objGF = objFSO.GetFile(img)
      imgSiz = objGF.Size
     Set objGF = Nothing
     Set objLP = loadpicture(img)
      imgWdt = round(objLP.width / 26.4583)
      imgHgt = round(objLP.height / 26.4583)
     Set objLP = Nothing
     Set fs = Nothing
     ImgDim = imgWdt &"x"& imgHgt &" - "& imgSiz/1024 &"KBytes"
    End If
   End Function

works beautifully, hope it helps.

Share:
14,967

Related videos on Youtube

Jim
Author by

Jim

Updated on June 04, 2022

Comments

  • Jim
    Jim almost 2 years

    I have been trying for days to find a way to get the image width of .png files which reside on our server. I am trying to read the first 24 bytes of the file and parse out the width from bytes 17-20. I have found several routines on the web but have not been successful. Strangely enough, it seems I am getting the height from bytes 21-24 decoded from hex to decimal just fine. I have verified the file contents using a hex viewer and the file is good. Here is the main portion of the routine:

    Function ReadPNG(fichero)
    Dim fso, ts, s, HW, nbytes
        HW = Array("0", "0")
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.OpenTextFile(Server.MapPath("\forums\attachments/" & fichero), 1)
        s = Right(ts.Read(24), 8)
        HW(0) = HexToDec(HexAt(s,3) & HexAt(s,4))
        HW(1) = HexToDec(HexAt(s,7) & HexAt(s,8))
        ts.Close
        ReadPNG = HW
    End Function
    
    Function HexAt(s, n)
        HexAt = Hex(AscAt(s, n))
    End Function
    
    Function HexToDec(ByVal HexVal)
    
    Dim i, num, part
    num = 0
    For I = 1 to Len(HexVal)
        part = Mid(StrReverse(UCase(HexVal)), I, 1)
        If IsNumeric(part) Then
            num = num + (CInt(part) * 16 ^ (I - 1) )
        Else
            num = num + ( (Asc(part) - 55) * 16^(I - 1) )
        End If
    Next
    
    HexToDec = num
    
    End Function
    

    As an example, my file has hex "00 00 01 80" in the width bytes (decimal 384) and hex "00 00 01 32" in the heigth bytes (decimal 306)

    I am getting the heigth 306 but thee width is returning "0011" (decimal 17).

    I am totally stummped! I do not have to use this routine either.

    Thanks, Jim

    • T.J. Crowder
      T.J. Crowder about 14 years
      In your title, do you mean server-side VBScript?
    • T.J. Crowder
      T.J. Crowder about 14 years
      You clearly do mean server-side, I've fixed the title for you.
  • T.J. Crowder
    T.J. Crowder about 14 years
    LoadPicture docs: msdn.microsoft.com/en-us/library/66bd1sx9(VS.85).aspx Doesn't expressly mention PNG, but it may well support them.
  • Jim
    Jim about 14 years
    If it doesn't work with png's, I can use another file type. I shall test this and advise. Thanks guys!
  • Dustin Laine
    Dustin Laine about 14 years
    I have seen some posts that show this with PNG's so I am hopeful.
  • Jim
    Jim about 14 years
    Oh boy! Loadpicture doesn't work on 64bit platforms! Out of luck there.
  • Jim
    Jim about 14 years
    Durilai, Yes actually I have tried that logic. It was pointing to the wrong position in the file which I corrected, but that routine seems to return the problem I have described.
  • Jim
    Jim about 14 years
    The idea here is to limit the width displayed in the browser. Anything larger than 820 pixels should display at 820. Unfortunately the CSS Max-width does not work in IE, works fine in Firefox.
  • Jim
    Jim about 14 years
    Ahhh, I see the problem... I need to convert the first part of the file to an ascii string from the binary file. There is code from 4Guys to do this. I will try that.
  • frumbert
    frumbert about 10 years
    This doesn't work on any of my PNG files. They are valid, photoshop and preview (macos) both recognise them, as does the browser. I'm getting the width as a large number but the height is correct, e.g. 02386C87651E40209D6802DAD046F3FE.png => -15430, 300 The source article is here: 4guysfromrolla.com/webtech/050300-1.shtml.
  • Developer Webs
    Developer Webs almost 7 years
    @Jim Then run your vbscript using the 32 bit version of wscript (on that 64 bit machine). Example: c:\windows\syswow64\wscript c:\path\to\your.vbs
  • MeSo2
    MeSo2 over 2 years
    I have been using this, but loadpicture produces 800a004b Path/File_access_error in my Windows Logs.
  • MeSo2
    MeSo2 over 2 years
    I have been using this, but loadpicture produces 800a004b Path/File_access_error in my Windows Logs. Any way to prevent this?