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

Ok, I am working on a banner site and I need to figure out a way to do this:

How can I include a GIF picture *inside* an HTML document?
Example:

print "Content-Type: text/html\n\n"; print " <html> <body> <---Banner needs to go here---> Hi, welcome to the site.... ";

But where the image needs to go, the image source needs to be pointing to a perl script, not an image!
So the perl script would look something like this maybe:
$random = rnd($numofbanners); open IMAGE, "images/banner$random.gif"; while (<IMAGE>) { print $_; } close IMAGE;

But of course this won't work.
Also, I don't want the server to have to have server includes setup.
I was thinking maybe an embed of some kind?

HELP!

Replies are listed 'Best First'.
Re: Mixing HTML and GIF
by lhoward (Vicar) on Jul 15, 2000 at 23:31 UTC
    You can't actually embed an image straight into your HTML. There are 2 ways to go about this:
    1. You have your IMG tag point to a separate CGI program that generates/serves the image on the fly. This is good for when the image is truly dynamic. Example:
      script1.cgi (this method will also work if the HTML is not dynamically generated):
      #!/usr/bin/perl -w use strict; print "Content-type: text/html\n\n"; print <<EOT; <html><head><title>Image Example<title></head> <body> <img src="script2.cgi?size=20"> </body> EOT
      script2.cgi
      #!/usr/bin/perl -w use strict; use CGI; use GD; my $cgi=new CGI; my $cgi_size=$cgi->param('size') || '50'; print "Content-type: image/gif\n\n"; my $gd=new GD::Image($cgi_size,$cgi_size); my $blue=$gd->colorAllocate(0,0,255); $gd->fill(0,0,$blue); print $gd->gif;
    2. Your CGI generating the HTML page varies the content of the IMG tag. This is good if you are pulling the image from a fixed set of pregenerated images.
      script3.cgi (assuming the images are named banner1.gif to banner8.gif)
      #!/usr/bin/perl -w use strict; my $rnd=int(rand()*8); my $img="images/banner".$rnd".gif"; print "Content-type: text/html\n\n"; print <<EOT; <html><head><title>Another Image Example<title></head> <body> <img src="$img"> </body> EOT
RE: Mixing HTML and GIF (Q&A Link)
by Russ (Deacon) on Jul 16, 2000 at 07:15 UTC
Re: Mixing HTML and GIF
by Abigail (Deacon) on Jul 16, 2000 at 00:40 UTC
    Since this question has nothing to do with Perl, I keep the answer short. Goto www.w3c.org, and learn about the OBJECT element, and the data pseudo-URL.

    -- Abigail

RE: Mixing HTML and GIF
by BigJoe (Curate) on Jul 15, 2000 at 23:20 UTC
    I did something like this make the html into an .shtml then put in the html <!--#exec cgi="banner.pl"-->

    print ("Content-type: text/html\n\n"); $i = "<img src=$path" . banner . "$random" . "$end >"; print ("$i");


    That worked for me

    --BigJoe
Re: Mixing HTML and GIF
by Arjen (Sexton) on Jul 15, 2000 at 23:22 UTC
    mt2k, Wouldn't it be possible to use Server Side Includes for this?

    In the HTML:

    <!--exec cgi="/your/script/here.pl" -->

    And the script producing output with an image/gif content type document with the picture in it.

    Cheers

    Arjen
Re: Mixing HTML and GIF
by mt2k (Hermit) on Jul 15, 2000 at 23:25 UTC
    Lhoward - major problem with either me or the code!
    How does that code in #1 load the image? Your code doesn't open a file to read out the image!
    Not angry, just how would I go about loading an existing image?
      Off the top of my head, more suited to show you the general means by which I'd tackle the program than the exact syntax:
      use strict; use CGI; my $q = CGI->new(); { local *IMAGE; open(IMAGE, $q->param('image')) || die "Can't open: $!"; binmode(STDOUT); print while <IMAGE>; close IMAGE; }
      His #1 has two code segments. The first one is from your main cgi, which calls the second segment. That segment prints out two things: the header (image/gif) and the image itself.
      Paris Sinclair    |    4a75737420416e6f74686572
      pariss@efn.org    |    205065726c204861636b6572
      I wear my Geek Code on my finger.
      
      if the banners are on a different site just put that in the path on the example I gave. Otherwise you can use JavaScript to pick a random pic but you have to load every pic to the page. But then it would be a JavaScript thing and not a PERL question.

      --BigJoe
A reply falls below the community's threshold of quality. You may see it by logging in.