LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-19-2004, 02:15 PM   #1
dodger-newbie
LQ Newbie
 
Registered: Jan 2004
Location: Meath, Ireland
Distribution: Red Hat Linux 9 - 2.4.20
Posts: 8

Rep: Reputation: 0
Unhappy Displaying images in CGI scripts


Hi

I am trying (without much luck) to insert an image into a cgi script. I have RedHat 9 with Apache running. I am trying to create a script and I am inserting an image into the script.

I am placing the image in the here document. The page appears but the image does not... I have the image in the /var/www/cgi-bin directory. I have also tried placing the image in the / dir but to no avail.

If anyone has any ideas on what I am doing wrong could they let me know. Any help is appreciated!!

Thanks
 
Old 03-19-2004, 04:30 PM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
What do you mean exactly by "inserting an image into the script?"

It sounds like you are physically taking the binary data for an image and appending it to the end of a script. If that is the case, it's not going to work.

Generally what you would do to have a CGI script generate an image, would be to send the appropriate Content-Type, then print out the binary data. (Note, I said print out the binary data, not append the data. You don't say what language you are using for the CGI program, so how you do that exactly, I can't tell you.)

Take for example a CGI script written in C... I might do something like so:

Code:
// Disclaimer, this code is just off the top of my head and isn't
// guaranteed to work perfectly.  I am probably forgetting some
// minor details here and there. :)
#include <stdio.h>
#include <file.h>

int main()
{
    FILE* fp = fopen("someimage.jpg");
    char ch;

    printf("Content-Type:Image/jpeg\r\n\r\n");

    while (!feof(fp))
    {
         // Read the file 1 byte at a time and print it out...
         // Could probably read bigger chunks, but this code
         // is just for demonstration purposes.
         fread(&ch, 1, 1, fp);
         printf("%c", ch);
    }

    return 0;
}
 
Old 03-19-2004, 06:46 PM   #3
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 54
CGI scripting is done in Perl. In fact, files ending .cgi and most files in cgi-bin on the internet are perl scripts using the CGI module.
If you're using the CGI module then before you print any other data you should be sending the content type headers (as shown in the C example above, only using Perl like a normal person ) or the easy way:
Code:
print(headers());
After that you can use normal HTML code just by including it in print statements...including images. Try:
Code:
print("<img src=myfile.png>");
Bear in mind that if you want to use quote marks (") around the filenames like you would when using ONLY HTML then you must escape them so they don't intefere with perl:
Code:
print("<img src=\"myfile.png\">");
Forgot to mention, if you knew all that but meant that the script simply can't find the image, then try putting it in the parent directory from the cgi-bin and reference it by "../image.png" (using the two periods to indicate parent directory).

Forgot also if you're using HTML within the perl script then you still need to send <HTML> and <BODY> (print("<html><body>") as you would with a normal HTML document.

Hope this helps.

Last edited by Komakino; 03-19-2004 at 06:52 PM.
 
Old 03-19-2004, 09:16 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
CGI actually doesn't need to be done in specific language. Perl is the most common language, but I'm most familar with C, and figured I'd have less chance of messing something up completely with an example in that.

My example is actually an example that can be called from another html page by embedding it in a page like so:

<img src="mycgi_example.cgi">

I was under the impression that you were trying to do some sort of validation before people could see images. If that is the case, you would want to put the image files somewhere that they wouldn't be directly accessible to an HTTP user, but that a CGI script could access and serve similar to my example.

As an example, I once worked on a photo album app that allowed people to log in to see photos that had been taken by a camera installed in their home. In this case, we obviously wouldn't want just anyone to be able to see the photos due to privacy issues... For this we stored the images in a database, but they could just as easily have been stored in a folder that was outside of the scope of what the web server would allow people to see. (e.g. if a user has a ~/public_html folder that can be seen from a web page, you could put the actual images in ~/images, which isn't accessible directly from the web server.)

If my assumption of what you were trying to do was wrong, however, just having your CGI print out an HTML image tag would be sufficient. And thinking about it, my assumption was most likely wrong. My mind was still thinking about how I used CGI scripts to create images. Using something like CGI or php to return an image content-type can also allow you to do other interesting things like create images "on the fly" based on parameters you pass in.

Last edited by deiussum; 03-19-2004 at 09:29 PM.
 
Old 03-20-2004, 06:39 AM   #5
dodger-newbie
LQ Newbie
 
Registered: Jan 2004
Location: Meath, Ireland
Distribution: Red Hat Linux 9 - 2.4.20
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for getting back to me..

What I meant by inserting a image into the script was that I am writing a perl script with CGI and I am trying to insert an image onto a webpage. I am inserting the image in a here document but I dont know for certain if this will work?? Ive tried placing the image in the root directory, the cgi-bin, parent dir to cgi-bin but it still doesnt display.. do I need to incorporate any module into my script or change permissions of the image..
I have enclosed a snippet of code to show what I am doing..could it be the Content - Type needs to change as I am inserting an image?

Any more help is greatly appreciated.

#!/usr/bin/perl


use DBI; #uses the DBI module

use strict;
use CGI qw(:standard); #uses CGI module
...

print header();

print <<HTML;
<HTML>
<head>
<title>TEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="white">
dkkdjlkdlkkldg
<table width="980" border="0" cellspacing="0" cellpadding="0" name="test2">
<tr>
<td bgcolor="#CCCCCC" width="342"><font color="#CCCCCC">gg</font></td>
<td bgcolor="#CCCCCC" width="340"><font color="#CCCCCC"></font></td>
<td bgcolor="#CCCCCC" width="318"><font color="#CCCCCC"></font></td>
</tr>
<tr>
<td width="342">
<div align="center"></div>
</td>
<td width="340"><img src="../interface.png" width="400" height="119"></td>
<td width="318">&nbsp;</td>
</tr>
<tr>
.........
....
 
