Return Image 
Author Message
 Return Image

Hello,
Does anybody know how to make a CGI in Perl that it returns me an image (as
if it was a counter) or that it returns me its link so that in the HTML page
can appear the following:

<img src="cgi-bin/prova.pl" width="120" height="20"
alt="Prova">

Thanks.

--
________________________________________
web personal: http://www.*-*-*.com/



Sun, 22 Dec 2002 03:00:00 GMT  
 Return Image
print "Content-type: image/gif", "\n\n";
open(IMAGE, "<path_to_image/image.gif");
binmode(IMAGE);
while (<IMAGE>){
        print $_;
Quote:
}

close(IMAGE);

Quote:

> Hello,
> Does anybody know how to make a CGI in Perl that it returns me an image (as
> if it was a counter) or that it returns me its link so that in the HTML page
> can appear the following:

> <img src="cgi-bin/prova.pl" width="120" height="20"
> alt="Prova">

> Thanks.

> --
> ________________________________________
> web personal: http://www.ctv.es/USERS/jumigas

--
*********************************
*                               *
*  John C. Nichel IV            *
*  Web-Guru, By-Tor's Doghouse  *
*  http://www.by-tor.com        *

*  ICQ# 650118                  *
*                               *
*********************************


Sun, 22 Dec 2002 03:00:00 GMT  
 Return Image


Quote:
> print "Content-type: image/gif", "\n\n";
> open(IMAGE, "<path_to_image/image.gif");
> binmode(IMAGE);
> while (<IMAGE>){
>    print $_;
> }
> close(IMAGE);

You forgot to binmode STDOUT also, which is ~256 times likelier to cause
a problem on a system where it is needed.  It can go on either side of
the header-print, and all will be well.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sun, 22 Dec 2002 03:00:00 GMT  
 Return Image
On Wed, 5 Jul 2000, John Nichel up-ended usenet and opined:

Quote:
> print "Content-type: image/gif", "\n\n";
> open(IMAGE, "<path_to_image/image.gif");
> binmode(IMAGE);
> while (<IMAGE>){
>    print $_;
> }
> close(IMAGE);

First of all, for most WWW applications this would be a waste of
effort.  If the image is already available on the web server, then
  print "Location: /URLpath/to/image.gif\n\n";
is defined to produce the same effect, for less effort.  See the CGI
spec.   (For a parsed-headers script.)

Whereas, if you're generating the image dynamically, you'd normally
write it directly to standard output, rather than constructing a disk
file and they solemnly copying _that_ out (not forgetting to clean up
the mess afterwards).

Sure, there may be conceivable special cases where the above is
defensible - just that IMHO it seems a bit misleading to offer it
without comment as if it might be the normal thing to do.

However, if we take it just as a simple demonstration, then the
correctly-specified binmode for the GIF input ought surely to have
triggered the thought that the output also needs to be opened in
binmode too.

And reading and writing a binary file "record-by-record" with the
diamond operator could be a bit of a hit and miss operation.

Still, it _was_ an upside-down posting...

My suggestion would be to take a look at some well-reputed source. Say
for example in
http://www.oreilly.com/catalog/cookbook/chapter/ch08.html below the
heading "Processing Binary Files", and the specific example which
appears after the initial discussion.  The hon. Usenaut would need to
add the CGI response header, of course, for the present requirement.

have fun.



Sun, 22 Dec 2002 03:00:00 GMT  
 Return Image
Alan J. Flavell posted
 >
 > First of all, for most WWW applications this would be a waste of
 > effort.  If the image is already available on the web server, then
 >   print "Location: /URLpath/to/image.gif\n\n";
 > is defined to produce the same effect, for less effort.  See the CGI
 > spec.   (For a parsed-headers script.)
 >
This is /loosely/ related to the main thread.

We would like to avoid writing a csv (generated from a DB call) to a file
and then redirecting the browser to that file.  The latter implementation
causes MSIE (apologies) to run excel inline and renders the csv in excel -
kewl.  However we would like to write directly to the browser with a
mime-header first as suggested in this thread.  I am not getting anything
from: grep  csv /etc/mime.types
any suggestions.

thanx

--
Eric Smith



Mon, 23 Dec 2002 03:00:00 GMT  
 Return Image
: Alan J. Flavell posted
:  >
:  > First of all, for most WWW applications this would be a waste of
:  > effort.  If the image is already available on the web server, then
:  >   print "Location: /URLpath/to/image.gif\n\n";
:  > is defined to produce the same effect, for less effort.  See the CGI
:  > spec.   (For a parsed-headers script.)
:  >
: This is /loosely/ related to the main thread.

: We would like to avoid writing a csv (generated from a DB call) to a file
: and then redirecting the browser to that file.  The latter implementation
: causes MSIE (apologies) to run excel inline and renders the csv in excel -
: kewl.  However we would like to write directly to the browser with a
: mime-header first as suggested in this thread.  I am not getting anything
: from: grep  csv /etc/mime.types
: any suggestions.

