Anyone ever seen this before?
I have a piece of code I use to get the dimensions of various kinds of image files (gif, jpeg, etc). For the gif file format, this is particularly simple: the width is stored in bytes 7-6, the height in bytes 9-8. The following code should extract the gif header:
path = "mygif.gif"
mappedPath = Server.MapPath(path)
Set fs = Server.CreateObject("Scripting.FileSystemObject"
If fs.FileExists(mappedPath) Then
set lStream = fs.OpenTextFile(mappedPath)
s = lStream.readAll() ' does not work. Use lstream.read(10) instead
width = asc(mid(s,8))*256+asc(mid(s,7))
height = asc(mid(s,10))*256+asc(mid(s,9))
end if
But it doesn't work!! For some reason, when a gif file has a zero byte in the width, which it always does when it's less than 256px wide, readAll() goes haywire. It returns a long string of null bytes after the width, the height comes back as 0, and none of the results can be trusted. By changing the readAll() to read(10) (I really only need the first 10 bytes), it works fine.
This really makes me worry about where else readAll() might fail. I've run these tests on several machines running Windows 2000 server with the latest service packs and everything else (IIS, etc) up-to-date. Try it yourself! Use readAll to dump the first 10 bytes from a small gif file (< 256 px wide). Compare the result to using read(10) to do the same thing. Your answers should be different. Isn't this kind of scary?
TIA for any thoughts you might have.
-Rob
I have a piece of code I use to get the dimensions of various kinds of image files (gif, jpeg, etc). For the gif file format, this is particularly simple: the width is stored in bytes 7-6, the height in bytes 9-8. The following code should extract the gif header:
path = "mygif.gif"
mappedPath = Server.MapPath(path)
Set fs = Server.CreateObject("Scripting.FileSystemObject"
If fs.FileExists(mappedPath) Then
set lStream = fs.OpenTextFile(mappedPath)
s = lStream.readAll() ' does not work. Use lstream.read(10) instead
width = asc(mid(s,8))*256+asc(mid(s,7))
height = asc(mid(s,10))*256+asc(mid(s,9))
end if
But it doesn't work!! For some reason, when a gif file has a zero byte in the width, which it always does when it's less than 256px wide, readAll() goes haywire. It returns a long string of null bytes after the width, the height comes back as 0, and none of the results can be trusted. By changing the readAll() to read(10) (I really only need the first 10 bytes), it works fine.
This really makes me worry about where else readAll() might fail. I've run these tests on several machines running Windows 2000 server with the latest service packs and everything else (IIS, etc) up-to-date. Try it yourself! Use readAll to dump the first 10 bytes from a small gif file (< 256 px wide). Compare the result to using read(10) to do the same thing. Your answers should be different. Isn't this kind of scary?
TIA for any thoughts you might have.
-Rob