views:

282

answers:

1

Hi,

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

+1  A: 

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

Dustin Laine
LoadPicture docs: http://msdn.microsoft.com/en-us/library/66bd1sx9(VS.85).aspx Doesn't expressly mention PNG, but it may well support them.
T.J. Crowder
Good point, +1 thanks @T.J. Crowder
Dustin Laine
If it doesn't work with png's, I can use another file type. I shall test this and advise. Thanks guys!
Jim
I have seen some posts that show this with PNG's so I am hopeful.
Dustin Laine
Oh boy! Loadpicture doesn't work on 64bit platforms! Out of luck there.
Jim
I updated my answer with another alternative.
Dustin Laine
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
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
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.
Jim