in reply to Printing Random Image in HTML

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.

Replies are listed 'Best First'.
Re: (Ovid) RE: Printing Random Image in HTML
by markjugg (Curate) on Jul 04, 2002 at 14:42 UTC
    Thanks for this example, ovid.

    I had a little better luck with a version of this script I reworked the parens in the random image chooser like this:

    my $num = int(rand($#imgs +1));

    I had a list of just two images, and I kept getting the same image routine. I didn't think too hard about why my change worked any better, though. :)

    -mark