Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am new to Perl CGI.
Can anyone help me how to display an image(gif,jpg) on the output screen along with some text information.

My code looks like:
print qq~Content-type: text/html\n\n~; print qq~ <html> <title>Example</title> <body bgcolor="#ffffff"> <img src="logo.jpg"> "hello U r here" </body> </html> ~;
But by this image is not displayed, however the text msg was displayed.
It would be helpful if anyone provide me some code examples.

Thank you.

Replies are listed 'Best First'.
Re: To display images using CGI
by davorg (Chancellor) on Jul 24, 2006 at 07:54 UTC

    You haven't really given enough data to be sure, but here's my guess at what is happening.

    Your CGI program and the image are both in the /cgi-bin directory on your web server. Your web server is almost certainly configured to treat all requests for content within cgi-bin as a request for executable content. Therefore here's what happens:

    1. Browser requests /cgi-bin/your-program
    2. Your program returns your HTML, but that includes a reference to logo.jpg. This is a relative URL so the browser requests it from the same place as the current page
    3. Therefore the browser requests /cgi-bin/logo.jpg
    4. As this request is for a resource in cgi-bin the web server tries to execute the file (logo.jpg) and return the output to the browser (in exactly the same way as it does for any CGI request)
    5. As logo.jpg is not an executable file, the operating system returns an error when the web server tries to execute it
    6. The web server therefore returns a 500 error to the browser, but as this isn't the main request for the page the browser just puts a "broken image" placeholder on the page.

    If this is the case, there are a few lessons you can learn:

    1. Only executable files go in the cgi-bin directory. Static content goes elsewhere in the directory tree
    2. Checking the web server access log would have shown you the requests that were being made
    3. Checking the error log would have shown you the error that the web server got when trying to execute the image file.
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: To display images using CGI
by McDarren (Abbot) on Jul 24, 2006 at 05:48 UTC
    "It would be helpful if anyone provide me some code examples."
    #!/usr/bin/perl -wT use strict; use CGI qw/:standard/; my $logo = '/path/to/logo.jpg'; print header, start_html, img {src=>$logo };

    "I am new to Perl CGI."
    Two recommendations:
    1. Use CGI.pm
    2. Check out Ovid's CGI Course.

    Cheers,
    Darren

Re: To display images using CGI
by Cody Pendant (Prior) on Jul 24, 2006 at 05:25 UTC
    That's not a Perl or a CGI problem, as far as anyone can tell. It's probably an HTML problem. Where is your "logo.jpg"? Is it in the same folder as your script? If not, that's why it's not displaying, and you need to add a path, like
    <img src="/foo/bar/img/logo.jpg">
    or, of course, move the image so it is in the same folder...


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
      The file logo.jpg is in the same directory as the cgi script.
      But the image is not dispalyed. Instead an icon saying that image not displayed is present.
      Please help me.
      Thanks.
        The file logo.jpg is in the same directory as the cgi script

        Please specify for us exactly which folder the script is in, which folder the image is in, and which URL you're using to look at the script.

        Then use your browser (right-click on the image) to find out exactly where the browser thinks the image is, and tell us that.

        The only other possibility I can think of is that there's something wrong with the image. What happens when you look at it directly with the browser?



        ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
        =~y~b-v~a-z~s; print
        The file logo.jpg is in the same directory as the cgi script

        Actually, the way you've written it, I think 'logo.jpg' needs to be in the cwd (current working directory) which is not necessarily the same directory as the cgi script. You could modify the script to 'use Cwd;' and 'print getcwd();' so that you can check on just which directory is the cwd.

        What happens if you specify the full path to logo.jpg in the script ?

        Cheers,
        Rob