Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help displaying an image in CGI script

Status
Not open for further replies.

HSand

Programmer
Sep 17, 2002
7
0
0
IE
howdy all!

i'm having a little trouble with my CGI script: the problem is that i cannot get the script to display .gif images...

i don't know how to use the "content type: image/gif" statement, so if anyone has any sample code of a perl (or any language) CGI script that displays a .gif image, or can point me in the roght direction, then i'd REALLY appreciate it.

just for the record, i'm using a Linux (SuSE 7.3) server, that runs Apache 2.0.39 and Perl 5.6, but i don't reckon any of this matters

Thanx for your help
Colin :)
 
When you set up apache you can configure it to accept certain file types. If you look into the apache directory you see the conf directory. In there is the config file for apache. Also in the mime.types file which is in the same directory, make sure the type image.gif is listed.

To print out the Content-type header for image/gif:

print "Content-type: image\gif\n\n;

But here is something better so you have something for everyone visiting the page:

#!/usr/bin/perl

# Access the environment variable by name
#gets the content type they accept
$http_accept = $ENV{'HTTP_ACCEPT'};

# Unbuffer output so that we can see the image as it comes out,
# rather than waiting for the buffer to flush.

$| = 1;

# Test the value and take the appropriate action. For the first test,
# check whether the browser accepts JPEG images where the "image/jpeg"
# string will be found in the HTTP_ACCEPT list and if so then send a
# JPEG image to the browser.

if ($http_accept =~ m#image/jpeg#)
{
# Browser understands JPEGs so send it a JPEG image
&DumpFile("lighthou.jpg", "image/jpeg");
}

# Check whether the browser accepts GIF images. If the string "image/gif" is
# found in the HTTP_ACCEPT list then the browser claims to support GIF images
# so send it one.

elsif ($http_accept =~ m#image/gif#)
{
# Browser understands GIFs so send it a GIF image
&DumpFile("lighthou.gif", "image/gif");
}

# If the HTTP_ACCEPT value is not defined or neither image types (JPEG or GIF)
# are supported then it will fail the first two tests. For this case perform
# the default action which is to show some text.

else
{
# Browser does not understand GIFs nor JPEGs so show some plain old text
print "Content-type: text/plain\n\n";
print "Text is the only thing I can show you.\n";
print "Hope you weren't expecting an image or something.\n";
}

# Exit the program

exit;

sub DumpFile
{
local($filename, $content_type) = @_;

# Open the specified file

unless (open(FILE, "$filename"))
{
print "Content-type: text/plain\n\n";
print "Sorry, the file '$filename' cannot be opened.\n";
print "Please report this error to the webmaster.\n";
exit;
}

# Identify what type of file will be sent to the browser with the
# MIME type from which the variable $content_type will either be
# "image/jpeg "or "image/gif" depending on what the browser supports.

print "Content-type: $content_type\n\n";

# Read and print out the entire file.

while (<FILE>)
{
print;
}

# Close the file and return to the main program.

close(FILE);
}

#
# end of chk-acpt.pl
#



Good luck.
Thanks
Tricia
yorkeylady@earthlink.net
 
Thanx!!

you're a lifesaver!!!

;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top