I want to make something that I've not seen done in web development, but it seems possible. I want a URL that can be put into any arbitrary HTML <IMG/>
tag or CSS background-image
property that, when accessed, is a random image. I don't want the page to point or redirect to a random image, and I don't want it to display or embed a random image, as these things won't work with the above-stated requirements. I've seen solutions for displaying a random image in a webpage using Javascript to swap a property or hide an element, but this has to make the entire page the image, with no preceding or succeeding binary or ASCII data. I want it to be usable by people who don't know anything about programming. What I'm aiming for is something like this:
http://example.com/randomImage/
In theory, every time a page is loaded with this URL in the CSS or HTML, a random image will be displayed in the referenced element. It should work so that if you have several <IMG/>
elements or several elements with their background-image
referencing this page, they should all show randomly different images.
My first thought is to use PHP (in the structure described below) to look into a particular directory, pick an image file at random, and echo its binary data. If this is possible, then how would I go about doing it? If not, then does anyone have any other ideas?
My primary idea as pseudocode:
<?PHP
$chosenImage = getRandomFileFrom("./rand/");
$bytes = readFileAsBinary($chosenImage);
foreach($bytes as $byte){
echo $byte;
}
?>
I don't care about true or pseudo-random. It could cycle each time for all I care; all I require in this endeavor is that multiple images can be referenced as a single URL