Question re: graphics output of CGIs (mime-type issue?)

What’s the proper way to deliver a grpahical
file via HTTP?

For example, supposing a web page has a bit of
HTML like:

Then how should myprog.cgi deliver its output?
Is there direct support for this in one of the
libraries, or do you just have to know how to
do it?

I tried just using a Content-type of image/jpeg,
but I got garbage in the browser.

Thanks,
Hal

···


Hal Fulton
hal9000@hypermetrics.com

I tried just using a Content-type of image/jpeg,
but I got garbage in the browser.

That’s the way to do it, but make sure your code doesn’t output
/anything/ textual at all.

When in doubt, view-source, or save to disk in the browser and open in a
text editor. You’ll see if there’s anything obvious.

Ari

What’s the proper way to deliver a grpahical
file via HTTP?

For example, supposing a web page has a bit of
HTML like:

Then how should myprog.cgi deliver its output?
Is there direct support for this in one of the
libraries, or do you just have to know how to
do it?

I tried just using a Content-type of image/jpeg,
but I got garbage in the browser.

Don’t some browsers insist on looking at filetype
rather than mime-type?

Two workarounds spring to mind (I’ve installed Ruby-GD for this
very reason, but haven’t tried yet):

Would this work?
#(Apache,mod_ruby)

<Directory /docroot/genimg/ >
<FilesMatch “.jpg”>
Options +ExecCGI
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance

Or alternatively (and haven’t figured out how to do this yet)

map all requests to a particular location to your cgi
(i.e. requests to yourserver.com
are mapped to yourserver.com
)
then use the trailing path_info to identify what kind of inmage to create:

so

will return a greyscale version of yourserver.com,

does the obvious,etc.

This is handy for existing images you need to manipulate, obviously.

···


As the trials of life continue to take their toll, remember that there
is always a future in Computer Maintenance.
– National Lampoon, “Deteriorata”
Rasputin :: Jack of All Trades - Master of Nuns

Hal E. Fulton wrote:

What’s the proper way to deliver a grpahical
file via HTTP?

with mod_ruby:
image is an object with such methods:
image.mime-type → image/jpeg, image/png, image/gif - depending on image
image.content → binary content of picture

picture.rbx

···

image = Image.new( filename )

r = Apache.request

r.content_type = image.mime_type
r.headers_out[‘Accept-Ranges’] = ‘bytes’
r.headers_out[‘Content-Length’] = image.content.size.to_s

r.send_http_header

print image.content


Andrey Kulinich
IT Group
Software developer
Phone/Fax +380 (372) 58-43-10
e-mail: Andrey.Kulinich@itgrp.net
http://www.itgrp.net

I tried just using a Content-type of image/jpeg,
but I got garbage in the browser.

Don’t some browsers insist on looking at filetype
rather than mime-type?

What do you mean by “filetype”? Do you mean “extension”? It shouldn’t make
any different. If the server sends back

Content-type: image/jpeg

then where is the filename to be found in the response? The browser could
look at the source URL (/foo/bar/baz.cgi) but that doesn’t tell it anything
about the type of the response.

I’ve written CGIs which spit out jpeg’s and they’ve worked, so I think it’s
likely to be some other trivial problem. Post the URL here and we can test
it using telnet to port 80…

Or alternatively (and haven’t figured out how to do this yet)

map all requests to a particular location to your cgi
(i.e. requests to yourserver.com
are mapped to yourserver.com
)

Something like:

ScriptAlias “/edit-img” “/usr/local/apache/cgi-bin/image.cgi”

ought to do the trick.

Regards,

Brian.

···

On Fri, Jul 18, 2003 at 07:28:42PM +0900, Rasputin wrote:

I tried just using a Content-type of image/jpeg,
but I got garbage in the browser.

Don’t some browsers insist on looking at filetype
rather than mime-type?

What do you mean by “filetype”? Do you mean “extension”? It shouldn’t make
any different. If the server sends back

Content-type: image/jpeg

then where is the filename to be found in the response? The browser could
look at the source URL (/foo/bar/baz.cgi) but that doesn’t tell it anything
about the type of the response.

I’m talking about the extension.
And some versions of IE. So laws of logic don’t apply :slight_smile:

ScriptAlias “/edit-img” “/usr/local/apache/cgi-bin/image.cgi”

ought to do the trick.

Doh! Cheers, will try that.

···

On Fri, Jul 18, 2003 at 07:28:42PM +0900, Rasputin wrote:

A successful [software] tool is one that was used to do something
undreamed of by its author.
– S. C. Johnson
Rasputin :: Jack of All Trades - Master of Nuns