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
Here is a generic set of functions I found ages ago for getting information on an image. I'll put the way I've been using it at the end.
I've been using it to generate a randomized XML file for an image library. The script only gets run when I finish uploading a new image to the library.
Here's the usage: (or at least how I used it)
As you can see, I'm initializing the variables for width, height, etc. and the function sets them as appropriate. I know it's not kosher to use global variables like that, but it works.
Performance isn't as bad as you would think. In this particular case I am filtering the image library to just JPGs, but that is due to a limitation in the image library the XML is for, not due to a limitation in the functions.
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.
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
I use this simple function to return width, height and file size (eg. 640x480 - 200KBytes):
works beautifully, hope it helps.