<?php
$today = (int)date('Yz');
srand($today);
$files = glob('*/*/thumbs/*.jpg'); // your folder structure goes here, my new one is like 2018/september/thumbs/*.jpg
$file = array_rand($files);
// open the file in a binary mode
$name = $files[$file];
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
Embedded like this:
<img src="oftheday.php" alt="[img]">
(If you need to display EXIF information or link to the original image, see this script instead.)
I’ve been working on a revamp of my site! In doing so I’ve been practicing some PHP; one thing I wanted to do was have a randomized image on the homepage that would change every day. Looking around online I didn’t find many drop-in solutions I could just stick on the page, so I pieced together my own; and PHP actually makes it rather simple.
To explain, $today is set to the current date in the Yz format which is in the form of year and day in the year; for example, if today is 4/6/2020, it would come out to 202097. Tomorrow, it’d be 202098. That is converted from a string into an integer with (int), as it is going to seed PHP’s random number generator with srand(). Otherwise, the output would be random on every refresh.
glob() is then used to get all the jpg files in the photography directory and store that as $files. array_rand() is used to select one item’s index value at random from that array. The filename as $files[$file] is then concatenated and printed into the HTML with echo.
Date Taken/Exif Information
If desired, you can display other information about the image as well later down the page. I’ve done this to show the date the image was taken. This too is fairly simple when you get down to it, provided that your image does have this in its Exif metadata.
exif_read_data() is used to read this information. The DateTime IDF0 attribute is fed into strtotime() so it’s understandable by PHP and then echoed out as a new human-readable format with date(). And that’s about it!
Since its inception, w2krepo had lived on my personal dotcomboom.somnolescent.net domain. I’m excited to announce that it now resides at w2krepo.somnolescent.net.
Moving the files was fairly painless, as the subdomain had been set up under my current FTP account. What was next was redirecting the old URLs to match the new domain, also fairly simple when you get down to it: just this .htaccess file in the w2krepo folder:
RewriteEngine on
RewriteRule ^(.*)$ http://w2krepo.somnolescent.net/$1 [R=301,L]
That left one thing: trying to get my root directory on the subdomain to display the builtin Apache directory listing was a bit problematic, so I settled on using a basic PHP script scraped from what I could find online:
It’ll work as a root for now, and I intend on messing more with .htaccess to get the subdirectories’ listings all prettied up as well.
For the future, I intend to get some sort of system up for user submissions, whether that be by anonymous FTP or an upload form, as well as some more information on the indexes and homepage.
The Patio has been sunset, so consider this site an archive. My LiveJournal posts additionally have been relocated here for safekeeping.
You may find me at my dcb.somnol (tech) or lince.somnol (personal) web sites, and for more spur of the moment thoughts, my profile on status.ryslig.xyz. Thanks for visiting.