You need to return 'Content-type: image/jpeg' in your headers. Some clients will break with your Content-Length: 0 as well.
If you use CGI; it can help make alot of these tasks easier, but content-type is what you need to fix this problem. Text works fine because text/html is the browser's default.