Another Picture of the Day script
Posted in Other Projects, Tutorials May 27th, 2020 by dotcomboom
<?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.)

Picture of the Day in PHP
Posted in Site Updates, Tutorials April 6th, 2020 by dotcomboom
pog

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.

    <?php
        $today = (int)date('Yz');
        srand($today);
    
        $files = glob('photography/*.jpg');
        $file = array_rand($files);
        echo '<a href="[#root#]photography/"><img src="' . $files[$file] . '" alt="Picture of the day" style="border: 3px solid #444"></a>';
    ?>

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.

<?php
    $exif = exif_read_data($files[$file], 'IFD0');
    $takenDate = strtotime($exif['DateTime']);
    echo date('F j, Y', $takenDate);
?>

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!

Here’s a place with some more Exif information in case you’re into experimenting. Also check out the PHP documentation.

w2krepo, now on a new subdomain!
Posted in Site Updates, w2krepo February 8th, 2020 by dotcomboom

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:

<?php
$fileList = glob('*');
foreach($fileList as $filename){
    if(is_dir($filename)){
        echo '<a href="', $filename, '/">', $filename, '</a><br>'; 
    }   
}
echo '<br>';
$fileList = glob('*.7z');
foreach($fileList as $filename){
    if(is_file($filename)){
        echo '<a href="', $filename, '">', $filename, '</a><br>'; 
    }   
}
?>

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.