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 img.pl 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 => 'img.pl'});
print $q->end_html();
Notes:
- 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 img.pl 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 img.pl; a static HTML page with an img tag where the src attribute is img.pl 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 img.pl 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 img.pl 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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.