What the browser will do depends on the configuration of the browser.
They typically render some data types themselves, and pass other data
types to some preconfigured other application and/or ask the user if
they want to save the data in a file.

You could try text/plain.  The browser will probably show the raw text so
someone can cut and past the parts they need.

Otherwise look at the browsers (i.e. fred's browser if fred is going to
get the data, NOT MSIE in general), and see what mime types are
configured and what they do, and then choose one that does what you
want.



Mon, 23 Dec 2002 03:00:00 GMT  
 Return Image

Quote:
>  > First of all, for most WWW applications this would be a waste of
>  > effort.  If the image is already available on the web server, then
>  >   print "Location: /URLpath/to/image.gif\n\n";
>  > is defined to produce the same effect, for less effort.
> We would like to avoid writing a csv (generated from a DB call) to a file
> and then redirecting the browser to that file.  The latter implementation
> causes MSIE (apologies) to run excel inline and renders the csv in excel -
> kewl.  However we would like to write directly to the browser with a
> mime-header first as suggested in this thread.  I am not getting anything
> from: grep  csv /etc/mime.types

Well, whichever mechanism you use for sending it, the same
Content-type would be appropriate, so I'd say your question is more
suitable to be asked on a WWW group.

I must stress though that as far as the client agent is concerned,
there's no reason for it to know or care whether you wrote a disk file
and pointed to it, or wrote directly to a CGI script's standard
output, as long as you sort the headers out properly.  In the one case
it's the server's configuration that decides the content-type header
that's sent, in the other case it's your script, but the client need
have no idea what mechanism actually led to the network transaction
that it sees.  This is an important principle in client/server
engineering.

For the proper open-systems approach, one would first look at the IANA
registry of MIME types.  They are below
http://www.isi.edu/in-notes/iana/assignments/media-types/

To be honest I've been here before and not got a satisfactory answer.
text/tab-separated-values is registered, but the corresponding one
with commas isn't.

Excel is registered as application/vnd.ms-excel

However, MSIE's disdain for the open systems approach is legendary,
and their "handling" (I use the term advisedly) of the content-type
header is revealed here:

http://msdn.microsoft.com/workshop/networking/moniker/overview/append...

Maybe that has something which you can use.  Pretending that your csv
data is application/vnd.ms-excel might do the trick, although it is
deeply hostile to the web ethos of describing the data honestly as
what it is, rather than trying to persuade the browser to undertake a
particular course of action.

Apologies to the Perl folks, this has strayed way off theme now.
f'ups suggested on the basis of a server configuration issue.



Mon, 23 Dec 2002 03:00:00 GMT  
 Return Image

[follow-ups set]


Quote:
> Alan J. Flavell posted

>  > First of all, for most WWW applications this would be a waste of
>  > effort.  If the image is already available on the web server, then
>  >   print "Location: /URLpath/to/image.gif\n\n";
>  > is defined to produce the same effect, for less effort.  See the CGI
>  > spec.   (For a parsed-headers script.)

> This is /loosely/ related to the main thread.

> We would like to avoid writing a csv (generated from a DB call) to a file
> and then redirecting the browser to that file.  The latter implementation
> causes MSIE (apologies) to run excel inline and renders the csv in excel -
> kewl.  However we would like to write directly to the browser with a
> mime-header first as suggested in this thread.  I am not getting anything
> from: grep  csv /etc/mime.types
> any suggestions.

what do you want to happen on the client side?  i've done this with
all sorts of formats since one of my products allows the client to
dump the data in whichever format they like (including user defined
formats).

i give them the option of downlaoding the dump file (in whatever format)
just as it is or as a zip or tar & gzipped [1] archive.  if they want to
save it to a file, they can use one of the compression schemes available.
even if a browser decides to hand it off to a helper, the helper will only
create files (most likely).  judicious use of PATH_INFO makes all of
the names come out correctly.

but "writing to the browser" sounds like something different.  if you
want it to actaully show up in the browser window, then you might
want to consider sending back text/plain and not giving the file some
extension that IE will intercept.  a kludge, but welcome to IE^WWeb
programming.

[1] and, if they archive the dump file, we also throw in some READMEs
and a few other things, which is why it's not just compressed.

--
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>



Mon, 23 Dec 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Image Magick and returning images to a web page

2. Return image

3. CGI returns image, closes connection, keeps going?

4. Return image

5. who to return images

6. returning a image via perl

7. Image Magick finding image size without loading image

8. Returning an image from a call to a script

9. Returning an image

10. How to return an image from perl

11. Read / Concat / Return GIF Images

12. Converting images without Image::Magick

 

 
Powered by phpBB® Forum Software