Old 03-20-2004, 07:13 AM   #6
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 54
That all looks ok to me. My website uses cgi, I'll post an example:
Code:
#!/usr/bin/perl

use CGI qw (:standard);

#get POST data
$cgi = new CGI;
$page = $cgi->param('pagename');
$page = $page.".txt";

print(header());

print("<HTML>");
print("<head><style>");
print("body{
font-family: Tahoma, Arial, Helvetica, sans-serif;
color: 000000
}");
print("</style></head>");
print("<BODY>");
print("<center><img src=../title.png>");
print("<br>");
print("</center>");

#insert page here

if(!($page eq '.txt')){
print("<center>");
print("<a href=index.cgi?pagename=annabel><img src=../annabel.png border=0></a>");
print("<a href=index.cgi?pagename=language><img src=../language.png border=0></a>");
print("<a href=index.cgi?pagename=friends><img src=../friends.png border=0></a>");
print("<a href=index.cgi?pagename=annoyances><img src=../annoyances.png border=0></a>");
print("<a href=musings.cgi><img src=../musings.png border=0></a>");
print("<br>");
print("<a href=index.cgi?pagename=favourites><img src=../favourites.png border=0></a>");
print("<a href=guestbook.cgi><img src=../guestbook.png border=0></a>");
print("<a href=index.cgi?pagename=manifesto><img src=../manifesto.png border=0></a>");
print("<a href=index.cgi?pagename=starwarskid><img src=../starwarskid.png border=0></a>");
print("<a href=index.cgi?pagename=cartoons><img src=../cartoons.png border=0></a>");
print("<br><br>");

if($page eq "main.txt"){
print("<table width=50% border=1 bgcolor=4593b8><tr><td>");
print("Random song lyric:<br>");
print("<font size=-2>");
open(PAGE, "songlyrics.txt");
@lyrics = <PAGE>;
close(PAGE);
$random = int(rand(scalar(@lyrics)));
$thelyric = $lyrics[$random];
print("$thelyric<br>");
print("</font>");
print("</td></tr></table>");
}

print("<br><br>");
print("</center>");
	open(DATA, $page);
	@lines = <DATA>;
	close(DATA);
print("<table width=50% border=0 align=center><tr><td>");
	print(@lines);
print("</td></tr></table>");

} else {
@fortunes = `ls ../fortunes/*.png`;
$randomnum = int(rand(scalar(@fortunes)));
$randomnum++;
$image="welcome".$randomnum.".png";
print("<br><center><a href=index.cgi?pagename=main><img src=../fortunes/$image border=0></a></center>");
}


print("<br><br>");
print("<center>");
print("<img src=../banner_bottom.png>");
print("</center>");


print("</BODY>");
print("</HTML>");
 
Old 03-21-2004, 05:45 AM   #7
dodger-newbie
LQ Newbie
 
Registered: Jan 2004
Location: Meath, Ireland
Distribution: Red Hat Linux 9 - 2.4.20
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for the example..is it possible to insert the image within the here document like I have done above, instead of printing out all the individual print statements?? Do I need to change any of my code??

Thanks in advance..
 
Old 03-21-2004, 12:12 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Ok... I've only done a few CGI apps, but have done a lot of server-side scripting stuff with php/asp. But, I'm not quite sure what you mean by "within the here document." What are you referring to as a "here" document exactly?

Last edited by deiussum; 03-21-2004 at 12:16 PM.
 
Old 03-22-2004, 06:12 AM   #9
dodger-newbie
LQ Newbie
 
Registered: Jan 2004
Location: Meath, Ireland
Distribution: Red Hat Linux 9 - 2.4.20
Posts: 8

Original Poster
Rep: Reputation: 0
The here document allows you to specify a string that spans several lines.

To start one you would use the syntax
<<html
.
.
...
html

I think using here documents allows you to embed a large amount of HTML in your Perl program without having to fuss with quotation marks, multiple print statements etc..

I am just wondering is it possible to print out a image on a webpage from this?

Thanks
 
Old 03-22-2004, 02:28 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Ahhh, ok. I've seen that used, but didn't know the term for it.

Anyway, what you want isn't really feasible. HTML documents are text only, and your CGI script is basically creating an HTML document. In HTML, to display an image, you use an <IMG> tag, as has been shown in examples above already. You cannot have binary image data embedded directly into an HTML document.

If you are trying to create an image "on the fly", or are trying to display images from some sort of protected directory that a web browser doesn't have direct access too, then you will need a separate CGI script that returns only the image data specified by the Content-Type, and then in your HTML, you use that CGI script as the src attribute of the IMG tag.

If you aren't trying to protect the image, and you aren't generating it on the fly, you really just want to use the <IMG> tag. If you are having problems understanding that, you will need to be more specific with what exactly you are trying to do.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick way of displaying images Hydroksyde Linux - Software 0 10-02-2005 09:24 PM
displaying jpeg images Dave31836 Programming 2 01-25-2005 11:28 AM
apache not displaying images tlgates Linux - Networking 0 01-24-2005 04:06 PM
Simultaneously displaying Images with different depths yrraja Linux - General 0 04-25-2003 06:17 AM
Problem with Displaying 8/24bit XWD Images yrraja Programming 2 04-22-2003 06:33 AM


All times are GMT -5. The time now is 03:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration