One thing that I noticed is that you are printing out a Location: header. I haven't tried this as a method of providing an image in an HTML document, so I can't say for sure that it will work. However, take the following code and customize it and it should work fine.
#!/usr/bin/perl -w use strict; use LWP::Simple; use vars qw($in_file @imgdata @imgs $num $id $image $url $type $format $out_file); $in_file = 'ads.dat'; @imgdata = <DATA>; @imgs = grep {/\|(?:internal|partner)$/} @imgdata; $num = int(rand($#imgs) +1); ($id, $image, $url, $type) = split /\|/, $imgs[$num]; $url = $url . $image; $format = $1 if $image =~ /\.(\w+)$/; $format = 'jpeg' if $format =~ /^jpe?g$/; $out_file = get($url); print "Content-type: image/$format\n\n"; print $out_file; __DATA__ 1|/images/dibona_sm.gif|http://perlmonks.org|internal 2|/images/usermonkpics/ovidmonk.gif|http://perlmonks.org|partner 3|/pics/gol.gif|http://www.f.com|payed
The way to call it from an HTML document would be something like the following:
<img src="http://www.somedomain.com/cgi-bin/myscriptname.cgi" alt="Ran +dom image">
The only problem that I see with it is that you can't specify the height and width tags, but being able to embed it in HTML instead of just serving an image by itself should give you greater flexibility. (This code can probably be simplified further. I just threw it together).

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just go the the link and check out our stats.


In reply to (Ovid) RE: Printing Random Image in HTML by Ovid
in thread Printing Random Image in HTML by damian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.