You could use "fsockopen()", then "fgetc()" to read the bytes one at a time. For a gif image the width is stored in byte offset 6 and 7, and the height in bytes 8 and 9 (little-endian). For jpeg, even though there are several variations of the format, I think they all contain two-byte start-of-frame markers throughout the file, with the height in the 5th and 6th bytes after, and the width in the 7th and 8th bytes (big-endian).
Edit: code removed. I screwed it up before posting it. Besides, you want size, not dimensions, I guess. Sorry. Check out verran's user comment on the " fosockopen()" page.