Display Image using CGI 
Author Message
 Display Image using CGI

Hi,

I saw an example in a book that said that I could use a perl script to
dynamically display an image when an HTML page is displayed.  For example,
in my web page, I would call this script in the following fashion:

<p><img src=" http://www.*-*-*.com/ ;</p>

Now the "set_sanity_colour.pl" script would simply select and return the
location of the graphic file I wish to display.  In order to simplify
things, I have streamed down the script for this email:

====================================
#! /usr/bin/perl


work... ;-)
$file = "sanity_green.jpg";

print "Location: $baseurl$file\n\n";
====================================

Now, obviously, this does not seem to work.  Any suggestions as to why?

Just to let you know, I do have full access rights to the web server and the
appropriate priviledges have been given to the script file. (Apache Web
Server)

Thank you



Sun, 16 May 2004 17:02:16 GMT  
 Display Image using CGI

Quote:
> Hi,

> I saw an example in a book that said that I could use a perl script to
> dynamically display an image when an HTML page is displayed.  For example,
> in my web page, I would call this script in the following fashion:

> <p><img src="http://www.skyweb.ca/cgi-bin/set_sanity_colour.pl"</p>

> Now the "set_sanity_colour.pl" script would simply select and return the
> location of the graphic file I wish to display.  In order to simplify
> things, I have streamed down the script for this email:

> ====================================
> #! /usr/bin/perl


> work... ;-)
> $file = "sanity_green.jpg";

> print "Location: $baseurl$file\n\n";
> ====================================

> Now, obviously, this does not seem to work.  Any suggestions as to why?

> Just to let you know, I do have full access rights to the web server and
the
> appropriate priviledges have been given to the script file. (Apache Web
> Server)

> Thank you

The basic of way of displaying an image is to send the appropriate
Content-type header along with the image.  To send that "sanity_green.jpg",
try the following:

    #!/usr/bin/perl -wT
    use strict;
    $!++;
    my $file = 'sanity_green.jpg';
    open IMAGE, "< $file" or die "Cannot open $file for reading: $!";
    binmode IMAGE;
    print "Content-type: image/jpeg\n\n";
    print <IMAGE>;
    close IMAGE;

Notes:

1.  binmode has no effect on many systems, but ensures that those systems
for which it *does* have an effect (e.g. Windows), no line-ending
translations are attempted.
2.  A common source of error for something like this is get the Content-type
line wrong or to send the wrong media type. Pay extra attention to that
line.

--
Cheers,
Curtis Poe
Senior Programmer
ONSITE! Technology
www.onsitetech.com
503.233.1418



Sun, 16 May 2004 17:30:22 GMT  
 Display Image using CGI
On Nov 28, RAL inscribed on the eternal scroll:

Quote:
> I would call this script in the following fashion:

> <p><img src="http://www.skyweb.ca/cgi-bin/set_sanity_colour.pl"</p>

Indeed, but that's nothing specific to Perl...

Quote:
> #! /usr/bin/perl

You've just told us you couldn't be bothered to do your homework
before posting to this group.  As such, you're on sufference...

Quote:

> work... ;-)
> $file = "sanity_green.jpg";

> print "Location: $baseurl$file\n\n";

Looks to be along the right lines, but next time let's see some
evidence that you're taking advantage of all the help that Perl itself
offers you.  Read, mark, learn and digest the new users briefing.

Quote:
> Now, obviously, this does not seem to work.

Once again you disregarded the new-user cautions that you should not
merely say "doesn't work", you should tell us clearly WHAT you
observed.

Quote:
>  Any suggestions as to why?

No, but if I were in this position, I would first break the problem
into parts and verify that I could make each one of them work, before
re-assembling them into a solution.  While I'm doing that, I usually
realise what it was that I got wrong, and fix it without having to
wait for an answer from usenet.  And when it does happen that I need
help, then I can post a nice clean question without extraneous detail.

And, by the way, if you provide real URLs where we can try your stuff
out for ourselves we often get more quickly to the answer.

I don't yet know whether your problem is with Perl or with the WWW
part of your task.  I'm inclined to think you'd have been better
advised to post to c.i.w.authoring.cgi, but I could turn out to be
mistaken.



Sun, 16 May 2004 18:00:25 GMT  
 Display Image using CGI
On Nov 28, Curtis Poe inscribed on the eternal scroll:

[excessive quotage snipped]

Quote:
> The basic of way of displaying an image is to send the appropriate
> Content-type header along with the image.

You _can_ do that, sure, if you're generating the thing dynamically.

But if the final answer is a static resource, it's perfectly feasible
to redirect to its URL (in either of the two ways defined by the CGI
specification for the Location: response) if that's what the
questioner wanted to do.

None of that is a Perl question, of course.  Should be taken up on
c.i.w.authoring.cgi.

Quote:
>     open IMAGE, "< $file" or die "Cannot open $file for reading: $!";
>     binmode IMAGE;

Good move; but what about the output filehandle?


Sun, 16 May 2004 18:08:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Displaying an image using CGI.pm

2. CGI image generation using Image::Magick

3. Newbie Help: How to display image on web page using Perl

4. How display a png image in CGI script

5. Displaying Image::Magick image objects in Perl/Tk?

6. Constructing a next/more feature for a list display using PERL (CGI)

7. Inserting, deleting, displaying data from MySQL using CGI

8. perl CGI for displaying text encoded with ANSI display sequences

9. cgi-problem: internet explorer displays web-page - netscape displays source code

10. perl CGI for displaying text encoded with ANSI display sequences

11. Using CGI and GD to resize uploaded images...

12. Using CGI.pm to Retrieve Image

 

 
Powered by phpBB® Forum Software