This one seems to come up a lot in Seekers, so I thought I'd drop a simple script in here.

Although this script displays a random image from a specified directory, the principle should be adaptable to any image display code... basically, use a "Location:" redirect... It's worth noting that the version given here depends on the image directory being a sub-directory of the script's location. On most servers, this won't be the case, and you would probably have to manipulate the location parameter to account for this.

...And, as merlyn has pointed out, any implementation of this snippet will probably break at least one standard, or just plain not work, so I've provided two more standard-compliant (but longer) alternatives.

Non-standard-compliant version (and with probable caching):

#!/usr/bin/perl -wT use strict; # Call in img tag - e.g.: <img src="randimage.cgi" border="2"> my @files = glob("./jpegs/*.jpg"); print "Location: $files[int(rand @files)]\n\n";

Standard-compliant version (short, but with potential caching of images):

#!/usr/bin/perl -wT use strict; use URI; my @files = glob("./images/*"); my $file = URI->new_abs($files[int(rand @files)], "http://bugzilla"); print "Status: 307 Temporary redirect\n"; print "Location: $file\n\n";
<updated>

Standard-compliant version (long - no caching problems):

#!/usr/bin/perl -wT use strict; # types where extension <> content-type my %types = qw(jpg jpeg tif tiff); # get an array of image-files my @images = glob("./images/*"); die "No images found\n" unless scalar @images; # choose an image and and prepare content type extension my $image = $images[int(rand @images)]; die "No extension for $image\n" unless $image =~ /\.(.+)$/; my $ctype = $1; $ctype = $types{$ctype} if exists $types{$ctype}; # read and print image open(IMG, $image)||die "Cannot open $image:$!\n"; print "Content-type: image/$ctype\n\n"; binmode (IMG); binmode (STDOUT); print while(<IMG>); close IMG;
</updated>

In reply to How to display an image on a webpage with minimal code by Melly

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.