Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Display gif in CGI page

by PhillipHuang (Beadle)
on Dec 08, 2011 at 09:41 UTC ( #942381=perlquestion: print w/replies, xml ) Need Help??

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

I write a simply CGI page to display gif picture:
use strict; use warnings; use CGI; use CGI qw(:all); my $q = CGI->new; print $q->header(-type=>'image/gif', -charset=>'UTF-8'); print $q->start_html(-title=>'testgif', -author=>'test@test', ); open(my $F, '<', '1.gif') || die "can not open\n"; local $\ = undef; print <$F>; close($F); print $q->end_html;
Well I can see a lot of output when exec this script in command line, but nothing displayed by the WebBrowser. What's the matter?

Replies are listed 'Best First'.
Re: Display gif in CGI page
by choroba (Cardinal) on Dec 08, 2011 at 09:43 UTC
    Is it a picture, or an HTML? If a picture, do not start_html.
      Thanks, gif displayed when remove 'start_html' lines. CGI manual says'The start_html() routine creates the top of the page, along with a lot of optional information that controls the page's appearance and behavior.This method returns a canned HTML header and the opening <body> tag. '. It seems to have nothing with displaying images, I am still confused why remove it can take effect.

        For the same reason that:

        cat foo.html bar.gif baz.html > quux.gif

        will create a file that is neither valid HTML nor a valid GIF.

        (Interestingly cat bar.gif baz.html > quux.gif does create a valid GIF.)

        Because calling it adds some lines to the output that do not belong to the image. The "image file" thus becomes corrupted and cannot be displayed (or even cannot be recognised as a picture).
Re: Display gif in CGI page
by Sinistral (Monsignor) on Dec 08, 2011 at 15:22 UTC

    You're confusing two kinds of scripts, one that generates HTML and one that generates an image suitable for use as the src of an img tag.

    Here is a simple CGI script that simply generates a page using an image tag:

    #!/usr/bin/perl use strict; use warnings; use CGI qw(:all); my $q = CGI->new; print $q->header(); print $q->start_html(); print $q->img({src => 'image.gif'}); print $q->end_html();

    And here is the code you want that actually emits the conttents of an image file to be used as the image src:

    #!/usr/bin/perl use strict; use warnings; use CGI qw(:all); my $q = CGI->new; print $q->header(-type => 'image/gif'); open(my $F, '<', '1.gif') || die "can not open\n"; local $\ = undef; print <$F>; close($F);

    Save the 2nd script as and you can modify the first script to use your second script:

    #!/usr/bin/perl use strict; use warnings; use CGI qw(:all); my $q = CGI->new; print $q->header(); print $q->start_html(); print $q->img({src => ''}); print $q->end_html();


    • You don't need two inclusions of CGI, just one with the flag :all to get all the routines
    • The image MIME type does not include a character set. It's binary data, there's no concept of character set
    • The die in will never be seen unless you look at web server error logs - the browser expects binary GIF image data and won't show your error message indicating that 1.gif can't be read
    • You don't need a script to use; a static HTML page with an img tag where the src attribute is is all you need. I just showed you that to show the two types of scripts you are confusing
    • You may not be able to use as your file name; you might have to use img.cgi, depending on how your web server is configureed
    • Your Perl may not be in /usr/bin; configure as appropriate for your system
    • These scripts assume that and 1.gif are all in the same directory. This is not necessary; you can use subdirectories.
    • Be sure to read the instructions for CGI.
    • You may also want to check out Beginner's Guide to CGI Scripting with Perl

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://942381]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2023-06-06 03:51 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (26 votes). Check out past polls.