Preferably with as little code as possible to make sure nothing goes wrong on lower end viewers, even without JavaScript perhaps?
Yeah, don't inflict JavaS'creep on your viewer unless absolutely necessary.
If you are considering a server-side approach and have no special requirement regarding different on-page setup for each different diced image, it would be easiest to just hard-code the image reference in your HTML to point to a
(server-side thingamajig). And let that thingamajig dice out an image for you--- usually via a HTTP 302 "Found" redirect to one of the actual image files.
I referred to that stuff as a
(server-side thingamajig), because it could be many kind of server-side stuffs you can
interchangeably use for this purpose; as I'll elaborate...
In case of PHP,
@Slix already hinted about some part of it; but I will note that using PHP `rand()` like in examples linked there would usually leave (few-)first and (few-)last image(s) in the array underused-- because of imperfect randomness distribution of such pseudorandom algorithms. Using the max dicing range then modulo the result with total items would have a much better randomization result.
PHP example of
server-side thingamajig with random-modulo and redirection approach I mentioned: `randomimage.php`...
<?php
$images = array(
"image1.jpg",
"image2.jpg",
"image3.jpg"
);
$target = $images[rand() % count($images)];
header("Location: ".$target, true, 302);
?>
Which would be called from HTML using a plain:
<img src="randomimage.php" alt="Random picture">
This should work on any PHP version from 4 to 8. Tested on:
- PHP 5.16.3 running under Apache 2.4 via its `mod_php`; CentOS GNU/Linux 7 x86-64.
- PHP 8.2.24 running under Nginx 1.26.2 via PHP-FPM; Fedora GNU/Linux 39 x86-64.
Very terse and effective, right?
But if you got a distaste for PHP like me, and you were lucky enough to use a server that allow you to write a shell script CGI program
(1), you might just dice stuff Quick&Dirty (TM) way with
GNU `shuf` and a bit of here-document, then call it a day.
Shell script CGI example of
server-side thingamajig: `randomimage.sh`...
#!/bin/sh
echo "Status: 302"
echo "Content-Type: text/plain"
printf "Location: "
shuf -n 1 << 'EOF'
image1.jpg
image2.jpg
image3.jpg
EOF
echo
Which would be called from HTML using a plain:
<img src="randomimage.sh" alt="Random picture">
Tested with GNU Bash 5.2.26 as `sh` and GNU Coreutils 9.3 `shuf`, running under Nginx 1.26.2 via fcgiwrap 1.1.0; Fedora GNU/Linux 39 x86-64.Note that in a shell script approach, there is even an easier way to let it pick images from current folder without you having to explicitly list them too.
However, if you're on a server with 90s-style Perl CGI support, it can be used for this as well: so here is a Perl CGI example of
server-side thingamajig using random-modulo approach: `randomimage.pl`...
#!/usr/bin/perl
@images = (
"image1.jpg",
"image2.jpg",
"image3.jpg"
);
$target = $images[int(rand(65535)) % scalar(@images)];
print "Status: 302\n";
print "Content-Type: text/plain\n";
print "Location: ".$target."\n";
print "\n";
Which would be called from HTML using a plain:
<img src="randomimage.pl" alt="Random picture">
This should work with any Perl 5 version (likely Perl 4 too, but don't quote me on this) and any CGI-supporting web server from the current millennium. Tested with:
- PHP 5.16.3 running under Apache 2.4; CentOS GNU/Linux 7 x86-64.
- Perl 5.38.2 running under Nginx 1.26.2 via fcgiwrap 1.1.0; Fedora GNU/Linux 39 x86-64.
If you're using Apache web server with access to your site's `<VirtualHost>` configuration, you don't even need PHP or CGI program; you can even set up a file with mappings containing lists of images you would like like to roulette out, and dice that out directly in `.htaccess` using `mod_rewrite`'s directives.
However, as it require configuration in `<VirtualHost>` section; if you're on shared hosting, it would require you to ask your hosting's support technician to put that in; so I'm not going to bore you with this novelty approach unless you (or others) would like to know more about it.
In any case, you can be most terse about the filenames listed within the
(server-side thingamajig) in question, if you
put that thingamajig in the same folder as your images. Remember to use web-safe file and directory names
(2), and replace paths in the examples with the actual image paths (absolute or relative-to-the-server-side-thingamajig) you're using. You can even use URL which point to image on other servers in these as well.
P.S. All code samples in this post are written by me and released to public domain under
Creative Commons Zero 1.0.
(1) Commercial web hosting services don't usually allow this; but it is more common in a pubnix and tilde realm. (If you don't know what pubnix and tilde are; you may as well skip over this shell script section)
(2) Technically, the so-called filenames in these thingamajigs are URL; feel free to use percent escapes if you're feeling adventurous.